修改一些数组可能产生bug, get改成范性返回值

This commit is contained in:
huangsimin 2022-07-01 12:01:21 +08:00
parent 1336a89e59
commit 22b97d6194
3 changed files with 119 additions and 32 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

@ -164,7 +164,7 @@ public class Config {
*/
public boolean exists(Object... keys) {
Object cur = data;
for (var i = 0; i < keys.length - 1; i++) {
for (var i = 0; i < keys.length; i++) {
if(cur == null) {
return false;
}
@ -176,9 +176,7 @@ public class Config {
var opcur = (Map<String, Object>)cur ;
cur = opcur.get(key);
}
}
return true;
}
@ -191,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;
}
@ -204,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;
}
@ -227,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);
}
/**
@ -241,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);
}
}
}
}
@ -273,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

@ -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) {
@ -210,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:
@ -233,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:
@ -249,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) {