Skip to content

Commit

Permalink
Remove deprecated ConfigMappings#mappedProperties (#1260)
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez authored Dec 3, 2024
1 parent 40f5c29 commit d5a6317
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static io.smallrye.config.ConfigMappings.ConfigClass.configClass;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -75,20 +74,6 @@ public static Map<String, Property> getProperties(final ConfigClass configClass)
return properties;
}

@Deprecated
public static Set<String> mappedProperties(final ConfigClass configClass, final Set<String> properties) {
ConfigMappingNames names = new ConfigMappingNames(
ConfigMappingLoader.getConfigMapping(configClass.getKlass()).getNames());
Set<String> mappedNames = new HashSet<>();
for (String property : properties) {
if (names.hasAnyName(configClass.getKlass().getName(), configClass.getPrefix(), configClass.getPrefix(),
Set.of(property))) {
mappedNames.add(property);
}
}
return mappedNames;
}

private static void mapConfiguration(
final SmallRyeConfig config,
final SmallRyeConfigBuilder configBuilder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static io.smallrye.config.ConfigMappings.registerConfigProperties;
import static io.smallrye.config.ConfigMappings.ConfigClass.configClass;
import static io.smallrye.config.KeyValuesConfigSource.config;
import static java.util.Collections.emptySet;
import static java.util.Collections.singleton;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand All @@ -15,7 +14,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import org.eclipse.microprofile.config.inject.ConfigProperties;
import org.eclipse.microprofile.config.spi.Converter;
Expand Down Expand Up @@ -326,22 +324,4 @@ void properties() {
assertTrue(properties.containsKey("mapped.value"));
assertTrue(properties.containsKey("mapped.collection[*].value"));
}

@Test
void mappedProperties() {
Set<String> mappedProperties = ConfigMappings.mappedProperties(configClass(MappedProperties.class),
Set.of("mapped.value", "mapped.nested.value", "mapped.collection[0].value", "mapped.unknown"));
assertEquals(3, mappedProperties.size());
assertTrue(mappedProperties.contains("mapped.value"));
assertTrue(mappedProperties.contains("mapped.nested.value"));
assertTrue(mappedProperties.contains("mapped.collection[0].value"));

assertTrue(ConfigMappings.mappedProperties(configClass(MappedProperties.class), emptySet()).isEmpty());
}

@Test
void invalidMappedProperties() {
assertTrue(ConfigMappings.mappedProperties(configClass(MappedProperties.class),
Set.of("foo.bar", "mapped", "mapped.something", "mapped.collection")).isEmpty());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.smallrye.config;

import static io.smallrye.config.ConfigMappings.mappedProperties;
import static io.smallrye.config.ConfigMappings.ConfigClass.configClass;
import static io.smallrye.config.KeyValuesConfigSource.config;
import static io.smallrye.config.SmallRyeConfig.SMALLRYE_CONFIG_PROFILE;
import static java.util.Collections.singletonMap;
Expand All @@ -15,7 +13,6 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -201,22 +198,22 @@ void fallbackMaps() {
if (name.startsWith("child.")) {
return "parent." + name.substring(6);
}
if (name.startsWith("parent.")) {
return "child." + name.substring(7);
}
return name;
});

// To rewrite the fallback names to the main name. Required for fallbacks to work properly with Maps keys
RelocateConfigSourceInterceptor relocateConfigSourceInterceptor = new RelocateConfigSourceInterceptor(name -> {
if (name.startsWith("parent.")) {
return "child." + name.substring(7);
}
return name;
}) {
@Override
public Iterator<String> iterateNames(final ConfigSourceInterceptorContext context) {
Set<String> names = new HashSet<>();
Set<String> hierarchyCandidates = new HashSet<>();
Iterator<String> namesIterator = context.iterateNames();
while (namesIterator.hasNext()) {
String name = namesIterator.next();
names.add(name);
if (name.startsWith("parent.")) {
hierarchyCandidates.add("child." + name.substring(7));
}
}
names.addAll(mappedProperties(configClass(Child.class), hierarchyCandidates));
return names.iterator();
public ConfigValue getValue(final ConfigSourceInterceptorContext context, final String name) {
return context.proceed(name);
}
};

Expand All @@ -227,8 +224,11 @@ public Iterator<String> iterateNames(final ConfigSourceInterceptorContext contex
"parent.labels.parent", "parent",
"parent.nested.parent.value", "parent-nested"))
.withInterceptors(fallbackConfigSourceInterceptor)
.withInterceptors(relocateConfigSourceInterceptor)
.withMapping(Parent.class)
.withMapping(Child.class)
.withMappingIgnore("parent.**")
.withMappingIgnore("child.**")
.build();

Parent parent = config.getConfigMapping(Parent.class);
Expand All @@ -248,20 +248,23 @@ public Iterator<String> iterateNames(final ConfigSourceInterceptorContext contex
"parent.labels.parent", "parent", "child.labels.child", "child",
"parent.nested.parent.value", "parent-nested", "child.nested.child.value", "child-nested"))
.withInterceptors(fallbackConfigSourceInterceptor)
.withInterceptors(relocateConfigSourceInterceptor)
.withMapping(Parent.class)
.withMapping(Child.class)
.withMappingIgnore("parent.**")
.withMappingIgnore("child.**")
.build();

parent = config.getConfigMapping(Parent.class);
child = config.getConfigMapping(Child.class);

assertEquals("parent", parent.value().orElse(null));
assertEquals("child", child.value().orElse(null));
assertEquals(1, parent.labels().size());
assertEquals(2, parent.labels().size());
assertEquals("parent", parent.labels().get("parent"));
assertEquals(2, child.labels().size());
assertEquals("child", child.labels().get("child"));
assertEquals(1, parent.nested().size());
assertEquals(2, parent.nested().size());
assertEquals("parent-nested", parent.nested().get("parent").value());
assertEquals(2, child.nested().size());
assertEquals("child-nested", child.nested().get("child").value());
Expand Down Expand Up @@ -326,7 +329,14 @@ void relocateMapping() {
assertEquals("old", mapping.map().get("old"));

Set<String> properties = stream(config.getPropertyNames().spliterator(), false).collect(toSet());
Set<String> mappedProperties = mappedProperties(configClass(RelocateMapping.class), properties);
ConfigMappingNames names = new ConfigMappingNames(
ConfigMappingLoader.getConfigMapping(RelocateMapping.class).getNames());
Set<String> mappedProperties = new HashSet<>();
for (String property : properties) {
if (names.hasAnyName(RelocateMapping.class.getName(), "reloc.old", "reloc.old", Set.of(property))) {
mappedProperties.add(property);
}
}
properties.removeAll(mappedProperties);
Set<String> relocateProperties = new HashSet<>();
for (String property : properties) {
Expand Down Expand Up @@ -362,7 +372,14 @@ void fallbackMapping() {
assertEquals("old", mapping.map().get("old"));

Set<String> properties = stream(config.getPropertyNames().spliterator(), false).collect(toSet());
Set<String> mappedProperties = mappedProperties(configClass(FallbackMapping.class), properties);
ConfigMappingNames names = new ConfigMappingNames(
ConfigMappingLoader.getConfigMapping(FallbackMapping.class).getNames());
Set<String> mappedProperties = new HashSet<>();
for (String property : properties) {
if (names.hasAnyName(FallbackMapping.class.getName(), "fall.new", "fall.new", Set.of(property))) {
mappedProperties.add(property);
}
}
properties.removeAll(mappedProperties);
Set<String> fallbackProperties = new HashSet<>();
for (String property : properties) {
Expand Down

0 comments on commit d5a6317

Please sign in to comment.