Merge branch 'master' into develop

This commit is contained in:
Francis Dong
2021-11-15 10:19:07 +08:00
4 changed files with 216 additions and 8 deletions

View File

@@ -4,7 +4,7 @@ English | [简体中文](./README.md)
<a href="https://www.fizzgate.com"><img src="https://raw.githubusercontent.com/wiki/wehotel/fizz-gateway-community/img/icon-color.png" width="70%"></a>
</p>
<p>
<img alt="Version" src="https://img.shields.io/badge/version-2.3.2-blue.svg?cacheSeconds=2592000" />
<img alt="Version" src="https://img.shields.io/badge/version-2.3.3-blue.svg?cacheSeconds=2592000" />
<a href="http://www.fizzgate.com/fizz-gateway-community/" target="_blank">
<img alt="Documentation" src="https://img.shields.io/badge/documentation-yes-brightgreen.svg" />
</a>
@@ -109,6 +109,7 @@ Starting from v1.3.0, the frontend and backend of the management backend are mer
| v2.2.3 | v2.2.3 |
| v2.3.0 | v2.3.0 |
| v2.3.2 | v2.3.2 |
| v2.3.3 | v2.3.3 |
Please download the corresponding management backend version according to the version of the community version

View File

@@ -3,7 +3,7 @@
<a href="https://www.fizzgate.com"><img src="https://raw.githubusercontent.com/wiki/wehotel/fizz-gateway-community/img/icon-color.png" width="70%"></a>
</p>
<p>
<img alt="Version" src="https://img.shields.io/badge/version-2.3.2-blue.svg?cacheSeconds=2592000" />
<img alt="Version" src="https://img.shields.io/badge/version-2.3.3-blue.svg?cacheSeconds=2592000" />
<a href="http://www.fizzgate.com/fizz-gateway-community/" target="_blank">
<img alt="Documentation" src="https://img.shields.io/badge/documentation-yes-brightgreen.svg" />
</a>
@@ -110,6 +110,7 @@ API地址http://demo.fizzgate.com/proxy/[服务名]/[API_Path]
| v2.2.3 | v2.2.3 |
| v2.3.0 | v2.3.0 |
| v2.3.2 | v2.3.2 |
| v2.3.3 | v2.3.3 |
请根据社区版的版本下载对应的管理后台版本

View File

@@ -25,6 +25,8 @@ import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import we.fizz.exception.FizzRuntimeException;
/**
* List Functions
*
@@ -58,6 +60,8 @@ public class ListFunc implements IFunc {
FuncExecutor.register(NAME_SPACE_PREFIX + "list.merge", this);
FuncExecutor.register(NAME_SPACE_PREFIX + "list.extract", this);
FuncExecutor.register(NAME_SPACE_PREFIX + "list.join", this);
FuncExecutor.register(NAME_SPACE_PREFIX + "list.rename", this);
FuncExecutor.register(NAME_SPACE_PREFIX + "list.removeFields", this);
}
/**
@@ -127,7 +131,9 @@ public class ListFunc implements IFunc {
*
* @param dest destination list
* @param src source list
* @param joinField join field
* @param joinField join field, pattern: joinFieldOfDest:joinFieldOfSrc,
* :joinFieldOfSrc could be omitted if both join field names
* are the same
* @param fields fields which will be merge to destination list, all fields
* will be merged if it is null
* @return
@@ -137,18 +143,23 @@ public class ListFunc implements IFunc {
if (dest == null || dest.size() == 0 || src == null || src.size() == 0) {
return dest;
}
String[] joinFields = joinField.split(":");
if (joinFields.length == 1) {
joinFields = new String[] {joinField, joinField};
}
Map<String, Map<String, Object>> index = new HashMap<>();
for (Map<String, Object> record : dest) {
if (record.get(joinField) != null) {
index.put(record.get(joinField).toString(), record);
if (record.get(joinFields[0]) != null) {
index.put(record.get(joinFields[0]).toString(), record);
}
}
for (Map<String, Object> m : src) {
if (m.get(joinField) == null) {
Object srcJoinFieldVal = m.get(joinFields[1]);
if (srcJoinFieldVal == null || !index.containsKey(srcJoinFieldVal.toString())) {
continue;
}
Map<String, Object> record = index.get(m.get(joinField).toString());
Map<String, Object> record = index.get(srcJoinFieldVal.toString());
if (fields == null || fields.length == 0) {
record.putAll(m);
@@ -162,4 +173,59 @@ public class ListFunc implements IFunc {
return dest;
}
/**
* Rename fields of list
*
* @param data list
* @param fieldPairs old and new key pair of map of list, pattern:
* oldFieldName:newFieldName
* @return
*/
public List<Map<String, Object>> rename(List<Map<String, Object>> data, String... fieldPairs) {
if (data == null || data.size() == 0) {
return data;
}
if (fieldPairs == null || fieldPairs.length == 0) {
return data;
}
for (Map<String, Object> m : data) {
for (String fieldPair : fieldPairs) {
String[] parts = fieldPair.split(":");
if (parts == null || parts.length != 2) {
LOGGER.warn("invalid fieldPair: {} , field pair pattern is: oldFieldName:newFieldName", fieldPair);
throw new FizzRuntimeException(
"invalid fieldPair: " + fieldPair + " , field pair pattern is: oldFieldName:newFieldName");
}
if (m.containsKey(parts[0])) {
m.put(parts[1], m.get(parts[0]));
m.remove(parts[0]);
}
}
}
return data;
}
/**
* Remove fields from list
*
* @param data
* @param fields fields to be removed
* @return
*/
public List<Map<String, Object>> removeFields(List<Map<String, Object>> data, String... fields) {
if (data == null || data.size() == 0) {
return data;
}
if (fields == null || fields.length == 0) {
return data;
}
for (Map<String, Object> m : data) {
for (String field : fields) {
m.remove(field);
}
}
return data;
}
}

