Reproduce优化

This commit is contained in:
redkale
2023-08-06 00:27:14 +08:00
parent d228228eee
commit 906c147ed6
6 changed files with 218 additions and 58 deletions

View File

@@ -0,0 +1,50 @@
/*
*
*/
package org.redkale.test.util;
import java.util.Map;
import org.redkale.util.Reproduce;
/**
*
* @author zhangjx
*/
public class ReproduceBeanMap implements Reproduce<Map<String, Object>, TestInterface> {
@Override
public Map apply(Map<String, Object> dest, TestInterface src) {
Object v;
dest.put("id", src.getId());
v = src.getMap();
if (v != null) {
dest.put("map", v);
}
return dest;
}
public Map run(Map<String, Object> dest, TestBean src) {
Object v;
v = src.getName();
if (v != null) {
dest.put("name", v);
}
v = src.time;
if (v != null) {
dest.put("time", v);
}
dest.put("id", src.getId());
v = src.getName();
if (v != null) {
dest.put("name", v);
}
//
return dest;
}
}

View File

@@ -0,0 +1,31 @@
/*
*
*/
package org.redkale.test.util;
import java.util.*;
import org.redkale.util.*;
/**
* @author zhangjx
*/
public class ReproduceMapBean implements Reproduce<TestBean, HashMap> {
@Override
public TestBean apply(TestBean dest, HashMap src) {
src.forEach((k, v) -> {
if ("id".equals(k) && v != null) {
dest.setId(Utility.convertValue(int.class, v));
} else if ("map".equals(k)) {
dest.setMap(Utility.convertValue(Map.class, v));
} else if ("map2".equals(k) && v != null) {
dest.setMap(Utility.convertValue(Map.class, v));
} else if ("time".equals(k)) {
dest.time = Utility.convertValue(long.class, v);
} else {
}
});
return dest;
}
}

View File

@@ -4,6 +4,7 @@
package org.redkale.test.util;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import org.junit.jupiter.api.*;
import org.redkale.convert.json.JsonConvert;
import org.redkale.util.*;
@@ -21,6 +22,8 @@ public class ReproduceTest {
test.run3();
test.run4();
test.run5();
test.run6();
test.run7();
}
@Test
@@ -87,4 +90,28 @@ public class ReproduceTest {
System.out.println("Map: " + JsonConvert.root().convertTo(rs));
Assertions.assertEquals(JsonConvert.root().convertTo(map), JsonConvert.root().convertTo(rs));
}
@Test
public void run6() throws Exception {
TestBean bean = new TestBean();
bean.setId(222);
bean.time = 55555L;
bean.setName(null);
bean.setMap(Utility.ofMap("aa", "bbb"));
ConcurrentHashMap rs = Reproduce.copy(ConcurrentHashMap.class, bean);
System.out.println(JsonConvert.root().convertTo(rs));
System.out.println("------------------------------------------");
}
@Test
public void run7() throws Exception {
TestBean bean = new TestBean();
Map map = new TreeMap();
map.put("name", "haha");
map.put("time", "55555");
map.put("id", null);
map.put("map", Utility.ofMap("aa", "bbb"));
Reproduce.load(TestBean.class, Map.class).apply(bean, map);
System.out.println(JsonConvert.root().convertTo(bean));
}
}

View File

@@ -2,10 +2,10 @@
*/
package org.redkale.test.util;
import org.redkale.annotation.Resource;
import org.junit.jupiter.api.*;
import org.redkale.annotation.Resource;
import org.redkale.convert.json.JsonFactory;
import org.redkale.util.*;
import org.redkale.util.ResourceFactory;
/**
*
@@ -46,10 +46,10 @@ public class ResourceLoaderTest {
public static class Bean {
@Resource(name = "$.id")
@Resource(name = "#.id")
public int id;
@Resource(name = "$.name")
@Resource(name = "#.name")
public String name;
public Bean() {