Skip to content

Commit

Permalink
GH-316 Expose SimpleSchematicGenerator methods for override. (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rollczi authored Dec 2, 2023
1 parent 7063b1c commit 6d0605a
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ public interface SchematicGenerator<SENDER> {

Schematic generate(SchematicInput<SENDER> schematicInput);

@Deprecated
static <SENDER> SchematicGenerator<SENDER> from(SchematicFormat format, ValidatorService<SENDER> validatorService, WrapperRegistry wrapperRegistry) {
return new SchematicGeneratorSimpleImpl<>(format, validatorService, wrapperRegistry);
return new SimpleSchematicGenerator<>(format, validatorService, wrapperRegistry);
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package dev.rollczi.litecommands.schematic;

import dev.rollczi.litecommands.argument.Argument;
import dev.rollczi.litecommands.command.CommandRoute;
import dev.rollczi.litecommands.command.executor.CommandExecutor;
import dev.rollczi.litecommands.validator.ValidatorService;
import dev.rollczi.litecommands.wrapper.WrapperRegistry;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class SimpleSchematicGenerator<SENDER> implements SchematicGenerator<SENDER> {

private static final String SEPARATOR = " ";

protected final SchematicFormat format;
protected final ValidatorService<SENDER> validatorService;
protected final WrapperRegistry wrapperRegistry;

public SimpleSchematicGenerator(SchematicFormat format, ValidatorService<SENDER> validatorService, WrapperRegistry wrapperRegistry) {
this.format = format;
this.validatorService = validatorService;
this.wrapperRegistry = wrapperRegistry;
}

@Override
public Schematic generate(SchematicInput<SENDER> schematicInput) {
List<String> schematics = generateRaw(schematicInput)
.map(schematic -> format.prefix() + schematic.trim() + format.suffix())
.collect(Collectors.toList());

return new Schematic(schematics);
}

protected Stream<String> generateRaw(SchematicInput<SENDER> schematicInput) {
CommandExecutor<SENDER> executor = schematicInput.getExecutor();
String base = schematicInput.collectRoutes().stream()
.map(route -> route.getName())
.collect(Collectors.joining(SEPARATOR))
+ SEPARATOR;

if (executor != null) {
return Stream.of(base + generateExecutor(schematicInput, executor));
}

return generateRoute(schematicInput, schematicInput.getLastRoute(), base);
}

protected Stream<String> generateRoute(SchematicInput<SENDER> input, CommandRoute<SENDER> route, String base) {
Stream<String> children = route.getChildren().stream()
.flatMap(subRoute -> generateRoute(input, subRoute, base + subRoute.getName() + SEPARATOR));

Stream<String> executors = route.getExecutors().stream()
.filter(executor -> isVisible(input, executor))
.map(executor -> base + generateExecutor(input, executor));

return Stream.concat(executors, children);
}

protected String generateExecutor(SchematicInput<SENDER> input, CommandExecutor<SENDER> executor) {
return executor.getArguments().stream()
.map(argument -> String.format(generateArgumentFormat(input, argument), generateArgumentName(input, argument)))
.collect(Collectors.joining(SEPARATOR));
}

protected String generateArgumentFormat(SchematicInput<SENDER> input, Argument<?> argument) {
return this.isOptional(input, argument) ? format.optionalArgumentFormat() : format.argumentFormat();
}

protected String generateArgumentName(SchematicInput<SENDER> input, Argument<?> argument) {
return argument.getName();
}

protected boolean isVisible(SchematicInput<SENDER> input, CommandExecutor<SENDER> executor) {
return validatorService.validate(input.getInvocation(), executor).isContinue();
}

protected boolean isOptional(SchematicInput<SENDER> input, Argument<?> argument) {
return wrapperRegistry.getWrappedExpectedFactory(argument.getWrapperFormat()).canCreateEmpty() || argument.hasDefaultValue();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class SchematicGeneratorTest {

static WrapperRegistry wrapperRegistry = new WrapperRegistry();
static ValidatorService validatorService = new ValidatorService();
static SchematicGenerator schematicGenerator = SchematicGenerator.from(SchematicFormat.angleBrackets(), validatorService, wrapperRegistry);
static SchematicGenerator schematicGenerator = new SimpleSchematicGenerator<>(SchematicFormat.angleBrackets(), validatorService, wrapperRegistry);

@BeforeAll
static void beforeAll() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import dev.rollczi.litecommands.scheduler.SchedulerSameThreadImpl;
import dev.rollczi.litecommands.schematic.SchematicFormat;
import dev.rollczi.litecommands.schematic.SchematicGenerator;
import dev.rollczi.litecommands.schematic.SimpleSchematicGenerator;
import dev.rollczi.litecommands.scope.Scope;
import dev.rollczi.litecommands.argument.suggester.Suggester;
import dev.rollczi.litecommands.suggestion.SuggestionResult;
Expand Down Expand Up @@ -91,7 +92,7 @@ public class LiteCommandsBaseBuilder<SENDER, C extends PlatformSettings, B exten
protected final WrapperRegistry wrapperRegistry = new WrapperRegistry();

protected Scheduler scheduler = new SchedulerSameThreadImpl();
protected SchematicGenerator<SENDER> schematicGenerator = SchematicGenerator.from(SchematicFormat.angleBrackets(), validatorService, wrapperRegistry);
protected SchematicGenerator<SENDER> schematicGenerator = new SimpleSchematicGenerator<>(SchematicFormat.angleBrackets(), validatorService, wrapperRegistry);

/**
* Constructor for {@link LiteCommandsBaseBuilder}
Expand Down Expand Up @@ -396,7 +397,7 @@ public LiteCommandsBuilder<SENDER, C, B> schematicGenerator(SchematicGenerator<S

@Override
public LiteCommandsBuilder<SENDER, C, B> schematicGenerator(SchematicFormat format) {
this.schematicGenerator = SchematicGenerator.from(format, validatorService, wrapperRegistry);
this.schematicGenerator = new SimpleSchematicGenerator<>(format, validatorService, wrapperRegistry);
return this;
}

Expand Down

0 comments on commit 6d0605a

Please sign in to comment.