|
| 1 | +package com.codingapi.example.convertor; |
| 2 | + |
| 3 | +import com.alibaba.fastjson.JSON; |
| 4 | +import com.codingapi.example.entity.GroovyScriptEntity; |
| 5 | +import com.codingapi.springboot.script.GroovyScript; |
| 6 | +import lombok.SneakyThrows; |
| 7 | +import org.springframework.util.StringUtils; |
| 8 | + |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +public class GroovyScriptConvertor { |
| 13 | + |
| 14 | + public static GroovyScriptEntity convert(GroovyScript groovyScript) { |
| 15 | + if (groovyScript == null) { |
| 16 | + return null; |
| 17 | + } |
| 18 | + GroovyScriptEntity entity = new GroovyScriptEntity(); |
| 19 | + entity.setKey(groovyScript.getKey()); |
| 20 | + entity.setScript(groovyScript.getScript()); |
| 21 | + entity.setDescription(groovyScript.getDescription()); |
| 22 | + entity.setMethod(groovyScript.getMethod()); |
| 23 | + entity.setReturnType(groovyScript.getReturnType() != null ? groovyScript.getReturnType().getName() : null); |
| 24 | + entity.setBinds(groovyScript.getBinds() != null ? JSON.toJSONString(groovyScript.getBinds()) : null); |
| 25 | + entity.setRequests(groovyScript.getRequests() != null ? JSON.toJSONString(groovyScript.getRequests()) : null); |
| 26 | + entity.setTypeOne(groovyScript.getTypeOne()); |
| 27 | + entity.setTypeTwo(groovyScript.getTypeTwo()); |
| 28 | + entity.setRemark(groovyScript.getRemark()); |
| 29 | + entity.setTag(groovyScript.getTag()); |
| 30 | + entity.setCreateTime(groovyScript.getCreateTime()); |
| 31 | + entity.setUpdateTime(groovyScript.getUpdateTime()); |
| 32 | + return entity; |
| 33 | + } |
| 34 | + |
| 35 | + @SneakyThrows |
| 36 | + public static Map<String, Class<?>> toClassMap(String content) { |
| 37 | + if (!StringUtils.hasText(content)) { |
| 38 | + return null; |
| 39 | + } |
| 40 | + Map<String, Object> value = JSON.parseObject(content, Map.class); |
| 41 | + if (value == null || value.isEmpty()) { |
| 42 | + return null; |
| 43 | + } |
| 44 | + Map<String, Class<?>> data = new HashMap<>(); |
| 45 | + |
| 46 | + for (String key : value.keySet()) { |
| 47 | + Object clazzValue = value.get(key); |
| 48 | + if (clazzValue instanceof String) { |
| 49 | + data.put(key, Class.forName((String) clazzValue)); |
| 50 | + } |
| 51 | + if (clazzValue instanceof Class<?>) { |
| 52 | + data.put(key, (Class) clazzValue); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return data; |
| 57 | + } |
| 58 | + |
| 59 | + @SneakyThrows |
| 60 | + public static GroovyScript convert(GroovyScriptEntity entity) { |
| 61 | + if (entity == null) { |
| 62 | + return null; |
| 63 | + } |
| 64 | + return new GroovyScript(entity.getKey(), |
| 65 | + entity.getScript(), |
| 66 | + entity.getDescription(), |
| 67 | + entity.getMethod(), |
| 68 | + entity.getReturnType() != null ? Class.forName(entity.getReturnType()) : null, |
| 69 | + toClassMap(entity.getBinds()), |
| 70 | + toClassMap(entity.getRequests()), |
| 71 | + entity.getTypeOne(), |
| 72 | + entity.getTypeTwo(), |
| 73 | + entity.getTag(), |
| 74 | + entity.getRemark(), |
| 75 | + entity.getCreateTime(), |
| 76 | + entity.getUpdateTime()); |
| 77 | + } |
| 78 | +} |
0 commit comments