TODO: 修改配置, 更加便利使用
This commit is contained in:
parent
2fc3f7d265
commit
1336a89e59
2
pom.xml
2
pom.xml
|
@ -2,7 +2,7 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.yuandian.common</groupId>
|
||||
<artifactId>config</artifactId>
|
||||
<version>1.0.3-SNAPSHOT</version>
|
||||
<version>1.0.4-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>config</name>
|
||||
<url>http://yuandian.com</url>
|
||||
|
|
|
@ -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> T get(Object... keys) {
|
||||
|
||||
Object cur = data;
|
||||
|
||||
for (var i = 0; i < keys.length; i++) {
|
||||
var key = keys[i];
|
||||
cur = (Map<String, Object>) cur.get(key);
|
||||
|
||||
if(key.getClass() != String.class ) {
|
||||
var opcur = (ArrayList<Object>)cur ;
|
||||
cur = opcur.get((int)key);
|
||||
} else {
|
||||
var opcur = (Map<String, Object>)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<String, Object>) cur.get(key);
|
||||
if(key.getClass() != String.class ) {
|
||||
var opcur = (ArrayList<Object>)cur ;
|
||||
cur = opcur.get((int)key);
|
||||
} else {
|
||||
var opcur = (Map<String, Object>)cur ;
|
||||
cur = opcur.get(key);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return cur.get(keys[keys.length-1]) != null;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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;
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue
Block a user