diff --git a/pom.xml b/pom.xml index 0ee9de5..00056c5 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 com.yuandian.common config - 1.0.3-SNAPSHOT + 1.0.4-SNAPSHOT jar config http://yuandian.com diff --git a/src/main/java/com/yuandian/common/Config.java b/src/main/java/com/yuandian/common/Config.java index c96fa95..18d47fb 100644 --- a/src/main/java/com/yuandian/common/Config.java +++ b/src/main/java/com/yuandian/common/Config.java @@ -6,6 +6,7 @@ */ package com.yuandian.common; +import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; @@ -135,29 +136,50 @@ public class Config { * @param keys 获取的key值 * @return */ - public Object get(String... keys) { - var cur = data; - for (var i = 0; i < keys.length - 1; i++) { + public T get(Object... keys) { + + Object cur = data; + + for (var i = 0; i < keys.length; i++) { var key = keys[i]; - cur = (Map) cur.get(key); + + if(key.getClass() != String.class ) { + var opcur = (ArrayList)cur ; + cur = opcur.get((int)key); + } else { + var opcur = (Map)cur ; + cur = opcur.get(key); + } } - return cur.get(keys[keys.length - 1]); + return (T)cur; + // return (T)cur.get(keys[keys.length - 1]); } + /** * 判断 keys 是否存在 * * @param keys 获取的key值 * @return */ - public boolean exists(String... keys) { - var cur = data; + public boolean exists(Object... keys) { + Object cur = data; for (var i = 0; i < keys.length - 1; i++) { + if(cur == null) { + return false; + } var key = keys[i]; - cur = (Map) cur.get(key); + if(key.getClass() != String.class ) { + var opcur = (ArrayList)cur ; + cur = opcur.get((int)key); + } else { + var opcur = (Map)cur ; + cur = opcur.get(key); + } + } - return cur.get(keys[keys.length-1]) != null; + return true; } /** diff --git a/src/test/java/com/yuandian/common/ConfigTest.java b/src/test/java/com/yuandian/common/ConfigTest.java index c00a006..63f44a1 100644 --- a/src/test/java/com/yuandian/common/ConfigTest.java +++ b/src/test/java/com/yuandian/common/ConfigTest.java @@ -35,7 +35,7 @@ public class ConfigTest { * key2: * path_value */ - log.info("{}",cnf.get("key1", "key2")); + log.info("{}", (Object)cnf.get("key1", "key2")); return null; }); } @@ -158,6 +158,21 @@ public class ConfigTest { return null; }); } + + @Test + @Order(5) + void testConfigGetArray() throws Exception { + Config.UseConfig((cnf) -> { + /** + * nacos config 默认 group=yuandian dataId=dataflow + * key1: + * key2: + * path_value + */ + log.info("{}", (Object)cnf.get("listKeys1", "listKeys2", 1)); + return null; + }); + } @Test @Order(1) @@ -181,7 +196,7 @@ public class ConfigTest { void testLabelConfig() throws Exception { Config.UseConfig("org.fortest", (cnf)->{ - log.info("{}", cnf.get("test")); + log.info("{}", (Object)cnf.get("test")); Assertions.assertEquals(cnf.get("test"), "groupAndDataId"); return null; });