Skip to content

Commit

Permalink
Fix @withname with dotted names in Map with quoted keys (#954)
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez authored Jun 28, 2023
1 parent 5070932 commit cd0a22e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ public StringBuilder getStringBuilder() {
}

public void reportProblem(RuntimeException problem) {
problem.printStackTrace();
problems.add(new Problem(problem.toString()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,17 +462,17 @@ private void processLazyMapValue(
String mapPath = String.join(".", currentPath);
addAction(currentPath, mapProperty, (mc, ni) -> {
// Place the cursor at the map path
NameIterator niAtMapPath = atMapPath(mapPath, ni);
Map<?, ?> map = getEnclosingMap.apply(mc, niAtMapPath);
NameIterator niMapPath = mapPath(mapPath, ni);
Map<?, ?> map = getEnclosingMap.apply(mc, niMapPath);

String rawMapKey;
String configKey;
boolean indexed = isIndexed(ni.getPreviousSegment());
if (indexed && ni.hasPrevious()) {
rawMapKey = normalizeIfIndexed(niAtMapPath.getName().substring(niAtMapPath.getPosition() + 1));
configKey = niAtMapPath.getAllPreviousSegmentsWith(rawMapKey);
rawMapKey = normalizeIfIndexed(niMapPath.getName().substring(niMapPath.getPosition() + 1));
configKey = niMapPath.getAllPreviousSegmentsWith(rawMapKey);
} else {
rawMapKey = niAtMapPath.getName().substring(niAtMapPath.getPosition() + 1);
rawMapKey = niMapPath.getName().substring(niMapPath.getPosition() + 1);
configKey = ni.getAllPreviousSegments();
}

Expand Down Expand Up @@ -607,7 +607,7 @@ private static String indexName(final String name, final String groupPath, final
return name;
}

private static NameIterator atMapPath(final String mapPath, final NameIterator propertyName) {
private static NameIterator mapPath(final String mapPath, final NameIterator propertyName) {
int segments = 0;
NameIterator countSegments = new NameIterator(mapPath);
while (countSegments.hasNext()) {
Expand Down Expand Up @@ -727,15 +727,15 @@ static class GetOrCreateEnclosingGroupInMap implements BiFunction<ConfigMappingC
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public ConfigMappingObject apply(final ConfigMappingContext context, final NameIterator ni) {
NameIterator atMapPath = atMapPath(mapPath, ni);
MapKey mapKey = mapKey(context, ni);
Map<?, ?> ourEnclosing = getEnclosingMap.apply(context, atMapPath);
NameIterator niMapPath = mapPath(mapPath, ni);
MapKey mapKey = mapKey(context, ni, niMapPath);
Map<?, ?> ourEnclosing = getEnclosingMap.apply(context, niMapPath);
ConfigMappingObject val = (ConfigMappingObject) context.getEnclosedField(enclosingGroup.getInterfaceType(),
mapKey.getKey(), ourEnclosing);
if (val == null) {
StringBuilder sb = context.getStringBuilder();
sb.replace(0, sb.length(), keyUnnamed ? atMapPath.getAllPreviousSegments()
: atMapPath.getAllPreviousSegmentsWith(mapKey.getKey()));
sb.replace(0, sb.length(), keyUnnamed ? niMapPath.getAllPreviousSegments()
: niMapPath.getAllPreviousSegmentsWith(mapKey.getKey()));
context.applyNamingStrategy(
namingStrategy(enclosedGroup.getGroupType().getNamingStrategy(), enclosingGroup.getNamingStrategy()));
val = (ConfigMappingObject) context.constructGroup(enclosedGroup.getGroupType().getInterfaceType());
Expand All @@ -751,7 +751,7 @@ public ConfigMappingObject apply(final ConfigMappingContext context, final NameI
// Get all the available indexes
List<Integer> indexes = keyUnnamed ? List.of(0)
: context.getConfig()
.getIndexedPropertiesIndexes(atMapPath.getAllPreviousSegmentsWith(mapKey.getNameKey()));
.getIndexedPropertiesIndexes(niMapPath.getAllPreviousSegmentsWith(mapKey.getNameKey()));
collection = collectionFactory.apply(indexes.size());
// Initialize all expected elements in the list
if (collection instanceof List) {
Expand Down Expand Up @@ -780,13 +780,15 @@ public void accept(final ConfigMappingContext context, final NameIterator ni) {
apply(context, ni);
}

private MapKey mapKey(final ConfigMappingContext context, final NameIterator ni) {
private MapKey mapKey(final ConfigMappingContext context, final NameIterator ni, final NameIterator mapPath) {
if (keyUnnamed && enclosingMap.getKeyUnnamed() == null) {
return new MapKey(null, null, null, 0);
}

String rawKey = keyUnnamed ? enclosingMap.getKeyUnnamed() : atMapPath(this.mapPath, ni).getNextSegment();
String pathKey = ni.getAllPreviousSegments();
String rawKey = keyUnnamed ? enclosingMap.getKeyUnnamed() : mapPath.getNextSegment();
mapPath.next();
String pathKey = mapPath.getAllPreviousSegments();
mapPath.previous();
Converter<?> converterKey = context.getKeyConverter(enclosingGroup.getInterfaceType(),
enclosingMap.getMethod().getName(), enclosingMap.getLevels() - 1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2078,6 +2078,7 @@ void withNameMultipleSegments() {
"my.optional.rest.api.url", "http://localhost:8094",
"my.list[0].rest.api.url", "http://localhost:8094",
"my.map.key.rest.api.url", "http://localhost:8094",
"my.map.\"quoted-key\".rest.api.url", "http://localhost:8094",
"my.map-list.key[0].rest.api.url", "http://localhost:8094"))
.build();

Expand All @@ -2087,6 +2088,7 @@ void withNameMultipleSegments() {
assertEquals("http://localhost:8094", mapping.optional().get().apiUrl());
assertEquals("http://localhost:8094", mapping.list().get(0).apiUrl());
assertEquals("http://localhost:8094", mapping.map().get("key").apiUrl());
assertEquals("http://localhost:8094", mapping.map().get("quoted-key").apiUrl());
assertEquals("http://localhost:8094", mapping.mapList().get("key").get(0).apiUrl());
assertEquals("other", mapping.other());
assertEquals("other", config.getConfigValue("my.rest.api.other").getValue());
Expand Down

0 comments on commit cd0a22e

Please sign in to comment.