AnyValue增加toProperties方法

This commit is contained in:
redkale
2024-08-14 22:11:30 +08:00
parent 0c9576603d
commit d3580668b6
2 changed files with 62 additions and 1 deletions

View File

@@ -1166,6 +1166,35 @@ public abstract class AnyValue {
}
public Properties toProperties() {
throw new UnsupportedOperationException("Not supported yet.");
Properties props = new Properties();
toProperties(props, new HashMap<>(), "", this);
return props;
}
private static void toProperties(Properties props, Map<String, Integer> keyPrefixs, String parent, AnyValue conf) {
conf.forEach(
(k, v) -> {
final String prefix = parent + k;
if (keyPrefixs.containsKey(prefix)) {
String key = prefix;
int index = keyPrefixs.get(key);
if (index == -1) {
key = parent + k + "[" + (++index) + "]";
props.put(key, props.remove(prefix));
}
key = parent + k + "[" + (++index) + "]";
while (props.containsKey(key)) {
key = parent + k + "[" + (++index) + "]";
}
props.put(key, v);
keyPrefixs.put(prefix, index);
} else {
props.put(prefix, v);
keyPrefixs.put(prefix, -1);
}
},
(k, c) -> {
toProperties(props, keyPrefixs, parent + k + ".", c);
});
}
}

View File

@@ -15,7 +15,11 @@ public class AnyValuePropertiesTest {
public static void main(String[] args) throws Throwable {
AnyValuePropertiesTest test = new AnyValuePropertiesTest();
test.run1();
test.run2();
test.run3();
test.run4();
test.run5();
}
@Test
@@ -84,6 +88,7 @@ public class AnyValuePropertiesTest {
+ " }\r\n"
+ "}";
Assertions.assertEquals(result, AnyValue.loadFromProperties(properties).toString());
System.out.println("------------------------ 01 ------------------------");
}
@Test
@@ -111,6 +116,7 @@ public class AnyValuePropertiesTest {
// System.out.println(conf.copy().merge(conf2));
// System.out.println(conf);
System.out.println("------------------------ 02 ------------------------");
}
@Test
@@ -125,6 +131,7 @@ public class AnyValuePropertiesTest {
.addValue("desc", "nothing !!!"));
String json = "{\"name\":\"haha\",\"value\":{\"id\":\"1234\",\"key\":null,\"desc\":\"nothing !!!\"}}";
Assertions.assertEquals(json, conf.toJsonString());
System.out.println("------------------------ 03 ------------------------");
}
@Test
@@ -139,5 +146,30 @@ public class AnyValuePropertiesTest {
AnyValue conf = AnyValue.loadFromProperties(prop);
System.out.println(conf);
System.out.println("------------------------ 04 ------------------------");
}
@Test
public void run5() {
AnyValueWriter conf = AnyValue.create();
conf.addValue("name", "haha");
conf.addValue("name", "hehe");
conf.addValue("status", 45);
conf.addValue("name", AnyValueWriter.create("id", 123).addValue("desc", "test"));
conf.addValue("nodes", AnyValueWriter.create("time", 123).addValue("time", 456));
Properties props1 = conf.toProperties();
props1.forEach((k, v) -> System.out.println(k + " = " + v));
Properties props2 = new Properties();
props2.put("name[0]", "haha");
props2.put("name[1]", "hehe");
props2.put("status", "45");
props2.put("name.desc", "test");
props2.put("name.id", "123");
props2.put("nodes.time[0]", "123");
props2.put("nodes.time[1]", "456");
Assertions.assertEquals(props1.toString(), props2.toString());
System.out.println("------------------------ 05 ------------------------");
}
}