Skip to content

Commit

Permalink
#92 : Compiler: ConfigBuilder DEFAULTS refactoring #92
Browse files Browse the repository at this point in the history
  • Loading branch information
Gmugra committed May 10, 2021
1 parent d04dc75 commit 5021e32
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public interface GeneratorPart {
TypeName CONCURRENTHASHMAP_STRING_OBJECT = ParameterizedTypeName.get(CONCURRENTHASHMAP, STRING, OBJECT);

String VALUES_ATTR = "VALUES";
String DEFAULTS_ATTR = "DEFAULTS";
String URIS_ATTR = "URIS";

void addPart(TypeSpec.Builder classBuilder, Generator generator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.lang.model.element.Modifier;
Expand Down Expand Up @@ -37,7 +38,7 @@ public class BuildPart implements GeneratorPart {

addConverters(buildBuilder, generator.methodsInfo());

buildBuilder.addStatement("Map<$T,$T> values = new $T<>()", String.class, Object.class, HashMap.class);
buildBuilder.addStatement("$T<$T,$T> values = new $T<>()", Map.class, String.class, Object.class, HashMap.class);
generator.methodsInfo().forEach(mi -> buildBuilder.addStatement("values.put($S, $L)", mi.key(), convert(mi)));

classBuilder.addMethod(buildBuilder.addStatement("return new $T(values)", configClass).build());
Expand Down Expand Up @@ -123,6 +124,6 @@ private CodeBlock split(MethodInfo mi) {
}

private CodeBlock defaultValue(MethodInfo mi) {
return mi.defaultValue().map(s -> CodeBlock.of(", $L.get($S)", DEFAULTS_ATTR, mi.name())).orElse(CodeBlock.of(""));
return mi.defaultValue().map(s -> CodeBlock.of(", $S", s)).orElse(CodeBlock.of(""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

public final class ConfigBuilderGenerator extends Generator {

private static final List<GeneratorPart> PARTS = Arrays.asList(new UrisPart(), new DefaultPart(), new ConstructorPart(),
new BuildPart());
private static final List<GeneratorPart> PARTS = Arrays.asList(new UrisPart(), new ConstructorPart(), new BuildPart());

public ConfigBuilderGenerator(TypeElement interfaceElement, List<MethodInfo> methodsInfo, InterfaceInfo interfaceInfo) {
super(interfaceElement, methodsInfo, ConfigBuilder.BUILDER_CLASSNAME_PREFIX, interfaceInfo);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,8 @@
import net.cactusthorn.config.core.loader.Loaders;

public final class ConfigBuilder_TestConfig extends ConfigBuilder<Config_TestConfig> {

private static final String[] URIS = new String[] {""};

private static final Map<String, String> DEFAULTS;
static {
DEFAULTS = new HashMap<>();
DEFAULTS.put("aaa", "ddd");
DEFAULTS.put("dlist", "A,A");
DEFAULTS.put("dlist2", "B,B");
DEFAULTS.put("dset", "A,A");
DEFAULTS.put("dset2", "B,B");
DEFAULTS.put("dsort", "A,A");
DEFAULTS.put("dsort2", "B,B");
DEFAULTS.put("dstr", "A");
DEFAULTS.put("dstr2", "B");
DEFAULTS.put("testconverter", "default");
}

public ConfigBuilder_TestConfig(final Loaders loaders) {
super(loaders);
}
Expand All @@ -40,15 +24,15 @@ public Config_TestConfig build() {
CONVERTERS.computeIfAbsent(DurationConverter.class, c -> new DurationConverter());
CONVERTERS.computeIfAbsent(ToTestConverter.class, c -> new ToTestConverter());
Map<String,Object> values = new HashMap<>();
values.put("aaa", ch.get(s -> s, "aaa", DEFAULTS.get("aaa")));
values.put("test.dlist", ch.getList(s -> s, "test.dlist", ",", DEFAULTS.get("dlist")));
values.put("test.dlist2", ch.getList(s -> s, "test.dlist2", ",", DEFAULTS.get("dlist2")));
values.put("test.dset", ch.getSet(s -> s, "test.dset", ",", DEFAULTS.get("dset")));
values.put("test.dset2", ch.getSet(s -> s, "test.dset2", ",", DEFAULTS.get("dset2")));
values.put("test.dsort", ch.getSortedSet(s -> s, "test.dsort", ",", DEFAULTS.get("dsort")));
values.put("test.dsort2", ch.getSortedSet(s -> s, "test.dsort2", ",", DEFAULTS.get("dsort2")));
values.put("test.dstr", ch.get(s -> s, "test.dstr", DEFAULTS.get("dstr")));
values.put("test.dstr2", ch.get(s -> s, "test.dstr2", DEFAULTS.get("dstr2")));
values.put("aaa", ch.get(s -> s, "aaa", "ddd"));
values.put("test.dlist", ch.getList(s -> s, "test.dlist", ",", "A,A"));
values.put("test.dlist2", ch.getList(s -> s, "test.dlist2", ",", "B,B"));
values.put("test.dset", ch.getSet(s -> s, "test.dset", ",", "A,A"));
values.put("test.dset2", ch.getSet(s -> s, "test.dset2", ",", "B,B"));
values.put("test.dsort", ch.getSortedSet(s -> s, "test.dsort", ",", "A,A"));
values.put("test.dsort2", ch.getSortedSet(s -> s, "test.dsort2", ",", "B,B"));
values.put("test.dstr", ch.get(s -> s, "test.dstr", "A"));
values.put("test.dstr2", ch.get(s -> s, "test.dstr2", "B"));
values.put("test.duration", ch.getOptional(s -> convert(DurationConverter.class, s), "test.duration"));
values.put("test.list", ch.getList(s -> s, "test.list", ","));
values.put("test.olist", ch.getOptionalList(s -> s, "test.olist", ","));
Expand All @@ -62,7 +46,7 @@ public Config_TestConfig build() {
values.put("test.set", ch.getSet(s -> s, "test.set", ","));
values.put("test.sort", ch.getSortedSet(s -> s, "test.sort", ","));
values.put("test.string", ch.get(s -> s, "test.string"));
values.put("test.testconverter", ch.get(s -> convert(ToTestConverter.class, s), "test.testconverter", DEFAULTS.get("testconverter")));
values.put("test.testconverter", ch.get(s -> convert(ToTestConverter.class, s), "test.testconverter", "default"));
return new Config_TestConfig(values);
}
}

0 comments on commit 5021e32

Please sign in to comment.