Skip to content

Commit 54f7da0

Browse files
committed
新增关键词 @string 把字段转为 String 输入;KEY_DEFULT 重命名为 KEY_DEFAULT;升级版本为 8.1.3
1 parent 200be8f commit 54f7da0

File tree

6 files changed

+100
-56
lines changed

6 files changed

+100
-56
lines changed

APIJSONORM/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.github.Tencent</groupId>
77
<artifactId>APIJSON</artifactId>
8-
<version>8.1.0</version>
8+
<version>8.1.3</version>
99
<packaging>jar</packaging>
1010

1111
<name>APIJSONORM</name>

APIJSONORM/src/main/java/apijson/JSONMap.java

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -109,34 +109,47 @@ public static boolean isTableArray(String key) {
109109
public static String KEY_USER_ID = "userId";
110110
public static String KEY_USER_ID_IN = KEY_USER_ID + "{}";
111111

112+
default String getIdKey() {
113+
return KEY_ID;
114+
}
115+
default String getIdInKey() {
116+
return KEY_ID_IN;
117+
}
118+
default String getUserIdKey() {
119+
return KEY_USER_ID;
120+
}
121+
default String getUserIdInKey() {
122+
return KEY_USER_ID_IN;
123+
}
124+
112125
/**set "id":id in Table layer
113126
* @param id
114127
* @return
115128
*/
116129
default JSONMap<M, L> setId(Long id) {
117-
return puts(KEY_ID, id);
130+
return puts(getIdKey(), id);
118131
}
119132
/**set "id{}":[] in Table layer
120133
* @param list
121134
* @return
122135
*/
123136
default JSONMap<M, L> setIdIn(List<Object> list) {
124-
return puts(KEY_ID_IN, list);
137+
return puts(getIdInKey(), list);
125138
}
126139

127140
/**set "userId":userId in Table layer
128141
* @param id
129142
* @return
130143
*/
131144
default JSONMap<M, L> setUserId(Long id) {
132-
return puts(KEY_USER_ID, id);
145+
return puts(getUserIdKey(), id);
133146
}
134147
/**set "userId{}":[] in Table layer
135148
* @param list
136149
* @return
137150
*/
138151
default JSONMap<M, L> setUserIdIn(List<Object> list) {
139-
return puts(KEY_USER_ID_IN, list);
152+
return puts(getUserIdInKey(), list);
140153
}
141154

142155

@@ -150,36 +163,37 @@ default JSONMap<M, L> setUserIdIn(List<Object> list) {
150163

151164

152165
//@key关键字都放这个类 <<<<<<<<<<<<<<<<<<<<<<
153-
String KEY_TRY = "@try"; //尝试,忽略异常
154-
String KEY_CATCH = "@catch"; //TODO 捕捉到异常后,处理方式 null-不处理;DEFAULT-返回默认值;ORIGIN-返回请求里的原始值
155-
String KEY_DROP = "@drop"; //丢弃,不返回,TODO 应该通过 fastjson 的 ignore 之类的机制来处理,避免导致下面的对象也不返回
156-
// String KEY_KEEP = "@keep"; //一定会返回,为 null 或 空对象时,会使用默认值(非空),解决其它对象因为不关联的第一个对为空导致也不返回
157-
String KEY_DEFULT = "@default"; //TODO 自定义默认值 { "@default":true },@default 可完全替代 @keep
158-
String KEY_NULL = "@null"; //值为 null 的键值对 "@null":"tag,pictureList",允许 is NULL 条件判断, SET tag = NULL 修改值为 NULL 等
159-
String KEY_CAST = "@cast"; //类型转换 cast(date AS DATE)
160-
161-
String KEY_ROLE = "@role"; //角色,拥有对某些数据的某些操作的权限
162-
String KEY_DATABASE = "@database"; //数据库类型,默认为MySQL
163-
String KEY_DATASOURCE = "@datasource"; //数据源
164-
String KEY_NAMESPACE = "@namespace"; //命名空间,Table 在非默认 namespace 内时需要声明
165-
String KEY_CATALOG = "@catalog"; //目录,Table 在非默认 catalog 内时需要声明
166-
String KEY_SCHEMA = "@schema"; //数据库,Table 在非默认 schema 内时需要声明
167-
String KEY_EXPLAIN = "@explain"; //分析 true/false
168-
String KEY_CACHE = "@cache"; //缓存 RAM/ROM/ALL
169-
String KEY_COLUMN = "@column"; //查询的Table字段或SQL函数
170-
String KEY_FROM = "@from"; //FROM语句
171-
String KEY_COMBINE = "@combine"; //条件组合,每个条件key前面可以放&,|,!逻辑关系 "id!{},&sex,!name&$"
172-
String KEY_GROUP = "@group"; //分组方式
173-
String KEY_HAVING = "@having"; //聚合函数条件,一般和@group一起用
174-
String KEY_HAVING_AND = "@having&"; //聚合函数条件,一般和@group一起用
175-
String KEY_SAMPLE = "@sample"; //取样方式
176-
String KEY_LATEST = "@latest"; //最近方式
177-
String KEY_PARTITION = "@partition"; //分区方式
178-
String KEY_FILL = "@fill"; //填充方式
179-
String KEY_ORDER = "@order"; //排序方式
166+
String KEY_TRY = "@try"; // 尝试,忽略异常
167+
String KEY_CATCH = "@catch"; // TODO 捕捉到异常后,处理方式 null-不处理;DEFAULT-返回默认值;ORIGIN-返回请求里的原始值
168+
String KEY_DROP = "@drop"; // 丢弃,不返回,TODO 应该通过 fastjson 的 ignore 之类的机制来处理,避免导致下面的对象也不返回
169+
// String KEY_KEEP = "@keep"; // 一定会返回,为 null 或 空对象时,会使用默认值(非空),解决其它对象因为不关联的第一个对为空导致也不返回
170+
String KEY_DEFAULT = "@default"; // TODO 自定义默认值 { "@default":true },@default 可完全替代 @keep
171+
String KEY_NULL = "@null"; // 值为 null 的键值对 "@null":"tag,pictureList",允许 is NULL 条件判断, SET tag = NULL 修改值为 NULL 等
172+
String KEY_CAST = "@cast"; // 类型转换 cast(date AS DATE)
173+
174+
String KEY_ROLE = "@role"; // 角色,拥有对某些数据的某些操作的权限
175+
String KEY_DATABASE = "@database"; // 数据库类型,默认为MySQL
176+
String KEY_DATASOURCE = "@datasource"; // 数据源
177+
String KEY_NAMESPACE = "@namespace"; // 命名空间,Table 在非默认 namespace 内时需要声明
178+
String KEY_CATALOG = "@catalog"; // 目录,Table 在非默认 catalog 内时需要声明
179+
String KEY_SCHEMA = "@schema"; // 数据库,Table 在非默认 schema 内时需要声明
180+
String KEY_EXPLAIN = "@explain"; // 分析 true/false
181+
String KEY_CACHE = "@cache"; // 缓存 RAM/ROM/ALL
182+
String KEY_COLUMN = "@column"; // 查询的 Table 字段或 SQL 函数
183+
String KEY_FROM = "@from"; // FROM语句
184+
String KEY_COMBINE = "@combine"; // 条件组合,每个条件 key 前面可以放 &,|,! 逻辑关系 "id!{},&sex,!name&$"
185+
String KEY_GROUP = "@group"; // 分组方式
186+
String KEY_HAVING = "@having"; // 聚合函数条件,一般和 @group 一起用
187+
String KEY_HAVING_AND = "@having&"; // 聚合函数条件,一般和 @group 一起用
188+
String KEY_SAMPLE = "@sample"; // 取样方式
189+
String KEY_LATEST = "@latest"; // 最近方式
190+
String KEY_PARTITION = "@partition"; // 分区方式
191+
String KEY_FILL = "@fill"; // 填充方式
192+
String KEY_ORDER = "@order"; // 排序方式
180193
String KEY_KEY = "@key"; // key 映射,year:left(date,4);name_tag:(name,tag)
181194
String KEY_RAW = "@raw"; // 自定义原始 SQL 片段
182-
String KEY_JSON = "@json"; //SQL Server 把字段转为 JSON 输出
195+
String KEY_JSON = "@json"; // 把字段转为 JSON 输出
196+
String KEY_STRING = "@string"; // 把字段转为 String 输入
183197
String KEY_METHOD = "@method"; // json 对象配置操作方法
184198
String KEY_GET = "@get"; // json 对象配置操作方法
185199
String KEY_GETS = "@gets"; // json 对象配置操作方法
@@ -214,6 +228,7 @@ default JSONMap<M, L> setUserIdIn(List<Object> list) {
214228
KEY_KEY,
215229
KEY_RAW,
216230
KEY_JSON,
231+
KEY_STRING,
217232
KEY_METHOD,
218233
KEY_GET,
219234
KEY_GETS,
@@ -255,7 +270,7 @@ default JSONMap<M, L> setDrop(Boolean drop) {
255270
* @return this
256271
*/
257272
default JSONMap<M, L> setDefault(Boolean hasDefault) {
258-
return puts(KEY_DEFULT, hasDefault);
273+
return puts(KEY_DEFAULT, hasDefault);
259274
}
260275

261276

@@ -525,6 +540,14 @@ default JSONMap<M, L> setJson(String keys) {
525540
return puts(KEY_JSON, keys);
526541
}
527542

543+
/**set keys to cast to string
544+
* @param keys "key0,key1,key2..."
545+
* @return
546+
*/
547+
default JSONMap<M, L> setString(String keys) {
548+
return puts(KEY_STRING, keys);
549+
}
550+
528551
//JSONObject内关键词 key >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
529552

530553

APIJSONORM/src/main/java/apijson/JSONResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public static String getMsg(Map<String, Object> response) {
119119
*/
120120
default long getId() {
121121
try {
122-
return JSON.getLongValue(this, KEY_ID);
122+
return JSON.getLongValue(this, getIdKey());
123123
} catch (Exception e) {
124124
//empty
125125
}

APIJSONORM/src/main/java/apijson/Log.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class Log {
1414

1515
public static boolean DEBUG = true;
1616

17-
public static final String VERSION = "8.0.2";
17+
public static final String VERSION = "8.1.3";
1818
public static final String KEY_SYSTEM_INFO_DIVIDER = "\n---|-----APIJSON SYSTEM INFO-----|---\n";
1919

2020
public static final String OS_NAME;

APIJSONORM/src/main/java/apijson/orm/AbstractObjectParser.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public AbstractObjectParser<T, M, L> setParser(Parser<T, M, L> parser) {
6161
* TODO Parser内要不因为 非 TYPE_ITEM_CHILD_0 的Table 为空导致后续中断。
6262
*/
6363
protected final boolean drop;
64+
private List<String> stringKeyList;
6465

6566
/**for single object
6667
*/
@@ -103,7 +104,12 @@ public AbstractObjectParser(@NotNull M request, String parentPath, SQLConfig<T,
103104
String raw = getString(request, JSONMap.KEY_RAW);
104105
String[] rks = StringUtil.split(raw);
105106
rawKeyList = rks == null || rks.length <= 0 ? null : Arrays.asList(rks);
106-
}
107+
108+
String str = getString(request, KEY_STRING);
109+
String[] sks = StringUtil.split(str);
110+
stringKeyList = sks == null || sks.length <= 0 ? null : Arrays.asList(sks);
111+
request.remove(KEY_STRING);
112+
}
107113
}
108114

109115
@Override
@@ -280,7 +286,13 @@ public AbstractObjectParser<T, M, L> parse(String name, boolean isReuse) throws
280286
// hasOtherKeyNotFun = true;
281287
// }
282288

283-
if (startsWithAt || key.endsWith("@") || (key.endsWith("<>") && value instanceof Map<?, ?>)) {
289+
if (stringKeyList != null && stringKeyList.contains(key)) {
290+
// 统一格式 String val = value == null || value instanceof String ? (String) value : JSON.toJSONString(value);
291+
if (onParse(key, JSON.toJSONString(value)) == false) {
292+
invalidate();
293+
}
294+
}
295+
else if (startsWithAt || key.endsWith("@") || (key.endsWith("<>") && value instanceof Map<?, ?>)) {
284296
if (onParse(key, value) == false) {
285297
invalidate();
286298
}
@@ -1227,7 +1239,9 @@ public void recycle() {
12271239
if (drop) {
12281240
request.put(KEY_DROP, drop);
12291241
}
1230-
1242+
if (stringKeyList != null) { // 避免被全局关键词覆盖 && ! stringKeyList.isEmpty()) {
1243+
request.put(KEY_STRING, StringUtil.get(stringKeyList.toArray()));
1244+
}
12311245

12321246
method = null;
12331247
parentPath = null;

APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
package apijson.orm;
77

88
import static apijson.JSON.*;
9-
import static apijson.JSONMap.KEY_COMBINE;
10-
import static apijson.JSONMap.KEY_KEY;
9+
import static apijson.JSONMap.*;
1110
import static apijson.RequestMethod.DELETE;
1211
import static apijson.RequestMethod.GET;
1312
import static apijson.RequestMethod.GETS;
@@ -923,6 +922,16 @@ public static <T, M extends Map<String, Object>, L extends List<Object>> M parse
923922
return null;
924923
}
925924

925+
Object _if = target.get(IF.name());
926+
boolean ifIsStr = _if instanceof String && StringUtil.isNotEmpty(_if, true);
927+
M ifObj = ifIsStr == false && _if instanceof Map<?,?> ? (M) _if : null;
928+
// : (_if instanceof String ? new apijson.JSONMap((String) _if, "" /* "throw new Error('')" */ ) : null);
929+
if (ifObj == null && _if != null && ifIsStr == false) {
930+
// if (_if instanceof List<?>) {
931+
// }
932+
throw new IllegalArgumentException(name + ": { " + IF.name() + ": value } 中 value 类型错误!只允许 String, JSONRequest!");
933+
}
934+
926935
// 获取配置<<<<<<<<<<<<<<<<<<<<<<<<<<<<
927936
M type = JSON.get(target, TYPE.name());
928937
M verify = JSON.get(target, VERIFY.name());
@@ -936,21 +945,8 @@ public static <T, M extends Map<String, Object>, L extends List<Object>> M parse
936945
String must = StringUtil.get(getString(target, MUST.name()));
937946
String refuse = StringUtil.get(getString(target, REFUSE.name()));
938947

939-
Object _if = target.get(IF.name());
940-
boolean ifIsStr = _if instanceof String && StringUtil.isNotEmpty(_if, true);
941-
M ifObj = ifIsStr == false && _if instanceof Map<?,?> ? (M) _if : null;
942-
// : (_if instanceof String ? new apijson.JSONMap((String) _if, "" /* "throw new Error('')" */ ) : null);
943-
if (ifObj == null && _if != null && ifIsStr == false) {
944-
// if (_if instanceof List<?>) {
945-
// }
946-
throw new IllegalArgumentException(name + ": { " + IF.name() + ": value } 中 value 类型错误!只允许 String, JSONRequest!");
947-
}
948-
949948
// Object code = target.get(CODE.name());
950949

951-
String allowPartialUpdateFail = StringUtil.get(getString(target, ALLOW_PARTIAL_UPDATE_FAIL.name()));
952-
953-
954950
// 移除字段<<<<<<<<<<<<<<<<<<<
955951
String[] removes = StringUtil.split(remove);
956952
if (removes != null && removes.length > 0) {
@@ -976,6 +972,8 @@ public static <T, M extends Map<String, Object>, L extends List<Object>> M parse
976972
}
977973
// 判断必要字段是否都有>>>>>>>>>>>>>>>>>>>
978974

975+
String[] sks = StringUtil.split(getString(real, KEY_STRING));
976+
List<String> stringKeyList = sks == null || sks.length <= 0 ? null : Arrays.asList(sks);
979977

980978
Set<String> objKeySet = new HashSet<String>(); // 不能用tableKeySet,仅判断 Table:{} 会导致 key:{ Table:{} } 绕过判断
981979

@@ -991,6 +989,10 @@ public static <T, M extends Map<String, Object>, L extends List<Object>> M parse
991989
}
992990
Object tvalue = entry.getValue();
993991
Object rvalue = real.get(key);
992+
if (rvalue != null && stringKeyList != null && stringKeyList.contains(key)) {
993+
rvalue = JSON.toJSONString(rvalue);
994+
}
995+
994996
if (callback.onParse(key, tvalue, rvalue) == false) {
995997
continue;
996998
}
@@ -1091,8 +1093,8 @@ public static <T, M extends Map<String, Object>, L extends List<Object>> M parse
10911093

10921094
// 判断不允许传的key<<<<<<<<<<<<<<<<<<<<<<<<<
10931095
for (String rk : rkset) {
1094-
if (rk == null) { // 无效的key
1095-
real.remove(rk);
1096+
if (rk == null || KEY_STRING.equals(rk)) {
1097+
// ConcurrentModificationException real.remove(rk);
10961098
continue;
10971099
}
10981100

@@ -1111,6 +1113,9 @@ public static <T, M extends Map<String, Object>, L extends List<Object>> M parse
11111113
}
11121114

11131115
Object rv = real.get(rk);
1116+
if (rv != null && stringKeyList != null && stringKeyList.contains(rk)) {
1117+
rv = JSON.toJSONString(rv);
1118+
}
11141119

11151120
// 不允许传远程函数,只能后端配置
11161121
if (rk.endsWith("()") && rv instanceof String) {
@@ -1192,6 +1197,7 @@ public static <T, M extends Map<String, Object>, L extends List<Object>> M parse
11921197
// 校验重复>>>>>>>>>>>>>>>>>>>
11931198

11941199
// 校验并配置允许批量增删改部分失败<<<<<<<<<<<<<<<<<<<
1200+
String allowPartialUpdateFail = StringUtil.get(getString(target, ALLOW_PARTIAL_UPDATE_FAIL.name()));
11951201
String[] partialFails = StringUtil.split(allowPartialUpdateFail);
11961202
if (partialFails != null && partialFails.length > 0) {
11971203
for (String key : partialFails) {
@@ -1300,6 +1306,7 @@ public static <T, M extends Map<String, Object>, L extends List<Object>> M parse
13001306
}
13011307
}
13021308
}
1309+
13031310
Log.i(TAG, "parse return real = " + JSON.toJSONString(real));
13041311
return real;
13051312
}

0 commit comments

Comments
 (0)