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不存在就创建 #### setWithCreate方法 赋值. 如果key不存在就创建
```java ```java
Config.UseConfig((cnf) -> { 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创建. cnf.seek("listKeys1", "listKeys2").setWithCreate( values ); // 如果不存在key创建.
// listKeys1: // listKeys1:
// listKeys2: // listKeys2:

View File

@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.yuandian.common</groupId> <groupId>com.yuandian.common</groupId>
<artifactId>config</artifactId> <artifactId>config</artifactId>
<version>1.0.3-SNAPSHOT</version> <version>1.0.4-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>config</name> <name>config</name>
<url>http://yuandian.com</url> <url>http://yuandian.com</url>

View File

@ -6,6 +6,7 @@
*/ */
package com.yuandian.common; package com.yuandian.common;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
@ -135,29 +136,48 @@ public class Config {
* @param keys 获取的key值 * @param keys 获取的key值
* @return * @return
*/ */
public Object get(String... keys) { public <T> T get(Object... keys) {
var cur = data;
for (var i = 0; i < keys.length - 1; i++) { Object cur = data;
for (var i = 0; i < keys.length; i++) {
var key = keys[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 是否存在 * 判断 keys 是否存在
* *
* @param keys 获取的key值 * @param keys 获取的key值
* @return * @return
*/ */
public boolean exists(String... keys) { public boolean exists(Object... keys) {
var cur = data; 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;
}
var key = keys[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 true;
return cur.get(keys[keys.length-1]) != null;
} }
/** /**
@ -169,9 +189,9 @@ public class Config {
*/ */
public class Operator { public class Operator {
Config config; Config config;
String[] keys; Object[] keys;
Operator(Config config, String[] keys) { Operator(Config config, Object[] keys) {
this.config = config; this.config = config;
this.keys = keys; this.keys = keys;
} }
@ -182,19 +202,34 @@ public class Config {
* @return 返回自身 * @return 返回自身
*/ */
public Operator createKeys() { public Operator createKeys() {
var cur = config.data; Object cur = config.data;
for (var i = 0;;) { for (var i = 0;;) {
var key = keys[i]; var key = keys[i];
var vobj = cur.get(key); Object vobj;
if (vobj == null) { // var vobj = cur.get(key);
vobj = new LinkedHashMap<>();
cur.put(key, vobj); 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++; i++;
if (i >= keys.length) { if (i >= keys.length) {
break; break;
} }
cur = (Map<String, Object>) vobj; cur = vobj;
} }
return this; return this;
} }
@ -205,12 +240,28 @@ public class Config {
* @param value 赋值 * @param value 赋值
*/ */
public void set(Object value) { public void set(Object value) {
var cur = config.data; Object cur = config.data;
for (var i = 0; i < keys.length - 1; i++) { for (var i = 0; i < keys.length - 1; i++) {
var key = keys[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 赋值 * @param value 赋值
*/ */
public void setWithCreate(Object value) { public void setWithCreate(Object value) {
var cur = config.data; Object cur = config.data;
for (var i = 0; i < keys.length - 1; i++) { for (var i = 0; i < keys.length - 1; i++) {
var key = keys[i]; var key = keys[i];
var curValue = cur.get(key);
if(curValue == null) {
curValue = new LinkedHashMap<>(); if(key.getClass() != String.class ) {
cur.put(key, curValue); 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 * @param keys
* @return * @return
*/ */
public Object remove(String... keys) { public <T> T remove(Object... keys) {
var cur = data; Object cur = data;
for (var i = 0; i < keys.length - 1; i++) { for (var i = 0; i < keys.length - 1; i++) {
var key = keys[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: * key2:
* path_value * path_value
*/ */
log.info("{}",cnf.get("key1", "key2")); log.info("{}", (Object)cnf.get("key1", "key2"));
return null; return null;
}); });
} }
@ -94,8 +94,6 @@ public class ConfigTest {
Instant now = Instant.now(); Instant now = Instant.now();
cnf.data.put("use_config", now.toString()); cnf.data.put("use_config", now.toString());
try { try {
log.info("{}",cnf.update()); log.info("{}",cnf.update());
} catch (NacosException e) { } catch (NacosException e) {
@ -158,6 +156,21 @@ public class ConfigTest {
return null; 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 @Test
@Order(1) @Order(1)
@ -181,7 +194,7 @@ public class ConfigTest {
void testLabelConfig() throws Exception { void testLabelConfig() throws Exception {
Config.UseConfig("org.fortest", (cnf)->{ Config.UseConfig("org.fortest", (cnf)->{
log.info("{}", cnf.get("test")); log.info("{}", (Object)cnf.get("test"));
Assertions.assertEquals(cnf.get("test"), "groupAndDataId"); Assertions.assertEquals(cnf.get("test"), "groupAndDataId");
return null; return null;
}); });
@ -195,7 +208,11 @@ public class ConfigTest {
void testSetListValues() throws Exception { void testSetListValues() throws Exception {
Config.UseConfig((cnf) -> { 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创建. cnf.seek("listKeys1", "listKeys2").setWithCreate( values ); // 如果不存在key创建.
// listKeys1: // listKeys1:
// listKeys2: // listKeys2:
@ -218,7 +235,11 @@ public class ConfigTest {
void testMapData() throws Exception { void testMapData() throws Exception {
Config.UseConfig((cnf)->{ 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创建. cnf.seek("listKeys1", "listKeys2").setWithCreate( values ); // 如果不存在key创建.
// listKeys1: // listKeys1:
// listKeys2: // listKeys2:
@ -234,9 +255,12 @@ public class ConfigTest {
}); });
Config.UseConfig((cnf) -> { Config.UseConfig((cnf) -> {
log.info("{}", cnf.data); // Map<String, Object> data log.info("{}", cnf.data); // Map<String, Object> data
var key2 = (Map<String, Object>) (cnf.data.get("listKeys1")); 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 { try {
log.info("{}",cnf.update()); log.info("{}",cnf.update());
} catch (NacosException e) { } catch (NacosException e) {