Skip to content

Commit

Permalink
Add Class.class as an accept data type in TypedOption (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
Linary authored Mar 7, 2021
1 parent ccc4d3b commit 42e5260
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 9 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.baidu.hugegraph</groupId>
<artifactId>hugegraph-common</artifactId>
<version>1.8.4</version>
<version>1.8.5</version>

<name>hugegraph-common</name>
<url>https://github.com/hugegraph/hugegraph-common</url>
Expand Down Expand Up @@ -266,7 +266,7 @@
<manifestEntries>
<!-- Must be on one line, otherwise the automatic
upgrade script cannot replace the version number -->
<Implementation-Version>1.8.4.0</Implementation-Version>
<Implementation-Version>1.8.5.0</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected boolean forList() {
}

@Override
protected List<T> parse(Object value) {
protected List<T> parse(String value) {
return ConfigListOption.convert(value, part -> {
return this.parse(part, this.elemClass);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected boolean forList() {
}

@Override
protected List<T> parse(Object value) {
protected List<T> parse(String value) {
return convert(value, part -> this.parse(part, this.elemClass));
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/baidu/hugegraph/config/HugeConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private Object validateOption(String key, Object value) {
"Invalid value for key '%s': %s", key, value);

TypedOption<?, ?> option = OptionSpace.get(key);
return option.parseConvert(value);
return option.parseConvert((String) value);
}

private void checkRequiredOptions() {
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/com/baidu/hugegraph/config/TypedOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class TypedOption<T, R> {
Double.class,
String.class,
String[].class,
Class.class,
List.class
);

Expand Down Expand Up @@ -112,20 +113,27 @@ public R defaultValue() {
return this.convert(this.defaultValue);
}

public R parseConvert(Object value) {
public R parseConvert(String value) {
T parsed = this.parse(value);
this.check(parsed);
return this.convert(parsed);
}

@SuppressWarnings("unchecked")
protected T parse(Object value) {
protected T parse(String value) {
return (T) this.parse(value, this.dataType);
}

protected Object parse(Object value, Class<?> dataType) {
protected Object parse(String value, Class<?> dataType) {
if (dataType.equals(String.class)) {
return value;
} else if (dataType.equals(Class.class)) {
try {
return Class.forName(value);
} catch (ClassNotFoundException e) {
throw new ConfigException(
"Failed to parse Class from String '%s'", e, value);
}
} else if (List.class.isAssignableFrom(dataType)) {
E.checkState(this.forList(),
"List option can't be registered with class %s",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ public class CommonVersion {

// The second parameter of Version.of() is for all-in-one JAR
public static final Version VERSION = Version.of(CommonVersion.class,
"1.8.4");
"1.8.5");
}
31 changes: 31 additions & 0 deletions src/test/java/com/baidu/hugegraph/unit/config/HugeConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public void testOptionDataType() {
Assert.assertEquals(Double.class, TestOptions.double1.dataType());
Assert.assertEquals(Boolean.class, TestOptions.bool.dataType());

Assert.assertEquals(Class.class, TestOptions.clazz.dataType());

Assert.assertEquals(List.class, TestOptions.list.dataType());
Assert.assertEquals(List.class, TestOptions.map.dataType());

Expand Down Expand Up @@ -110,6 +112,8 @@ public void testOptionsToString() {
TestOptions.double1.toString());
Assert.assertEquals("[Boolean]group1.bool=true",
TestOptions.bool.toString());
Assert.assertEquals("[Class]group1.class=class java.lang.Object",
TestOptions.clazz.toString());
Assert.assertEquals("[List]group1.list=[list-value1, list-value2]",
TestOptions.list.toString());
Assert.assertEquals("[List]group1.map=[key1:value1, key2:value2]",
Expand Down Expand Up @@ -208,6 +212,15 @@ public void testOptionWithError() {
);
});

Assert.assertThrows(ConfigException.class, () -> {
new ConfigOption<>(
"group1.class",
"description of group1.class",
input -> input != null && input.equals(Long.class),
Integer.class
);
});

Assert.assertThrows(ConfigException.class, () -> {
new ConfigListOption<>(
"group1.list",
Expand Down Expand Up @@ -265,6 +278,14 @@ public void testHugeConfig() throws Exception {

Assert.assertEquals(true, config.get(TestOptions.bool));

Assert.assertEquals(Object.class, config.get(TestOptions.clazz));
Assert.assertThrows(ConfigException.class, () -> {
config.setProperty(TestOptions.clazz.name(),
"com.baidu.hugegraph.HugeGraph");
}, e -> {
Assert.assertTrue(e.getCause() instanceof ClassNotFoundException);
});

Assert.assertEquals(Arrays.asList("list-value1", "list-value2"),
config.get(TestOptions.list));

Expand Down Expand Up @@ -299,6 +320,8 @@ public void testHugeConfigWithFile() throws Exception {

Assert.assertEquals(false, config.get(TestOptions.bool));

Assert.assertEquals(String.class, config.get(TestOptions.clazz));

Assert.assertEquals(Arrays.asList("file-v1", "file-v2", "file-v3"),
config.get(TestOptions.list));

Expand Down Expand Up @@ -477,6 +500,14 @@ public static synchronized TestOptions instance() {
true
);

public static final ConfigOption<Class<?>> clazz =
new ConfigOption<>(
"group1.class",
"description of group1.class",
disallowEmpty(),
Object.class
);

public static final ConfigConvOption<String, WeekDay> weekday =
new ConfigConvOption<>(
"group1.weekday",
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/com/baidu/hugegraph/unit/config/test.conf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ group1.double1=66

group1.bool=false

group1.class=java.lang.String

group1.list=[file-v1, file-v2, file-v3]
group1.map=[key1:value1, key3:value3]

Expand Down

0 comments on commit 42e5260

Please sign in to comment.