Skip to content

Commit

Permalink
Support both indexed and plain keys for mappings leaf Maps (#971)
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez authored Aug 4, 2023
1 parent e018a33 commit 30be0d1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ private void processLazyMapValue(
valueConv = config.requireConverter(valueRawType);
}

if (indexed) {
if (mapProperty.getValueProperty().isCollection() && valConvertWith == null) {
CollectionProperty collectionProperty = mapProperty.getValueProperty().asCollection();
Class<?> collectionRawType = collectionProperty.getCollectionRawType();
IntFunction collectionFactory = createCollectionFactory(collectionRawType);
Expand All @@ -505,9 +505,9 @@ private void processLazyMapValue(
}
});
// action to match all segments of a key after the map path
KeyMap mapAction = matchActions.find(currentPath);
KeyMap mapAction = matchActions.find(mapPath);
if (mapAction != null) {
mapAction.putAny(matchActions.find(currentPath));
mapAction.putAny(matchActions.find(mapPath));
}

// collections may also be represented without [] so we need to register both paths
Expand Down Expand Up @@ -543,7 +543,7 @@ private void processLazyMapValue(
} else if (property.isCollection()) {
CollectionProperty collectionProperty = property.asCollection();
Property element = collectionProperty.getElement();
if (!element.hasConvertWith() && !keyUnnamed) {
if (!element.hasConvertWith() && !keyUnnamed && !element.isLeaf()) {
currentPath.addLast(currentPath.removeLast() + "[*]");
}
processLazyMapValue(currentPath, matchActions, defaultValues, mapProperty, element, keyUnnamed, keyConvertWith,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,11 +572,11 @@ void mapWithListsGroup() {
.withSources(config(
"map.roles.user[0].name", "p1",
"map.roles.user[0].aliases[0]", "user-role-p1",
"map.roles.user[0].permissions.read[0]", "read"))
"map.roles.user[0].permissions.read", "read"))
.withSources(config(
"map.roles.admin[0].name", "p2",
"map.roles.admin[0].aliases", "admin-role-p2,administrator-role-p2",
"map.roles.admin[0].permissions.read[0]", "read",
"map.roles.admin[0].permissions.read", "read",
"map.roles.admin[1].name", "p3",
"map.roles.admin[1].aliases", "admin-role-p3,administrator-role-p3",
"map.roles.admin[1].permissions.write[0]", "create",
Expand Down Expand Up @@ -727,4 +727,26 @@ void mapWithKeyAndListConverters() {
assertEquals("two", mapping.list().get("2").get(0));
assertEquals("2", mapping.list().get("2").get(1));
}

@ConfigMapping(prefix = "map")
interface MapIndexedAndPlain {
@WithParentName
Map<String, List<String>> map();
}

@Test
void mapIndexedAndPlain() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withMapping(MapIndexedAndPlain.class, "map")
.withSources(config(
"map.one[0]", "one", "map.one[1]", "1",
"map.two", "two,2"))
.build();

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

assertEquals("one", mapping.map().get("one").get(0));
assertEquals("1", mapping.map().get("one").get(1));
assertEquals("two", mapping.map().get("two").get(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ void star() {
orAdd.putAny(map.findOrAdd("map.key.*"));

assertEquals("value", map.findRootValue("map.key.one"));
assertEquals("value", map.findRootValue("map.key.one[1]"));
assertEquals("value", map.findRootValue("map.key.one.two"));
assertEquals("value", map.findRootValue("map.key.one.two.three"));
}
Expand Down

0 comments on commit 30be0d1

Please sign in to comment.