You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by mm...@apache.org on 2019/06/05 05:52:58 UTC
[pulsar] branch master updated: [pulsar-common] support map in
config parser (#4418)
This is an automated email from the ASF dual-hosted git repository.
mmerli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git
The following commit(s) were added to refs/heads/master by this push:
new e716273 [pulsar-common] support map in config parser (#4418)
e716273 is described below
commit e7162732a9806f6702ca0610ffead7e8cedc958f
Author: Rajan Dhabalia <rd...@apache.org>
AuthorDate: Tue Jun 4 22:52:53 2019 -0700
[pulsar-common] support map in config parser (#4418)
* [pulsar-common] support map in config parser
* fix error
---
.../org/apache/pulsar/common/util/FieldParser.java | 16 ++++++
.../apache/pulsar/common/util/FieldParserTest.java | 59 ++++++++++++++++++++++
2 files changed, 75 insertions(+)
diff --git a/pulsar-common/src/main/java/org/apache/pulsar/common/util/FieldParser.java b/pulsar-common/src/main/java/org/apache/pulsar/common/util/FieldParser.java
index d81cd75..b8660b5 100644
--- a/pulsar-common/src/main/java/org/apache/pulsar/common/util/FieldParser.java
+++ b/pulsar-common/src/main/java/org/apache/pulsar/common/util/FieldParser.java
@@ -19,6 +19,7 @@
package org.apache.pulsar.common.util;
import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Preconditions.checkArgument;
import static java.lang.String.format;
import java.lang.reflect.Field;
@@ -175,6 +176,9 @@ public final class FieldParser {
} // convert to set
else if (field.getType().equals(Set.class)) {
return stringToSet(strValue, clazz);
+ } else if (field.getType().equals(Map.class)) {
+ Class<?> valueClass = (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[1];
+ return stringToMap(strValue, clazz, valueClass);
} else if (field.getType().equals(Optional.class)) {
Type typeClazz = ((ParameterizedType) fieldType).getActualTypeArguments()[0];
if (typeClazz instanceof ParameterizedType) {
@@ -333,6 +337,18 @@ public final class FieldParser {
}).collect(Collectors.toSet());
}
+ private static <K, V> Map<K, V> stringToMap(String strValue, Class<K> keyType, Class<V> valueType) {
+ String[] tokens = trim(strValue).split(",");
+ Map<K, V> map = new HashMap<>();
+ for (String token : tokens) {
+ String[] keyValue = trim(token).split("=");
+ checkArgument(keyValue.length == 2,
+ strValue + " map-value is not in correct format key1=value,key2=value2");
+ map.put(convert(keyValue[0], keyType), convert(keyValue[1], valueType));
+ }
+ return map;
+ }
+
private static String trim(String val) {
checkNotNull(val);
return val.trim();
diff --git a/pulsar-common/src/test/java/org/apache/pulsar/common/util/FieldParserTest.java b/pulsar-common/src/test/java/org/apache/pulsar/common/util/FieldParserTest.java
new file mode 100644
index 0000000..a085311
--- /dev/null
+++ b/pulsar-common/src/test/java/org/apache/pulsar/common/util/FieldParserTest.java
@@ -0,0 +1,59 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.common.util;
+
+import static org.testng.Assert.assertEquals;
+
+import java.util.Map;
+
+import org.testng.annotations.Test;
+
+import com.google.common.collect.Maps;
+
+public class FieldParserTest {
+
+ @Test
+ public void testMap() {
+ Map<String, String> properties = Maps.newHashMap();
+ properties.put("name", "config");
+ properties.put("stringStringMap", "key1=value1,key2=value2");
+ properties.put("stringIntMap", "key1=1,key2=2");
+ properties.put("longStringMap", "1=value1,2=value2");
+ MyConfig config = new MyConfig();
+ FieldParser.update(properties, config);
+ assertEquals(config.name, "config");
+ assertEquals(config.stringStringMap.get("key1"), "value1");
+ assertEquals(config.stringStringMap.get("key2"), "value2");
+
+ assertEquals((int) config.stringIntMap.get("key1"), 1);
+ assertEquals((int) config.stringIntMap.get("key2"), 2);
+
+ assertEquals(config.longStringMap.get(1L), "value1");
+ assertEquals(config.longStringMap.get(2L), "value2");
+
+ }
+
+ public static class MyConfig {
+ public String name;
+ public Map<String, String> stringStringMap;
+ public Map<String, Integer> stringIntMap;
+ public Map<Long, String> longStringMap;
+ }
+
+}