Skip to content

Commit

Permalink
Generate defaults for all super types (#1087)
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Jan 11, 2024
1 parent 78589d1 commit 73c88e8
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,27 @@ public Property[] getProperties() {

public Property[] getProperties(boolean includeSuper) {
if (includeSuper) {
Map<String, Property> properties = new HashMap<>();
for (ConfigMappingInterface superType : superTypes) {
for (Property property : superType.getProperties()) {
properties.put(property.getMethod().getName(), property);
}
}
for (Property property : getProperties()) {
properties.put(property.getMethod().getName(), property);
Map<String, Property> properties = getSuperProperties(this);
for (Property property : this.properties) {
properties.put(property.getMemberName(), property);
}
return properties.values().toArray(new Property[0]);
} else {
return getProperties();
}
}

private static Map<String, Property> getSuperProperties(ConfigMappingInterface type) {
Map<String, Property> properties = new HashMap<>();
for (ConfigMappingInterface superType : type.getSuperTypes()) {
properties.putAll(getSuperProperties(superType));
for (Property property : superType.getProperties()) {
properties.put(property.getMemberName(), property);
}
}
return properties;
}

public boolean hasNamingStrategy() {
return interfaceType.getAnnotation(ConfigMapping.class) != null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -673,4 +673,56 @@ public interface DevServicesBuildTimeConfig {

Map<String, String> containerProperties();
}

@Test
void multipleLevelDefaults() {
Map<String, String> defaults = getDefaults(configClassWithPrefix(GrandChild.class));
assertEquals("parent", defaults.get("parent"));
assertEquals("child", defaults.get("child"));
assertEquals("grand-child", defaults.get("grand-child"));
assertEquals("child", defaults.get("override-by-child"));
assertEquals("grand-child", defaults.get("override-by-grand-child"));

SmallRyeConfig config = new SmallRyeConfigBuilder()
.withMapping(GrandChild.class)
.build();

GrandChild mapping = config.getConfigMapping(GrandChild.class);
assertEquals("parent", mapping.parent());
assertEquals("child", mapping.child());
assertEquals("grand-child", mapping.grandChild());
assertEquals("child", mapping.overrideByChild());
assertEquals("grand-child", mapping.overrideByGrandChild());
}

interface Parent {
@WithDefault("parent")
String parent();

@WithDefault("parent")
String overrideByChild();

@WithDefault("parent")
String overrideByGrandChild();
}

interface Child extends Parent {
@WithDefault("child")
String child();

@WithDefault("child")
String overrideByChild();

@WithDefault("child")
String overrideByGrandChild();
}

@ConfigMapping
interface GrandChild extends Child {
@WithDefault("grand-child")
String grandChild();

@WithDefault("grand-child")
String overrideByGrandChild();
}
}

0 comments on commit 73c88e8

Please sign in to comment.