Compare commits

...

3 Commits

Author SHA1 Message Date
huangsimin
22b97d6194 修改一些数组可能产生bug, get改成范性返回值 2022-07-01 12:01:21 +08:00
huangsimin
1336a89e59 TODO: 修改配置, 更加便利使用 2022-06-30 18:10:14 +08:00
eson
2fc3f7d265 Merge tag 'v1.0.3' into develop
合并1.0.3版本
2022-06-28 14:38:20 +08:00
4 changed files with 167 additions and 43 deletions

View File

@ -91,7 +91,11 @@ cnf.seek("key1", "key2").set("do_set"); // seek先定位keys
#### setWithCreate方法 赋值. 如果key不存在就创建
```java
Config.UseConfig((cnf) -> {
int[] values = new int[]{1,2,3} ; // 数组配置
ArrayList<Object> values = new ArrayList<Object>(); // 数组配置1 2 3
values.add(1);
values.add(2);
values.add(3);
cnf.seek("listKeys1", "listKeys2").setWithCreate( values ); // 如果不存在key创建.
// listKeys1:
// listKeys2:

View File

@ -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>

View File

@ -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,48 @@ 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;
for (var i = 0; i < keys.length - 1; i++) {
public boolean exists(Object... keys) {
Object cur = data;
for (var i = 0; i < keys.length; 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;
}
/**
@ -169,9 +189,9 @@ public class Config {
*/
public class Operator {
Config config;
String[] keys;
Object[] keys;
Operator(Config config, String[] keys) {
Operator(Config config, Object[] keys) {
this.config = config;
this.keys = keys;
}
@ -182,19 +202,34 @@ public class Config {
* @return 返回自身
*/
public Operator createKeys() {
var cur = config.data;
Object cur = config.data;
for (var i = 0;;) {
var key = keys[i];
var vobj = cur.get(key);
if (vobj == null) {
vobj = new LinkedHashMap<>();
cur.put(key, vobj);
Object vobj;
// var vobj = cur.get(key);
if(key.getClass() != String.class ) {
var opcur = (ArrayList<Object>)cur ;
vobj = opcur.get((int)key);
if (vobj == null) {
vobj = new ArrayList<Object>();
opcur.set((int)key, vobj);
}
} else {
var opcur = (Map<String, Object>)cur ;
vobj = opcur.get(key);
if (vobj == null) {
vobj = new LinkedHashMap<>();
opcur.put((String)key, vobj);
}
}
i++;
if (i >= keys.length) {
break;
}
cur = (Map<String, Object>) vobj;
cur = vobj;
}
return this;
}
@ -205,12 +240,28 @@ public class Config {
* @param value 赋值
*/
public void set(Object value) {
var cur = config.data;
Object cur = config.data;
for (var i = 0; i < keys.length - 1; 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);
}
}
var key = keys[keys.length - 1];
if(key.getClass() != String.class ) {
var opcur = (ArrayList<Object>)cur ;
opcur.set((int)key, value);
} else {
var opcur = (Map<String, Object>)cur ;
opcur.put((String)key, value);
}
cur.put(keys[keys.length - 1], value);
}
/**
@ -219,18 +270,48 @@ public class Config {
* @param value 赋值
*/
public void setWithCreate(Object value) {
var cur = config.data;
Object cur = config.data;
for (var i = 0; i < keys.length - 1; i++) {
var key = keys[i];
var curValue = cur.get(key);
if(curValue == null) {
curValue = new LinkedHashMap<>();
cur.put(key, curValue);
if(key.getClass() != String.class ) {
var opcur = (ArrayList<Object>)cur ;
Object vobj = opcur.get((int)key);
if (vobj == null) {
vobj = new ArrayList<Object>();
opcur.set((int)key, vobj);
}
cur = vobj;
} else {
var opcur = (Map<String, Object>)cur ;
Object vobj = opcur.get(key);
if (vobj == null) {
vobj = new LinkedHashMap<>();
opcur.put((String)key, vobj);
}
cur = vobj;
}
cur = (Map<String, Object>)curValue;
}
cur.put(keys[keys.length - 1], value);
var key = keys[keys.length - 1];
if(key.getClass() != String.class ) {
var opcur = (ArrayList<Object>)cur ;
Object vobj = opcur.get((int)key);
if (vobj == null) {
vobj = new ArrayList<Object>();
opcur.set((int)key, vobj);
}
} else {
var opcur = (Map<String, Object>)cur ;
Object vobj = opcur.get(key);
if (vobj == null) {
vobj = new LinkedHashMap<>();
opcur.put((String)key, vobj);
}
}
}
}
@ -251,13 +332,28 @@ public class Config {
* @param keys
* @return
*/
public Object remove(String... keys) {
var cur = data;
public <T> T remove(Object... keys) {
Object cur = data;
for (var i = 0; i < keys.length - 1; 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);
}
}
var key = keys[keys.length - 1];
if(key.getClass() != String.class ) {
var opcur = (ArrayList<Object>)cur;
return (T)opcur.remove((int)key);
} else {
var opcur = (Map<String, Object>)cur ;
return (T)opcur.remove(key);
}
return cur.remove(keys[keys.length - 1]);
}
/**

View File

@ -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;
});
}
@ -94,8 +94,6 @@ public class ConfigTest {
Instant now = Instant.now();
cnf.data.put("use_config", now.toString());
try {
log.info("{}",cnf.update());
} catch (NacosException e) {
@ -158,6 +156,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 +194,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;
});
@ -195,7 +208,11 @@ public class ConfigTest {
void testSetListValues() throws Exception {
Config.UseConfig((cnf) -> {
int[] values = new int[]{1,2,3} ;
ArrayList<Object> values = new ArrayList<Object>();
values.add(1);
values.add(2);
values.add(3);
cnf.seek("listKeys1", "listKeys2").setWithCreate( values ); // 如果不存在key创建.
// listKeys1:
// listKeys2:
@ -218,7 +235,11 @@ public class ConfigTest {
void testMapData() throws Exception {
Config.UseConfig((cnf)->{
int[] values = new int[]{1,2,3} ;
ArrayList<Object> values = new ArrayList<Object>() ;
values.add(1);
values.add(2);
values.add(3);
cnf.seek("listKeys1", "listKeys2").setWithCreate( values ); // 如果不存在key创建.
// listKeys1:
// listKeys2:
@ -234,9 +255,12 @@ public class ConfigTest {
});
Config.UseConfig((cnf) -> {
log.info("{}", cnf.data); // Map<String, Object> data
var key2 = (Map<String, Object>) (cnf.data.get("listKeys1"));
((int[])key2.get("listKeys2"))[1] = 10; // 数组 1 2 3 的2为 10
ArrayList<Object> listKeys2 = (ArrayList<Object>)key2.get("listKeys2");
listKeys2.set(1,10); // 数组 1 2 3 的2为 10
try {
log.info("{}",cnf.update());
} catch (NacosException e) {