Skip to content

Commit

Permalink
#495 Add Support For Set Types In Config Beans
Browse files Browse the repository at this point in the history
  • Loading branch information
Karthick Sankarachary committed Sep 16, 2017
1 parent e020acd commit 002de45
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.time.Duration;
import java.util.Set;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigObject;
Expand Down Expand Up @@ -160,6 +162,8 @@ private static Object getValue(Class<?> beanClass, Type parameterType, Class<?>
return config.getAnyRef(configPropName);
} else if (parameterClass == List.class) {
return getListValue(beanClass, parameterType, parameterClass, config, configPropName);
} else if (parameterClass == Set.class) {
return getSetValue(beanClass, parameterType, parameterClass, config, configPropName);
} else if (parameterClass == Map.class) {
// we could do better here, but right now we don't.
Type[] typeArgs = ((ParameterizedType)parameterType).getActualTypeArguments();
Expand All @@ -186,6 +190,10 @@ private static Object getValue(Class<?> beanClass, Type parameterType, Class<?>
}
}

private static Object getSetValue(Class<?> beanClass, Type parameterType, Class<?> parameterClass, Config config, String configPropName) {
return new HashSet((List) getListValue(beanClass, parameterType, parameterClass, config, configPropName));
}

private static Object getListValue(Class<?> beanClass, Type parameterType, Class<?> parameterClass, Config config, String configPropName) {
Type elementType = ((ParameterizedType)parameterType).getActualTypeArguments()[0];

Expand Down
139 changes: 139 additions & 0 deletions config/src/test/java/beanconfig/SetsConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package beanconfig;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigMemorySize;
import com.typesafe.config.ConfigObject;
import com.typesafe.config.ConfigValue;

import java.time.Duration;
import java.util.Set;

public class SetsConfig {

Set<Integer> empty;
Set<Integer> ofInt;
Set<String> ofString;
Set<Double> ofDouble;
Set<Long> ofLong;
Set<Object> ofNull;
Set<Boolean> ofBoolean;
Set<Object> ofObject;
Set<Config> ofConfig;
Set<ConfigObject> ofConfigObject;
Set<ConfigValue> ofConfigValue;
Set<Duration> ofDuration;
Set<ConfigMemorySize> ofMemorySize;
Set<StringsConfig> ofStringBean;

public Set<Integer> getEmpty() {
return empty;
}

public void setEmpty(Set<Integer> empty) {
this.empty = empty;
}

public Set<Integer> getOfInt() {
return ofInt;
}

public void setOfInt(Set<Integer> ofInt) {
this.ofInt = ofInt;
}

public Set<String> getOfString() {
return ofString;
}

public void setOfString(Set<String> ofString) {
this.ofString = ofString;
}

public Set<Double> getOfDouble() {
return ofDouble;
}

public void setOfDouble(Set<Double> ofDouble) {
this.ofDouble = ofDouble;
}

public Set<Object> getOfNull() {
return ofNull;
}

public void setOfNull(Set<Object> ofNull) {
this.ofNull = ofNull;
}

public Set<Boolean> getOfBoolean() {
return ofBoolean;
}

public void setOfBoolean(Set<Boolean> ofBoolean) {
this.ofBoolean = ofBoolean;
}

public Set<Object> getOfObject() {
return ofObject;
}

public void setOfObject(Set<Object> ofObject) {
this.ofObject = ofObject;
}

public Set<Long> getOfLong() {
return ofLong;
}

public void setOfLong(Set<Long> ofLong) {
this.ofLong = ofLong;
}

public Set<Config> getOfConfig() {
return ofConfig;
}

public void setOfConfig(Set<Config> ofConfig) {
this.ofConfig = ofConfig;
}

public Set<ConfigObject> getOfConfigObject() {
return ofConfigObject;
}

public void setOfConfigObject(Set<ConfigObject> ofConfigObject) {
this.ofConfigObject = ofConfigObject;
}

public Set<ConfigValue> getOfConfigValue() {
return ofConfigValue;
}

public void setOfConfigValue(Set<ConfigValue> ofConfigValue) {
this.ofConfigValue = ofConfigValue;
}

public Set<Duration> getOfDuration() {
return ofDuration;
}

public void setOfDuration(Set<Duration> ofDuration) {
this.ofDuration = ofDuration;
}

public Set<ConfigMemorySize> getOfMemorySize() {
return ofMemorySize;
}

public void setOfMemorySize(Set<ConfigMemorySize> ofMemorySize) {
this.ofMemorySize = ofMemorySize;
}

public Set<StringsConfig> getOfStringBean() {
return ofStringBean;
}

public void setOfStringBean(Set<StringsConfig> ofStringBean) {
this.ofStringBean = ofStringBean;
}
}
26 changes: 26 additions & 0 deletions config/src/test/resources/beanconfig/beanconfig01.conf
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,31 @@
"valueObject": {
"mandatoryValue": "notNull"
}
},
"sets" : {
"empty" : [],
"ofInt" : [1, 2, 3, 2, 3],
"ofString" : [ ${strings.a}, ${strings.b}, ${strings.c} ],
"of-double" : [3.14, 4.14, 4.14, 5.14],
"of-long" : { "1" : 32, "2" : 42, "3" : 52 }, // object-to-list conversion
"ofNull" : [null, null, null],
"ofBoolean" : [true, false, false],
"ofArray" : [${arrays.ofString}, ${arrays.ofString}, ${arrays.ofString}],
"ofObject" : [${numbers}, ${booleans}, ${strings}],
"ofConfig" : [${numbers}, ${booleans}, ${strings}],
"ofConfigObject" : [${numbers}, ${booleans}, ${strings}],
"ofConfigValue" : [1, 2, "a"],
"ofDuration" : [1, 2h, 3 days],
"ofMemorySize" : [1024, 1M, 1G],
"ofStringBean" : [
{
abcd : "testAbcdOne"
yes : "testYesOne"
},
{
abcd : "testAbcdTwo"
yes : "testYesTwo"
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,39 @@ class ConfigBeanFactoryTest extends TestUtils {
assertEquals(List(stringsConfigOne, stringsConfigTwo).asJava, beanConfig.getOfStringBean)
}

@Test
def testCreateSet() {
val beanConfig: SetsConfig = ConfigBeanFactory.create(loadConfig().getConfig("sets"), classOf[SetsConfig])
assertNotNull(beanConfig)
assertEquals(Set().asJava, beanConfig.getEmpty)
assertEquals(Set(1, 2, 3).asJava, beanConfig.getOfInt)
assertEquals(Set(32L, 42L, 52L).asJava, beanConfig.getOfLong)
assertEquals(Set("a", "b", "c").asJava, beanConfig.getOfString)
assertEquals(3, beanConfig.getOfObject.size)
assertEquals(3, beanConfig.getOfDouble.size)
assertEquals(3, beanConfig.getOfConfig.size)
assertTrue(beanConfig.getOfConfig.iterator().next().isInstanceOf[Config])
assertEquals(3, beanConfig.getOfConfigObject.size)
assertTrue(beanConfig.getOfConfigObject.iterator().next().isInstanceOf[ConfigObject])
assertEquals(Set(intValue(1), intValue(2), stringValue("a")),
beanConfig.getOfConfigValue.asScala)
assertEquals(Set(Duration.ofMillis(1), Duration.ofHours(2), Duration.ofDays(3)),
beanConfig.getOfDuration.asScala)
assertEquals(Set(ConfigMemorySize.ofBytes(1024),
ConfigMemorySize.ofBytes(1048576),
ConfigMemorySize.ofBytes(1073741824)),
beanConfig.getOfMemorySize.asScala)

val stringsConfigOne = new StringsConfig();
stringsConfigOne.setAbcd("testAbcdOne")
stringsConfigOne.setYes("testYesOne")
val stringsConfigTwo = new StringsConfig();
stringsConfigTwo.setAbcd("testAbcdTwo")
stringsConfigTwo.setYes("testYesTwo")

assertEquals(Set(stringsConfigOne, stringsConfigTwo).asJava, beanConfig.getOfStringBean)
}

@Test
def testCreateDuration() {
val beanConfig: DurationsConfig = ConfigBeanFactory.create(loadConfig().getConfig("durations"), classOf[DurationsConfig])
Expand Down

0 comments on commit 002de45

Please sign in to comment.