Skip to content

Commit

Permalink
反映射功能实现,但是实现不太优雅。
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouwentong1993 committed Dec 5, 2023
1 parent 8703c8c commit 05f6f31
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.DynaBean;
import com.alibaba.fastjson2.JSONObject;
import com.wentong.ladder.aviator.AviatorHelper;
import com.wentong.ladder.exceptions.MappingException;
import com.wentong.ladder.handler.MappingHandler;
import com.wentong.ladder.interceptor.LoggingMappingInterceptor;
Expand Down Expand Up @@ -56,7 +58,7 @@ public T mapping(S source, T target) {
DynaBean dynaBean = DynaBean.create(target);
Map<String, Object> sourceMap = BeanUtil.beanToMap(source);
Class<?> clazz = target.getClass();
if (Objects.equals(clazz.getName(), HttpMappingVo.class)) {
if (Objects.equals(clazz.getName(), HttpMappingVo.class.getName())) {
MappingFieldWrapper successWrapper = select(clazz.getField("success"));
realMapping(source, dynaBean, sourceMap, clazz, successWrapper);
boolean success = dynaBean.get("success");
Expand All @@ -68,11 +70,9 @@ public T mapping(S source, T target) {
realMapping(source, dynaBean, sourceMap, clazz, messageWrapper);
}
} else { // 处理普通映射

Map<Object, Object> mappedResponse = (Map<Object, Object>) AviatorHelper.COMPILED_FUNCTION.apply(select(HttpMappingVo.class.getField("data")).expression()).execute((JSONObject) source);
dynaBean.set("data", mappedResponse);
}



T bean = dynaBean.getBean();
mappingInterceptor.afterMapping(source, bean);
return bean;
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/wentong/ladder/vo/HttpMappingVo.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.wentong.ladder.vo;

import com.alibaba.fastjson2.JSONObject;

/**
* HTTP 映射标准对象
*/
public class HttpMappingVo<T> {
public class HttpMappingVo {
public boolean success;
public String message;
public T data;
public JSONObject data;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import com.wentong.ladder.handler.MappingHandler;
import com.wentong.ladder.registry.MappingFieldWrapper;
import com.wentong.ladder.vo.HttpMappingVo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;

class AntiHttpMappingHandlerTest {

Expand All @@ -26,7 +28,7 @@ void mapping() {
void testMapping() throws Exception {
List<MappingFieldWrapper> mappingFieldWrappers = List.of(
new MappingFieldWrapper("errorcode == 0", "success判断", null, MappedType.EXPRESSION, HttpMappingVo.class.getField("success")),
new MappingFieldWrapper("data.totalCost", "预估价", null, MappedType.EXPRESSION, HttpMappingVo.class.getField("data")),
new MappingFieldWrapper("let map = seq.map(\"deliveryFee\",decimal(data.totalCost)-decimal(data.defCouCost),\"distance\",data.totalKm); if(decimal(data.defCouCost)!=0) {map[\"discountFee\"]=decimal(data.defCouCost);} return map;", "预估价", null, MappedType.EXPRESSION, HttpMappingVo.class.getField("data")),
new MappingFieldWrapper("message", "错误响应", null, MappedType.EXPRESSION, HttpMappingVo.class.getField("message"))

);
Expand All @@ -51,7 +53,8 @@ void testMapping() throws Exception {
JSONObject jsonObject = JSONObject.parseObject(sourceStr);

HttpMappingVo mapping = mappingHandler.mapping(jsonObject, HttpMappingVo.class);
System.out.println(mapping);
Assertions.assertTrue(mapping.success);
Assertions.assertEquals("11.19", ((Map) mapping.data.get("data")).get("distance"));

}
}

0 comments on commit 05f6f31

Please sign in to comment.