From 42e5260ba554112fdd052f0c66db391ab3cb987a Mon Sep 17 00:00:00 2001 From: Linary Date: Sun, 7 Mar 2021 16:42:38 +0800 Subject: [PATCH] Add Class.class as an accept data type in TypedOption (#65) --- pom.xml | 4 +-- .../config/ConfigListConvOption.java | 2 +- .../hugegraph/config/ConfigListOption.java | 2 +- .../baidu/hugegraph/config/HugeConfig.java | 2 +- .../baidu/hugegraph/config/TypedOption.java | 14 +++++++-- .../hugegraph/version/CommonVersion.java | 2 +- .../hugegraph/unit/config/HugeConfigTest.java | 31 +++++++++++++++++++ .../com/baidu/hugegraph/unit/config/test.conf | 2 ++ 8 files changed, 50 insertions(+), 9 deletions(-) diff --git a/pom.xml b/pom.xml index de8e289b1..820517ecf 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.baidu.hugegraph hugegraph-common - 1.8.4 + 1.8.5 hugegraph-common https://github.com/hugegraph/hugegraph-common @@ -266,7 +266,7 @@ - 1.8.4.0 + 1.8.5.0 diff --git a/src/main/java/com/baidu/hugegraph/config/ConfigListConvOption.java b/src/main/java/com/baidu/hugegraph/config/ConfigListConvOption.java index 8162500c7..151fbf92c 100644 --- a/src/main/java/com/baidu/hugegraph/config/ConfigListConvOption.java +++ b/src/main/java/com/baidu/hugegraph/config/ConfigListConvOption.java @@ -60,7 +60,7 @@ protected boolean forList() { } @Override - protected List parse(Object value) { + protected List parse(String value) { return ConfigListOption.convert(value, part -> { return this.parse(part, this.elemClass); }); diff --git a/src/main/java/com/baidu/hugegraph/config/ConfigListOption.java b/src/main/java/com/baidu/hugegraph/config/ConfigListOption.java index 565e9c71b..ef6e3249e 100644 --- a/src/main/java/com/baidu/hugegraph/config/ConfigListOption.java +++ b/src/main/java/com/baidu/hugegraph/config/ConfigListOption.java @@ -56,7 +56,7 @@ protected boolean forList() { } @Override - protected List parse(Object value) { + protected List parse(String value) { return convert(value, part -> this.parse(part, this.elemClass)); } diff --git a/src/main/java/com/baidu/hugegraph/config/HugeConfig.java b/src/main/java/com/baidu/hugegraph/config/HugeConfig.java index 5a8d345c9..ed5da5a51 100644 --- a/src/main/java/com/baidu/hugegraph/config/HugeConfig.java +++ b/src/main/java/com/baidu/hugegraph/config/HugeConfig.java @@ -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() { diff --git a/src/main/java/com/baidu/hugegraph/config/TypedOption.java b/src/main/java/com/baidu/hugegraph/config/TypedOption.java index 57d77c6fa..2aa50e3f7 100644 --- a/src/main/java/com/baidu/hugegraph/config/TypedOption.java +++ b/src/main/java/com/baidu/hugegraph/config/TypedOption.java @@ -50,6 +50,7 @@ public class TypedOption { Double.class, String.class, String[].class, + Class.class, List.class ); @@ -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", diff --git a/src/main/java/com/baidu/hugegraph/version/CommonVersion.java b/src/main/java/com/baidu/hugegraph/version/CommonVersion.java index ad6cd2cb6..34c07fbeb 100644 --- a/src/main/java/com/baidu/hugegraph/version/CommonVersion.java +++ b/src/main/java/com/baidu/hugegraph/version/CommonVersion.java @@ -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"); } diff --git a/src/test/java/com/baidu/hugegraph/unit/config/HugeConfigTest.java b/src/test/java/com/baidu/hugegraph/unit/config/HugeConfigTest.java index 9744a32cb..e6c8ac8be 100644 --- a/src/test/java/com/baidu/hugegraph/unit/config/HugeConfigTest.java +++ b/src/test/java/com/baidu/hugegraph/unit/config/HugeConfigTest.java @@ -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()); @@ -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]", @@ -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", @@ -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)); @@ -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)); @@ -477,6 +500,14 @@ public static synchronized TestOptions instance() { true ); + public static final ConfigOption> clazz = + new ConfigOption<>( + "group1.class", + "description of group1.class", + disallowEmpty(), + Object.class + ); + public static final ConfigConvOption weekday = new ConfigConvOption<>( "group1.weekday", diff --git a/src/test/java/com/baidu/hugegraph/unit/config/test.conf b/src/test/java/com/baidu/hugegraph/unit/config/test.conf index bd2ea2a85..9ee10b0a3 100644 --- a/src/test/java/com/baidu/hugegraph/unit/config/test.conf +++ b/src/test/java/com/baidu/hugegraph/unit/config/test.conf @@ -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]