Skip to content

Commit

Permalink
Move ConfigMapping classes to integrate with SmallRyeConfig.
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Jun 16, 2020
1 parent aa90434 commit c50ad1a
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 0 deletions.
5 changes: 5 additions & 0 deletions implementation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@
<artifactId>jboss-logging-processor</artifactId>
</dependency>

<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@

import io.smallrye.common.annotation.Experimental;
import io.smallrye.config.SmallRyeConfigBuilder.InterceptorWithPriority;
import io.smallrye.config.mapper.ConfigMapping;
import io.smallrye.config.mapper.ConfigurationValidationException;

/**
* @author <a href="http://jmesnil.net/">Jeff Mesnil</a> (c) 2017 Red Hat inc.
Expand Down Expand Up @@ -200,6 +202,26 @@ public <T, C extends Collection<T>> Optional<C> getOptionalValues(String name, C
return getOptionalValue(name, Converters.newCollectionConverter(converter, collectionFactory));
}

@Experimental("TODO")
public <T> T getConfigProperties(Class<T> klass) {
return getConfigProperties(klass, "");
}

@Experimental("TODO")
public <T> T getConfigProperties(Class<T> klass, String prefix) {
final ConfigMapping configMapping = ConfigMapping.builder()
.addRoot(prefix, klass)
.build();

try {
final ConfigMapping.Result result = configMapping.mapConfiguration(this);
return result.getConfigRoot(prefix, klass);
} catch (ConfigurationValidationException e) {
e.printStackTrace();
throw new IllegalArgumentException(e.getCause());
}
}

@Override
public Iterable<String> getPropertyNames() {
final HashSet<String> names = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.smallrye.config.mapper;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

import io.smallrye.config.KeyValuesConfigSource;
import io.smallrye.config.SmallRyeConfig;
import io.smallrye.config.SmallRyeConfigBuilder;

public class ConfigMappingTest {
@Test
void configMapping() {
final SmallRyeConfig config = buildConfig("server.host", "localhost", "server.port", "8080");
final ConfigsInterface configProperties = config.getConfigProperties(ConfigsInterface.class, "server");
//assertEquals("localhost", configProperties.getHost());
assertEquals("localhost", configProperties.host());
//assertEquals(8080, configProperties.getPort());
assertEquals(8080, configProperties.port());
}

interface ConfigsInterface {
String host();

int port();
}

private static SmallRyeConfig buildConfig(String... keyValues) {
return new SmallRyeConfigBuilder()
.addDefaultSources()
.addDefaultInterceptors()
.withSources(KeyValuesConfigSource.config(keyValues))
.build();
}
}

0 comments on commit c50ad1a

Please sign in to comment.