Skip to content

Commit bccb5ef

Browse files
committed
feat: add @Trim: "name,tag" to trim string values for keys
1 parent 5744eab commit bccb5ef

File tree

3 files changed

+82
-63
lines changed

3 files changed

+82
-63
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ default JSONMap<M, L> setUserIdIn(List<Object> list) {
194194
String KEY_RAW = "@raw"; // 自定义原始 SQL 片段
195195
String KEY_JSON = "@json"; // 把字段转为 JSON 输出
196196
String KEY_STRING = "@string"; // 把字段转为 String 输入
197+
String KEY_TRIM = "@trim"; // 去除首位空格等空白字符
197198
String KEY_METHOD = "@method"; // json 对象配置操作方法
198199
String KEY_GET = "@get"; // json 对象配置操作方法
199200
String KEY_GETS = "@gets"; // json 对象配置操作方法
@@ -229,6 +230,7 @@ default JSONMap<M, L> setUserIdIn(List<Object> list) {
229230
KEY_RAW,
230231
KEY_JSON,
231232
KEY_STRING,
233+
KEY_TRIM,
232234
KEY_METHOD,
233235
KEY_GET,
234236
KEY_GETS,
@@ -548,6 +550,14 @@ default JSONMap<M, L> setString(String keys) {
548550
return puts(KEY_STRING, keys);
549551
}
550552

553+
/**set keys to cast to string
554+
* @param keys "key0,key1,key2..."
555+
* @return
556+
*/
557+
default JSONMap<M, L> setTrim(String keys) {
558+
return puts(KEY_TRIM, keys);
559+
}
560+
551561
//JSONObject内关键词 key >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
552562

553563

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public AbstractObjectParser<T, M, L> setParser(Parser<T, M, L> parser) {
6262
*/
6363
protected final boolean drop;
6464
private List<String> stringKeyList;
65+
private List<String> trimKeyList;
6566

6667
/**for single object
6768
*/
@@ -109,6 +110,11 @@ public AbstractObjectParser(@NotNull M request, String parentPath, SQLConfig<T,
109110
String[] sks = StringUtil.split(str);
110111
stringKeyList = sks == null || sks.length <= 0 ? null : Arrays.asList(sks);
111112
request.remove(KEY_STRING);
113+
114+
String trim = getString(request, KEY_TRIM);
115+
String[] trims = StringUtil.split(trim);
116+
trimKeyList = trims == null || trims.length <= 0 ? null : Arrays.asList(trims);
117+
request.remove(KEY_TRIM);
112118
}
113119
}
114120

@@ -286,13 +292,16 @@ public AbstractObjectParser<T, M, L> parse(String name, boolean isReuse) throws
286292
// hasOtherKeyNotFun = true;
287293
// }
288294

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-
}
295+
boolean isTrim = (trimKeyList != null && trimKeyList.contains(key));
296+
boolean toStr = isTrim || (stringKeyList != null && stringKeyList.contains(key));
297+
if (toStr) {
298+
value = JSON.toJSONString(value);
294299
}
295-
else if (startsWithAt || key.endsWith("@") || (key.endsWith("<>") && value instanceof Map<?, ?>)) {
300+
if (isTrim && value != null) {
301+
value = StringUtil.trim(value);
302+
}
303+
304+
if (startsWithAt || key.endsWith("@") || (key.endsWith("<>") && value instanceof Map<?, ?>)) {
296305
if (onParse(key, value) == false) {
297306
invalidate();
298307
}
@@ -1242,6 +1251,9 @@ public void recycle() {
12421251
if (stringKeyList != null) { // 避免被全局关键词覆盖 && ! stringKeyList.isEmpty()) {
12431252
request.put(KEY_STRING, StringUtil.get(stringKeyList.toArray()));
12441253
}
1254+
if (trimKeyList != null) { // 避免被全局关键词覆盖 && ! trimKeyList.isEmpty()) {
1255+
request.put(KEY_TRIM, StringUtil.get(trimKeyList.toArray()));
1256+
}
12451257

12461258
method = null;
12471259
parentPath = null;

0 commit comments

Comments
 (0)