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

Add Class.class as an accept data type in TypedOption #65

Merged
merged 3 commits into from
Mar 7, 2021
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
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