Skip to content

Commit

Permalink
Change return type in LiteComponentFactory#createSection() to Result.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rollczi committed Dec 20, 2021
1 parent a1e3895 commit 20812da
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@ public LiteCommands register() {

for (Object instance : commandInstances) {
registerResolvers.register(factory.createSection(instance)
.orThrow(() -> new IllegalArgumentException(instance.getClass() + " instance isn't a section")));
.orElseThrow(RuntimeException::new));
}

for (Class<?> commandClass : commandClasses) {
registerResolvers.register(factory.createSection(commandClass)
.orThrow(() -> new IllegalArgumentException(commandClass + " class isn't a section")));
.orElseThrow(RuntimeException::new));
}

for (LiteComponent resolver : registerResolvers.getResolvers().values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public LiteComponentFactory(Logger logger, Injector injector, AnnotationParser p
this.parser = parser;
}

public Option<LiteSection> createSection(Object sectionInstance) {
public Result<LiteSection, Throwable> createSection(Object sectionInstance) {
Class<?> sectionClass = sectionInstance.getClass();

return parser.parse(sectionClass).map(scope -> {
Expand All @@ -35,7 +35,7 @@ public Option<LiteSection> createSection(Object sectionInstance) {
Set<LiteSection> innerSections = PandaStream.of(sectionClass.getClasses())
.concat(sectionClass.getDeclaredClasses())
.distinct()
.mapOpt(innerClass -> Result.attempt(Throwable.class, () -> createSection(innerClass))
.map(innerClass -> createSection(innerClass)
.orElseThrow(error -> new RuntimeException("Can't create inner class " + innerClass, error)))
.toSet();

Expand All @@ -44,11 +44,11 @@ public Option<LiteSection> createSection(Object sectionInstance) {
.resolvers(innerSections)
.resolvers(executions)
.build();
});
}).toResult(new RuntimeException(sectionInstance.getClass() + " class isn't a section"));
}

public Option<LiteSection> createSection(Class<?> sectionClass) {
return Option.attempt(Throwable.class, () -> injector.newInstance(sectionClass))
public Result<LiteSection, Throwable> createSection(Class<?> sectionClass) {
return Result.attempt(Throwable.class, () -> injector.newInstance(sectionClass))
.flatMap(this::createSection);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class LiteComponentFactoryTest : LiteCommandsSpec() {

val sectionOption = factory.createSection(TestCommand::class.java)

assertTrue(sectionOption.isPresent)
assertTrue(sectionOption.isOk)

val section = sectionOption.get()
val scope = section.scope
Expand Down

0 comments on commit 20812da

Please sign in to comment.