参考
1、需求分析
我们在这里均以 sie-snest-log
操作日志这个app为例来描述iidp-helper
插件的需求、设计和实现方案。
-
我们定义一个model的时候,采取的注解格式是这样的:
比如说在上述模型定义的注解中有个@Model(name = "operator_log", parent = "operator_log", displayName = "操作日志", isAutoLog = Bool.True, type = Model.ModelType.Buss) public class OperatorLog extends BaseModel<OperatorLog> { @Property(displayName = "id") private String id; @Property(displayName = "访问时间", dataType = DataType.DATE_TIME) private Date accessStartTime; @Property(displayName = "app名称") private String appName; @Property(displayName = "模型名称") private String logModelName; @Property(displayName = "服务名称") private String serviceName; @Property(displayName = "入参", dataType = DataType.TEXT, widget = "codeeditor") private String inParameterData; @Property(displayName = "出参", dataType = DataType.TEXT, widget = "codeeditor") private String outParameterData; // 用户id @ManyToOne(targetModel = "rbac_user", displayName = "用户") @JoinColumn(name = "caller_user_id") private Map<String, Object> callerUserId; // ip @Property(displayName = "ip") private String callerIp; // 耗时 @Property(displayName = "耗时(ms)") private String takeTime; // 变更 @Property(displayName = "数据变更") private Boolean modify; }
parent
字段,它是一个string,意味着它可以写任何的字符串,在编译阶段是不知道parent是否存在,是否填写正确的, 这给开发阶段带来一些容易出错的地方,这些错误只能到运行阶段才能发现,而且还不容易发现。 -
访问模型名称和模型字段等
继续举例通过meta访问模型数据的方式:meta = new Meta(Meta.SUPERUSER, new HashMap<>()); meta.get("operator_log").call("create", operatorLogList); RecordSet rs2 = meta.get("operator_details"); for (OperatorLog ol : operatorLogList) { rs2.call("create", ol.getOperatorDetailsList()); }
OperatorLog operatorLog = DbUtils.select(filter, "operator_log", OperatorLog.class);
Filter f = Filter.equal("traceID", filter.getFilterOp("id").getValue());
同样地发现存在 operator_log、create、traceID 等都是string字符串的形式存在,在编译阶段也不会做任何的校验,
只能在运行阶段才发现问题,而且还不容易发现,比如 traceID 写错了,只是这个filter失效,不会报错,但查出的结果却是不对的。
- 元模型get set方法
有时候我们需要从模型中get set对应成员变量的值,这些方法写起来也相对繁琐,而且很容易写错,因为这里的get其实会去查数据库, 而有时候我们需要的是从map直接获取值而已,但如果直接使用get方法并不是从map中获取值,所以这里会有一些容易混淆的地方。// getter setter public String getID() { return this.getStr("id"); } public void setID(String id) { this.set("id", id); } public Date getAccessStartTime() { return this.getDate("accessStartTime"); } public void setAccessStartTime(Date accessStartTime) { this.set("accessStartTime", accessStartTime); } public Date getAccessEndTime() { return this.getDate("accessEndTime"); } public void setAccessEndTime(Date accessEndTime) { this.set("accessEndTime", accessEndTime); } public String getAppName() { return this.getStr("appName"); } public void setAppName(String appName) { this.set("appName", appName); } public String getLogModelName() { return this.getStr("logModelName"); } public void setLogModelName(String logModelName) { this.set("logModelName", logModelName); } public String getServiceName() { return this.getStr("serviceName"); } public void setServiceName(String serviceName) { this.set("serviceName", serviceName); } public String getInParameterData() { return this.getStr("inParameterData"); } public void setInParameterData(String inParameterData) { this.set("inParameterData", inParameterData); } public String getOutParameterData() { return this.getStr("outParameterData"); } public void setOutParameterData(String outParameterData) { this.set("outParameterData", outParameterData); } public int getResultDisplay() { return this.getInt("resultDisplay"); } public void setResultDisplay(int resultDisplay) { this.set("resultDisplay", resultDisplay); } public String getAbnormalDisplay() { return this.getStr("abnormalDisplay"); } public void setAbnormalDisplay(String abnormalDisplay) { this.set("abnormalDisplay", abnormalDisplay); } public String getCallerUserName() { return this.getStr("callerUserName"); } public void setCallerUserName(String callerUserName) { this.set("callerUserName", callerUserName); } public String getCallerUserId() { return this.getStr("callerUserId"); } public void setCallerUserId(String callerUserId) { this.set("callerUserId", callerUserId); } public String getCallerIp() { return this.getStr("callerIp"); } public void setCallerIp(String callerIp) { this.set("callerIp", callerIp); } public String getTakeTime() { return this.getStr("takeTime"); } public void setTakeTime(String takeTime) { this.set("takeTime", takeTime); } public Boolean getModify() { return this.getBoolean("modify"); } public void setModify(Boolean modify) { this.set("modify", modify); } public List<OperatorDetails> getOperatorDetailsList() { return (List<OperatorDetails>) this.get("operatorDetailsList"); } public void setOperatorDetailsList(List<OperatorDetails> operatorDetailsList) { this.set("operatorDetailsList", operatorDetailsList); }
总结:从上述的几种基于iidp平台编写app的过程中,我们发现有很多地方都是基于string字符串字面量的形式来表述,很容易写错, 而且没办法在编译器进行校验,这给app开发者带来了很多麻烦,降低了开发的效率。如果我们能够提供一个插件,对模型名称、模型属性和模型方法等字符串字面量给与它本身的含义,变得结构化起来,那么在开发阶段会让开发者更容易识别这些含义,同时也很容易检测出错误,提高开发者开发效率,同时也能保持代码的精简。
2、方案设计
建立模型名称、模型属性和模型方法与它的字符串字面量建立引用关系,同时对字符串进行着色、跳转和检测
3、方案实现
基于 IDEA插件开发api,建立字符串字面量到实际定义处的引用,同时扫描当前工程的所有模型文件,建立所有模型的索引信息,从而达到模型名称、属性和方法的校验等功能。