View File

@@ -166,9 +166,149 @@ class ListFuncTests {
assertEquals(5, result.size());
assertEquals("a2", ((Map<String, Object>) result.get(1)).get("a").toString());
assertEquals("d4-abc", ((Map<String, Object>) result.get(3)).get("d").toString());
// System.out.println(JSON.toJSONString(result));
String json1 = "[\n"
+ " {\n"
+ " \"itemType\": \"3\",\n"
+ " \"currCd\": \"156\",\n"
+ " \"batchDate\": \"20190331\",\n"
+ " \"itemCd\": \"ORGAP0008\",\n"
+ " \"itemVal\": 0.7189,\n"
+ " \"itemNm\": \"balance rate\",\n"
+ " \"valueStr\": \"0.7189\",\n"
+ " \"orgCd1\": \"230009999\"\n"
+ " },\n"
+ " {\n"
+ " \"itemType\": \"3\",\n"
+ " \"currCd\": \"156\",\n"
+ " \"batchDate\": \"20190331\",\n"
+ " \"itemCd\": \"ORGAP0008\",\n"
+ " \"itemVal\": 0.7040,\n"
+ " \"itemNm\": \"balance rate\",\n"
+ " \"valueStr\": \"0.7040\",\n"
+ " \"orgCd1\": \"231009999\"\n"
+ " }\n"
+ " ]";
String json2 = "[\n"
+ " {\n"
+ " \"orgName\": \"Name1\",\n"
+ " \"orgCd\": \"230009999\"\n"
+ " },\n"
+ " {\n"
+ " \"orgName\": \"Name2\",\n"
+ " \"orgCd\": \"231009999\"\n"
+ " },\n"
+ " {\n"
+ " \"orgName\": \"Name3\",\n"
+ " \"orgCd\": \"232009999\"\n"
+ " },\n"
+ " {\n"
+ " \"orgName\": \"Name3\",\n"
+ " \"orgCd\": \"233009999\"\n"
+ " },\n"
+ " {\n"
+ " \"orgName\": \"Name4\",\n"
+ " \"orgCd\": \"234009999\"\n"
+ " },\n"
+ " {\n"
+ " \"orgName\": \"Name5\",\n"
+ " \"orgCd\": \"235009999\"\n"
+ " },\n"
+ " {\n"
+ " \"orgName\": \"Name6\",\n"
+ " \"orgCd\": \"236009999\"\n"
+ " },\n"
+ " {\n"
+ " \"orgName\": \"Name7\",\n"
+ " \"orgCd\": \"237009999\"\n"
+ " },\n"
+ " {\n"
+ " \"orgName\": \"Name8\",\n"
+ " \"orgCd\": \"238009999\"\n"
+ " },\n"
+ " {\n"
+ " \"orgName\": \"Name9\",\n"
+ " \"orgCd\": \"239009999\"\n"
+ " }\n"
+ " ]";
ONode ctxNode2 = ONode.load(new HashMap());
PathMapping.setByPath(ctxNode2, "test.list1", JSON.parseArray(json1, Map.class), true);
PathMapping.setByPath(ctxNode2, "test.list2", JSON.parseArray(json2, Map.class), true);
String funcExpression2 = "fn.list.join({test.list1},{test.list2},\"orgCd1:orgCd\")";
List<Object> result2 = (List<Object>) FuncExecutor.getInstance().exec(ctxNode2, funcExpression2);
System.out.println(JSON.toJSONString(result2));
assertEquals("Name1", ((Map<String, Object>) result2.get(0)).get("orgName").toString());
}
@Test
void testRename() {
List<Object> subList1 = new ArrayList<>();
subList1.add(createRecord2(1));
subList1.add(createRecord2(2));
subList1.add(createRecord2(3));
subList1.add(createRecord2(4));
subList1.add(createRecord2(5));
List<Object> subList2 = new ArrayList<>();
subList2.add(createRecord2(1));
Map<String, Object> m2 = createRecord2(2);
m2.put("b", "b22222");
subList2.add(m2);
subList2.add(createRecord2(3));
Map<String, Object> m4 = createRecord2(4);
m4.put("e", "e44444");
subList2.add(m4);
subList2.add(createRecord2(5));
ONode ctxNode = ONode.load(new HashMap());
PathMapping.setByPath(ctxNode, "test.data", subList1, true);
PathMapping.setByPath(ctxNode, "test.data2", subList2, true);
String funcExpression = "fn.list.rename({test.data}, \"b:birthday\", \"e:email\")";
List<Object> result = (List<Object>) FuncExecutor.getInstance().exec(ctxNode, funcExpression);
System.out.println(JSON.toJSONString(result));
assertEquals(5, result.size());
assertEquals("b2", ((Map<String, Object>) result.get(1)).get("birthday").toString());
assertEquals("e4", ((Map<String, Object>) result.get(3)).get("email").toString());
assertEquals(null, ((Map<String, Object>) result.get(3)).get("b"));
assertEquals(null, ((Map<String, Object>) result.get(3)).get("e"));
String funcExpression2 = "fn.list.join({test.data}, fn.list.rename({test.data2}, \"b:birthday\", \"e:email\"), \"a\", \"birthday\", \"email\")";
List<Object> result2 = (List<Object>) FuncExecutor.getInstance().exec(ctxNode, funcExpression2);
System.out.println(JSON.toJSONString(result2));
assertEquals(5, result2.size());
assertEquals("b2", ((Map<String, Object>) result2.get(1)).get("b").toString());
assertEquals("e4", ((Map<String, Object>) result2.get(3)).get("e").toString());
assertEquals("b22222", ((Map<String, Object>) result2.get(1)).get("birthday").toString());
assertEquals("e44444", ((Map<String, Object>) result2.get(3)).get("email").toString());
}
@Test
void testRemoveFields() {
List<Object> subList1 = new ArrayList<>();
subList1.add(createRecord2(1));
subList1.add(createRecord2(2));
subList1.add(createRecord2(3));
subList1.add(createRecord2(4));
subList1.add(createRecord2(5));
ONode ctxNode = ONode.load(new HashMap());
PathMapping.setByPath(ctxNode, "test.data", subList1, true);
String funcExpression = "fn.list.removeFields({test.data}, \"b\", \"e\")";
List<Object> result = (List<Object>) FuncExecutor.getInstance().exec(ctxNode, funcExpression);
System.out.println(JSON.toJSONString(result));
assertEquals(5, result.size());
assertEquals(null, ((Map<String, Object>) result.get(1)).get("b"));
assertEquals(null, ((Map<String, Object>) result.get(1)).get("e"));
assertEquals(null, ((Map<String, Object>) result.get(3)).get("b"));
assertEquals(null, ((Map<String, Object>) result.get(3)).get("e"));
}
}