Skip to content

Commit

Permalink
Keep all mapping names (#1158)
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez authored Apr 24, 2024
1 parent b841a1c commit c3fcadb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ private void matchPropertiesWithEnv(final Map<Class<?>, Set<String>> roots) {
matchedRoot = "";
}

for (Map<PropertyName, Set<PropertyName>> mappingsNames : names.getNames().values()) {
Set<PropertyName> propertyNames = mappingsNames.get(new PropertyName(""));
for (Map<PropertyName, List<PropertyName>> mappingsNames : names.getNames().values()) {
List<PropertyName> propertyNames = mappingsNames.get(new PropertyName(""));
if (propertyNames == null) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,41 @@

import static io.smallrye.config.PropertyName.name;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* Represents the full structure of mapping classes per class name, relative path and property name.
*/
class ConfigMappingNames {
private final Map<String, Map<PropertyName, Set<PropertyName>>> names;
private final Map<String, Map<PropertyName, List<PropertyName>>> names;

ConfigMappingNames(final Map<String, Map<String, Set<String>>> names) {
this.names = new HashMap<>(names.size());

for (Map.Entry<String, Map<String, Set<String>>> mappings : names.entrySet()) {
Map<PropertyName, Set<PropertyName>> mappingPropertyNames = new HashMap<>();
// We chance this to be a List because different names may equal the PropertyName
Map<PropertyName, List<PropertyName>> mappingPropertyNames = new HashMap<>();
for (Map.Entry<String, Set<String>> mappingNames : mappings.getValue().entrySet()) {
PropertyName key = name(mappingNames.getKey());
mappingPropertyNames.putIfAbsent(key, new HashSet<>());
mappingPropertyNames.putIfAbsent(key, new ArrayList<>());
// Give priority to the star key
if (key.getName().contains("*")) {
mappingPropertyNames.put(key, mappingPropertyNames.remove(key));
}

Set<PropertyName> values = mappingPropertyNames.get(key);
for (String value : mappingNames.getValue()) {
// Give priority to the star key
if (value.contains("*")) {
values.remove(name(value));
}
values.add(name(value));
mappingPropertyNames.get(key).add(name(value));
}
mappingPropertyNames.get(key).addAll(values);
}
this.names.put(mappings.getKey(), mappingPropertyNames);
}
}

public Map<String, Map<PropertyName, Set<PropertyName>>> getNames() {
public Map<String, Map<PropertyName, List<PropertyName>>> getNames() {
return names;
}

Expand All @@ -56,7 +52,7 @@ public Map<String, Map<PropertyName, Set<PropertyName>>> getNames() {
* @return <code>true</code> if a runtime config name exits in the mapping names or <code>false</code> otherwise
*/
boolean hasAnyName(final String mapping, final String rootPath, final String path, final Iterable<String> names) {
Map<PropertyName, Set<PropertyName>> mappings = this.names.get(mapping);
Map<PropertyName, List<PropertyName>> mappings = this.names.get(mapping);
if (mappings == null) {
return false;
}
Expand All @@ -81,7 +77,7 @@ boolean hasAnyName(final String mapping, final String rootPath, final String pat
} else {
mappingName = name(path.substring(rootPath.length()));
}
Set<PropertyName> mappingNames = mappings.get(mappingName);
List<PropertyName> mappingNames = mappings.get(mappingName);
if (mappingNames == null) {
return false;
}
Expand All @@ -101,9 +97,9 @@ boolean hasAnyName(final String mapping, final String rootPath, final String pat
return false;
}

boolean hasAnyName(final Map<PropertyName, Set<PropertyName>> mappings, final String path,
boolean hasAnyName(final Map<PropertyName, List<PropertyName>> mappings, final String path,
final Iterable<String> names) {
Set<PropertyName> mappingNames = mappings.get(name(path));
List<PropertyName> mappingNames = mappings.get(name(path));
if (mappingNames == null || mappingNames.isEmpty()) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -969,4 +969,35 @@ interface NestedOptional {
}
}
}

@Test
void ambiguousMapKeys() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withSources(config(
"ambiguous.map-key.another-nested.name", "name",
"ambiguous.map-key.another-nested.keys", "values"))
.withMapping(AmbiguousMapKeys.class)
.build();

AmbiguousMapKeys mapping = config.getConfigMapping(AmbiguousMapKeys.class);
assertEquals("name", mapping.nested().get("map-key").anotherNested().name());
assertEquals("values", mapping.nested().get("map-key").anotherNested().names().get("keys"));
}

@ConfigMapping(prefix = "ambiguous")
interface AmbiguousMapKeys {
@WithParentName
Map<String, Nested> nested();

interface Nested {
AnotherNested anotherNested();

interface AnotherNested {
String name();

@WithParentName
Map<String, String> names();
}
}
}
}

0 comments on commit c3fcadb

Please sign in to comment.