Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nested optionals and maps with empty values in the middle #740

Merged
merged 2 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -323,23 +323,12 @@ private void processLazyPropertyInGroup(

if (optional && property.asOptional().getNestedProperty().isGroup()) {
GroupProperty nestedGroup = property.asOptional().getNestedProperty().asGroup();
// To recursively create Optional nested groups
BiFunction<ConfigMappingContext, NameIterator, ConfigMappingObject> delegate = new BiFunction<ConfigMappingContext, NameIterator, ConfigMappingObject>() {
@SuppressWarnings("unchecked")
@Override
public ConfigMappingObject apply(ConfigMappingContext configMappingContext, NameIterator nameIterator) {
if (matchAction instanceof BiFunction) {
return (ConfigMappingObject) ((BiFunction) matchAction).apply(configMappingContext, nameIterator);
}
return null;
}
};
GetOrCreateEnclosingGroupInGroup nestedMatchAction = new GetOrCreateEnclosingGroupInGroup(
property.isParentPropertyName() ? delegate : new ConsumeOneAndThenFn<>(delegate),
property.isParentPropertyName() ? new GetNestedEnclosing(matchAction)
: new ConsumeOneAndThenFn<>(new GetNestedEnclosing(matchAction)),
group, nestedGroup, currentPath);
processLazyGroupInGroup(currentPath, matchActions, defaultValues, namingStrategy, nestedGroup.getGroupType(),
nestedMatchAction,
nestedMatchAction, new HashSet<>());
nestedMatchAction, nestedMatchAction, new HashSet<>());
} else if (property.isGroup()) {
GroupProperty asGroup = property.asGroup();
GetOrCreateEnclosingGroupInGroup nestedEnclosingFunction = new GetOrCreateEnclosingGroupInGroup(
Expand Down Expand Up @@ -397,8 +386,9 @@ public ConfigMappingObject apply(ConfigMappingContext configMappingContext, Name
}
}
} else if (property.isMap()) {
processLazyMapInGroup(currentPath, matchActions, defaultValues, property.asMap(), getEnclosingFunction,
namingStrategy, group);
GetNestedEnclosing nestedMatchAction = new GetNestedEnclosing(matchAction);
processLazyMapInGroup(currentPath, matchActions, defaultValues, property.asMap(), nestedMatchAction, namingStrategy,
group);
} else if (property.isCollection() || optional && property.asOptional().getNestedProperty().isCollection()) {
CollectionProperty collectionProperty = optional ? property.asOptional().getNestedProperty().asCollection()
: property.asCollection();
Expand All @@ -412,7 +402,8 @@ private void processLazyMapInGroup(
final ArrayDeque<String> currentPath,
final KeyMap<BiConsumer<ConfigMappingContext, NameIterator>> matchActions,
final KeyMap<String> defaultValues,
final MapProperty property, BiFunction<ConfigMappingContext, NameIterator, ConfigMappingObject> getEnclosingGroup,
final MapProperty property,
final BiFunction<ConfigMappingContext, NameIterator, ConfigMappingObject> getEnclosingGroup,
final NamingStrategy namingStrategy,
final ConfigMappingInterface enclosingGroup) {

Expand Down Expand Up @@ -744,6 +735,24 @@ public ConfigMappingObject apply(final ConfigMappingContext context, final NameI
}
}

// To recursively create Optional nested groups
static class GetNestedEnclosing implements BiFunction<ConfigMappingContext, NameIterator, ConfigMappingObject> {
private final BiConsumer<ConfigMappingContext, NameIterator> matchAction;

public GetNestedEnclosing(final BiConsumer<ConfigMappingContext, NameIterator> matchAction) {
this.matchAction = matchAction;
}

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public ConfigMappingObject apply(final ConfigMappingContext configMappingContext, final NameIterator nameIterator) {
if (matchAction instanceof BiFunction) {
return (ConfigMappingObject) ((BiFunction) matchAction).apply(configMappingContext, nameIterator);
}
return null;
}
}

public static Builder builder() {
return new Builder();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1402,7 +1402,7 @@ interface Second {
}

@Test
void nestedOptionals() throws Exception {
void nestedOptionals() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withMapping(NestedOptionals.class)
.withSources(config("optionals.first.second.value", "value"))
Expand All @@ -1414,4 +1414,33 @@ void nestedOptionals() throws Exception {
assertTrue(mapping.first().get().second().isPresent());
assertEquals("value", mapping.first().get().second().get().value());
}

@ConfigMapping(prefix = "optionals")
interface NestedOptionalMap {
Optional<First> first();

interface First {
Map<String, Second> second();
}

interface Second {
Optional<String> value();
}
}

@Test
void nestedOptionalMap() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withMapping(NestedOptionalMap.class)
.withSources(config("optionals.first.second.key.value", "value"))
.build();

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

assertTrue(mapping.first().isPresent());
assertEquals(1, mapping.first().get().second().size());
assertNotNull(mapping.first().get().second().get("key"));
assertTrue(mapping.first().get().second().get("key").value().isPresent());
assertEquals("value", mapping.first().get().second().get("key").value().get());
}
}