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

Allow using square brackets in map keys #711

Merged
merged 1 commit into from
Feb 7, 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 @@ -657,17 +657,19 @@ public void accept(final ConfigMappingContext context, final NameIterator ni) {
}

private String mapKey(final NameIterator ni) {
NameIterator mapPath = new NameIterator(normalizeIfIndexed(this.mapPath));
NameIterator mapKey = new NameIterator(normalizeIfIndexed(ni.getName()));
while (mapPath.hasNext() && mapKey.hasNext()) {
if (mapPath.getNextSegment().equals(mapKey.getNextSegment()) || mapPath.getNextSegment().equals("*")) {
NameIterator mapPath = new NameIterator(this.mapPath);
NameIterator mapPathKey = new NameIterator(anyIfIndexed(ni.getName()));
NameIterator mapRealKey = new NameIterator(ni.getName());
while (mapPath.hasNext() && mapPathKey.hasNext()) {
if (mapPath.getNextSegment().equals(mapPathKey.getNextSegment()) || mapPath.getNextSegment().equals("*")) {
mapPath.next();
mapKey.next();
mapPathKey.next();
mapRealKey.next();
} else {
break;
}
}
return mapKey.hasNext() ? mapKey.getNextSegment() : ni.getPreviousSegment();
return mapRealKey.hasNext() ? mapRealKey.getNextSegment() : ni.getPreviousSegment();
}
}

Expand Down Expand Up @@ -859,6 +861,25 @@ private static String normalizeIfIndexed(final String propertyName) {
return propertyName;
}

private static String anyIfIndexed(final String propertyName) {
StringBuilder builder = new StringBuilder();

int pos = 0;
for (int i = 0; i < propertyName.length(); i++) {
if (propertyName.charAt(i) == '[') {
builder.append(propertyName, pos, i + 1);
pos = i;
} else if (propertyName.charAt(i) == ']') {
builder.append("*");
builder.append(propertyName, i, i + 1);
pos = i + 1;
}
}
builder.append(propertyName, pos, propertyName.length());

return builder.toString();
}

private static boolean validateUnknown(final boolean validateUnknown, final SmallRyeConfig config) {
return config.getOptionalValue(SMALLRYE_CONFIG_MAPPING_VALIDATE_UNKNOWN, Boolean.class)
.orElse(validateUnknown);
Expand Down
15 changes: 14 additions & 1 deletion implementation/src/main/java/io/smallrye/config/KeyMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private KeyMap<V> findOrDefault(final String seg) {
KeyMap<V> next = getOrDefault(seg, any);
if (seg.endsWith("]")) {
int begin = seg.lastIndexOf('[');
if (begin != -1) {
if (begin != -1 && isValidIndex(seg, begin)) {
next = getOrDefault(seg.substring(0, begin), any);
if (next != null) {
next = next.find("[");
Expand All @@ -152,6 +152,19 @@ private KeyMap<V> findOrDefault(final String seg) {
return next;
}

private boolean isValidIndex(final String seg, final int begin) {
try {
String index = seg.substring(begin + 1, seg.length() - 1);
if (index.equals("*")) {
return true;
}
Integer.parseInt(index);
return true;
} catch (NumberFormatException e) {
return false;
}
}

public KeyMap<V> findOrAdd(final String path) {
return findOrAdd(new NameIterator(path));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,4 +499,24 @@ void mappingCollectionsMap() {
assertTrue(mapping.present().isPresent());
assertEquals("localhost", mapping.present().get().get(0).get("localhost"));
}

public interface MapKeyWithIndexedSyntax {
Map<String, String> map();
}

@Test
void mapKeyWithIndexedSyntax() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withMapping(MapKeyWithIndexedSyntax.class)
.withSources(config("map.key", "value"))
.withSources(config("map.key[x]", "value"))
.withSources(config("map.key[0-9]", "value"))
.build();

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

assertEquals("value", mapping.map().get("key"));
assertEquals("value", mapping.map().get("key[x]"));
assertEquals("value", mapping.map().get("key[0-9]"));
}
}