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

Remove deprecated ConfigMappings#mappedProperties #1260

Merged
merged 1 commit into from
Dec 3, 2024
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 @@ -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
Loading