Skip to content

Commit

Permalink
Recursively ignore mapping path (#824)
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez authored Sep 16, 2022
1 parent c495c58 commit f403786
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ final class ConfigMappingProvider implements Serializable {
static {
final KeyMap<BiConsumer<ConfigMappingContext, NameIterator>> map = new KeyMap<>();
map.putRootValue(DO_NOTHING);
//noinspection CollectionAddedToSelf
map.putAny(map);
IGNORE_EVERYTHING = map;
}

Expand Down Expand Up @@ -89,14 +91,26 @@ final class ConfigMappingProvider implements Serializable {
if (ignoredPath[len - 1].equals("**")) {
found = matchActions.findOrAdd(ignoredPath, 0, len - 1);
found.putRootValue(DO_NOTHING);
found.putAny(IGNORE_EVERYTHING);
ignoreRecursively(found);
} else {
found = matchActions.findOrAdd(ignoredPath);
found.putRootValue(DO_NOTHING);
}
}
}

static void ignoreRecursively(KeyMap<BiConsumer<ConfigMappingContext, NameIterator>> root) {
if (root.getAny() == null) {
root.putAny(IGNORE_EVERYTHING);
} else {
ignoreRecursively(root.getAny());
}

for (final KeyMap<BiConsumer<ConfigMappingContext, NameIterator>> value : root.values()) {
ignoreRecursively(value);
}
}

static final class ConsumeOneAndThen implements BiConsumer<ConfigMappingContext, NameIterator> {
private final BiConsumer<ConfigMappingContext, NameIterator> delegate;

Expand Down Expand Up @@ -932,6 +946,9 @@ private void mapConfiguration(SmallRyeConfig config, ConfigMappings mappings) th
}
}

boolean validateUnknown = config.getOptionalValue(SMALLRYE_CONFIG_MAPPING_VALIDATE_UNKNOWN, boolean.class)
.orElse(this.validateUnknown);

// lazily sweep
Set<String> unknownProperties = new HashSet<>();
for (String name : config.getPropertyNames()) {
Expand All @@ -945,7 +962,7 @@ private void mapConfiguration(SmallRyeConfig config, ConfigMappings mappings) th
if (action != null) {
action.accept(context, ni);
} else {
if (validateUnknown(validateUnknown, config)) {
if (validateUnknown) {
unknownProperties.add(name);
}
}
Expand Down Expand Up @@ -1118,11 +1135,6 @@ private static boolean isIndexed(final String propertyName) {
return false;
}

private static boolean validateUnknown(final boolean validateUnknown, final SmallRyeConfig config) {
return config.getOptionalValue(SMALLRYE_CONFIG_MAPPING_VALIDATE_UNKNOWN, Boolean.class)
.orElse(validateUnknown);
}

private static void unknownProperties(Set<String> properties, ConfigMappingContext context) {
Set<String> usedProperties = new HashSet<>();
for (String property : context.getConfig().getPropertyNames()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1581,4 +1581,45 @@ interface Ssl {
List<String> protocols();
}
}

@Test
void ignoreNestedUnknown() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.addDefaultInterceptors()
.withMapping(IgnoreNestedUnknown.class)
.withSources(config("ignore.nested.value", "value", "ignore.nested.ignore", "ignore"))
.withSources(config("ignore.nested.nested.value", "value", "ignore.nested.nested.ignore", "ignore"))
.withSources(config("ignore.nested.optional.value", "value", "ignore.nested.optional.ignore", "ignore"))
.withSources(config("ignore.nested.list[0].value", "value", "ignore.nested.list[0].ignore", "ignore"))
.withSources(config("ignore.nested.map.key", "value", "ignore.nested.map.ignored.ignored", "ignore"))
.withSources(config("ignore.nested.ignore.ignore", "ignore"))
.withMappingIgnore("ignore.nested.**")
.build();

IgnoreNestedUnknown mapping = config.getConfigMapping(IgnoreNestedUnknown.class);

assertEquals("value", mapping.value());
assertEquals("value", mapping.nested().value());
assertTrue(mapping.optional().isPresent());
assertEquals("value", mapping.optional().get().value());
assertEquals("value", mapping.list().get(0).value());
assertEquals("value", mapping.map().get("key"));
}

@ConfigMapping(prefix = "ignore.nested")
interface IgnoreNestedUnknown {
String value();

Nested nested();

Optional<Nested> optional();

List<Nested> list();

Map<String, String> map();

interface Nested {
String value();
}
}
}

0 comments on commit f403786

Please # to comment.