Skip to content

Commit d2d24f3

Browse files
committed
Server:新增URL,BOOLEAN[],STRING[],NUMBER[],DECIMAL[],URL[]类型校验,原有的类型名全改为大写
1 parent 8a5dc80 commit d2d24f3

File tree

2 files changed

+122
-42
lines changed

2 files changed

+122
-42
lines changed

APIJSON-Java-Server/APIJSONORM/src/main/java/zuo/biao/apijson/server/Operation.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@ public enum Operation {
4747
/**
4848
* 添加,当要被添加的对象不存在时
4949
*/
50-
ADD,
50+
INSERT,
51+
@Deprecated
52+
ADD, //用 INSERT 替代,和 RequestMethod.UPDATE 保持长度接近,最快在 4.0.0 移除,请尽快修改 Request 表 structure 字段对应值里的 PUT
5153
/**
5254
* 强行放入,不存在时就添加,存在时就修改
5355
*/
5456
UPDATE,
5557
@Deprecated
56-
PUT, //use UPDATE instead,容易和 RequestMethod.PUT 混淆,最快在 4.0.0 移除,请尽快修改 Request 表 structure 字段对应值里的 PUT
58+
PUT, // UPDATE 替代,容易和 RequestMethod.PUT 混淆,最快在 4.0.0 移除,请尽快修改 Request 表 structure 字段对应值里的 PUT
5759
/**
5860
* 替换,当要被替换的对象存在时
5961
*/

APIJSON-Java-Server/APIJSONORM/src/main/java/zuo/biao/apijson/server/Structure.java

Lines changed: 118 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020
import static zuo.biao.apijson.server.Operation.DISALLOW;
2121
import static zuo.biao.apijson.server.Operation.NECESSARY;
2222
import static zuo.biao.apijson.server.Operation.PUT;
23+
import static zuo.biao.apijson.server.Operation.INSERT;
2324
import static zuo.biao.apijson.server.Operation.REMOVE;
2425
import static zuo.biao.apijson.server.Operation.REPLACE;
2526
import static zuo.biao.apijson.server.Operation.TYPE;
2627
import static zuo.biao.apijson.server.Operation.UNIQUE;
2728
import static zuo.biao.apijson.server.Operation.UPDATE;
2829
import static zuo.biao.apijson.server.Operation.VERIFY;
2930

31+
import java.net.URL;
3032
import java.util.ArrayList;
3133
import java.util.Arrays;
3234
import java.util.Collection;
@@ -148,7 +150,7 @@ private static void verifyId(@NotNull String method, @NotNull String name, @NotN
148150
throw new IllegalArgumentException(method + "请求," + name + "/" + key
149151
+ " 里面的 " + idKey + ":value 中value的类型只能是 Long 或 String !");
150152
}
151-
153+
152154

153155
//批量修改或删除
154156
String idInKey = idKey + "{}";
@@ -227,6 +229,7 @@ public static JSONObject parse(String name, JSONObject target, JSONObject real
227229
//获取配置<<<<<<<<<<<<<<<<<<<<<<<<<<<<
228230
JSONObject type = target.getJSONObject(TYPE.name());
229231
JSONObject verify = target.getJSONObject(VERIFY.name());
232+
JSONObject insert = target.getJSONObject(INSERT.name());
230233
JSONObject add = target.getJSONObject(ADD.name());
231234
JSONObject update = target.getJSONObject(UPDATE.name());
232235
JSONObject put = target.getJSONObject(PUT.name());
@@ -240,6 +243,7 @@ public static JSONObject parse(String name, JSONObject target, JSONObject real
240243
//不还原,传进来的target不应该是原来的
241244
target.remove(TYPE.name());
242245
target.remove(VERIFY.name());
246+
target.remove(INSERT.name());
243247
target.remove(ADD.name());
244248
target.remove(UPDATE.name());
245249
target.remove(PUT.name());
@@ -361,6 +365,7 @@ public static JSONObject parse(String name, JSONObject target, JSONObject real
361365
//在tableKeySet校验后操作,避免 导致put/add进去的Table 被当成原Request的内容
362366
real = operate(TYPE, type, real, creator);
363367
real = operate(VERIFY, verify, real, creator);
368+
real = operate(INSERT, insert, real, creator);
364369
real = operate(ADD, add, real, creator);
365370
real = operate(UPDATE, update, real, creator);
366371
real = operate(PUT, put, real, creator);
@@ -431,6 +436,9 @@ else if (opt == PUT) {
431436
}
432437
}
433438
else {
439+
if (opt == INSERT) {
440+
real.put(tk, tv);
441+
}
434442
if (opt == ADD) {
435443
real.put(tk, tv);
436444
}
@@ -448,74 +456,144 @@ else if (opt == PUT) {
448456
* @param real
449457
* @throws Exception
450458
*/
451-
private static void type(@NotNull String tk, Object tv, @NotNull JSONObject real) throws Exception {
452-
if (tv == null) {
453-
return;
454-
}
459+
public static void type(@NotNull String tk, Object tv, @NotNull JSONObject real) throws UnsupportedDataTypeException {
455460
if (tv instanceof String == false) {
456461
throw new UnsupportedDataTypeException("服务器内部错误," + tk + ":value 的value不合法!"
457462
+ "Request表校验规则中 TYPE:{ key:value } 中的value只能是String类型!");
458463
}
459-
String t = (String) tv;
460-
Object rv = real.get(tk);
464+
465+
type(tk, (String) tv, real.get(tk));
466+
}
467+
468+
469+
public static void type(@NotNull String tk, @NotNull String tv, Object rv) throws UnsupportedDataTypeException {
461470
if (rv == null) {
462471
return;
463472
}
464-
465-
switch (t) {
466-
case "Boolean":
473+
474+
switch (tv) {
475+
case "BOOLEAN":
476+
case "Boolean": // @Deprecated,用 BOOLEAN 替代,最快在 4.0.0 移除,请尽快修改 Request 表 structure 字段对应值里的 Boolean
467477
//Boolean.parseBoolean(real.getString(tk)); 只会判断null和true
468478
if (rv instanceof Boolean == false) { //JSONObject.getBoolean 可转换Number类型
469-
throw new UnsupportedDataTypeException(tk + ":value 的value不合法!类型必须是 Boolean !");
479+
throw new UnsupportedDataTypeException(tk + ":value 的value不合法!类型必须是 BOOLEAN !");
470480
}
471481
break;
472-
case "Long":
482+
case "NUMBER": // 整数
483+
case "Long": // @Deprecated,用 Number 替代,最快在 4.0.0 移除,请尽快修改 Request 表 structure 字段对应值里的 Long
473484
try {
474-
Long.parseLong(real.getString(tk)); //1.23会转换为1 real.getLong(tk);
485+
Long.parseLong(rv.toString()); //1.23会转换为1 real.getLong(tk);
475486
} catch (Exception e) {
476-
throw new UnsupportedDataTypeException(tk + ":value 的value不合法!类型必须是 Long !");
487+
throw new UnsupportedDataTypeException(tk + ":value 的value不合法!类型必须是 NUMBER !");
477488
}
478489
break;
479-
case "Double":
490+
case "DECIMAL": // 小数
491+
case "Double": // @Deprecated,用 Decimal 替代,最快在 4.0.0 移除,请尽快修改 Request 表 structure 字段对应值里的 Double
480492
try {
481493
Double.parseDouble(rv.toString());
482494
} catch (Exception e) {
483-
throw new UnsupportedDataTypeException(tk + ":value 的value不合法!类型必须是 Double !");
495+
throw new UnsupportedDataTypeException(tk + ":value 的value不合法!类型必须是 DECIMAL !");
484496
}
485497
break;
486-
case "String":
498+
case "STRING":
499+
case "String": // @Deprecated,用 STRING 替代,最快在 4.0.0 移除,请尽快修改 Request 表 structure 字段对应值里的 String
500+
case "URL":
487501
if (rv instanceof String == false) { //JSONObject.getString 可转换任何类型
488-
throw new UnsupportedDataTypeException(tk + ":value 的value不合法!类型必须是 String !");
502+
throw new UnsupportedDataTypeException(tk + ":value 的value不合法!类型必须是 STRING !");
489503
}
490-
break;
491-
case "Object":
492-
if (rv instanceof Map == false) { //JSONObject.getJSONObject 可转换String类型
493-
throw new UnsupportedDataTypeException(tk + ":value 的value不合法!类型必须是 {Object} !");
504+
if (tv.equals("URL")) {
505+
try {
506+
new URL((String) rv);
507+
} catch (Exception e) {
508+
throw new UnsupportedDataTypeException(tk + ":value 的value不合法!类型必须是 URL !");
509+
}
494510
}
495511
break;
496-
case "Array":
497-
if (rv instanceof Collection == false) { //JSONObject.getJSONArray 可转换String类型
498-
throw new UnsupportedDataTypeException(tk + ":value 的value不合法!类型必须是 [Array] !");
512+
case "OBJECT":
513+
case "Object": // @Deprecated,用 OBJECT 替代,最快在 4.0.0 移除,请尽快修改 Request 表 structure 字段对应值里的 Object
514+
if (rv instanceof Map == false) { //JSONObject.getJSONObject 可转换String类型
515+
throw new UnsupportedDataTypeException(tk + ":value 的value不合法!类型必须是 OBJECT !");
499516
}
500517
break;
501-
//目前在业务表中还用不上,单一的类型校验已经够用
502-
// case "JSON":
503-
// try {
504-
// com.alibaba.fastjson.JSON.parse(rv.toString());
505-
// } catch (Exception e) {
506-
// throw new UnsupportedDataTypeException(tk + ":value 的value不合法!类型必须是 JSON !"
507-
// + "也就是 {Object}, [Array] 或 它们对应的字符串 '{Object}', '[Array]' 4种中的一个 !");
508-
// }
509-
// break;
510518
default:
511-
throw new UnsupportedDataTypeException("服务器内部错误,类型 " + t + " 不合法!Request表校验规则中"
512-
+ " TYPE:{ key:value } 中的value类型必须是 [Boolean, Long, Double, String, Object, Array] 中的一个!");
519+
switch (tv) {
520+
case "ARRAY":
521+
case "Array": // @Deprecated,用 ARRAY 替代,最快在 4.0.0 移除,请尽快修改 Request 表 structure 字段对应值里的 Array
522+
if (rv instanceof Collection == false) { //JSONObject.getJSONArray 可转换String类型
523+
throw new UnsupportedDataTypeException(tk + ":value 的value不合法!类型必须是 ARRAY !");
524+
}
525+
break;
526+
case "BOOLEAN[]":
527+
type(tk, "ARRAY", rv);
528+
for (Object o : (Collection<?>) rv) {
529+
try {
530+
type(tk, "BOOLEAN", o);
531+
} catch (UnsupportedDataTypeException e) {
532+
throw new UnsupportedDataTypeException(tk + ":value 的value不合法!类型必须是 BOOLEAN[] !");
533+
}
534+
}
535+
break;
536+
case "NUMBER[]":
537+
type(tk, "ARRAY", rv);
538+
for (Object o : (Collection<?>) rv) {
539+
try {
540+
type(tk, "NUMBER", o);
541+
} catch (UnsupportedDataTypeException e) {
542+
throw new UnsupportedDataTypeException(tk + ":value 的value不合法!类型必须是 NUMBER[] !");
543+
}
544+
}
545+
break;
546+
case "DECIMAL[]":
547+
type(tk, "ARRAY", rv);
548+
for (Object o : (Collection<?>) rv) {
549+
try {
550+
type(tk, "DECIMAL", o);
551+
} catch (UnsupportedDataTypeException e) {
552+
throw new UnsupportedDataTypeException(tk + ":value 的value不合法!类型必须是 DECIMAL[] !");
553+
}
554+
}
555+
break;
556+
case "STRING[]":
557+
type(tk, "ARRAY", rv);
558+
for (Object o : (Collection<?>) rv) {
559+
try {
560+
type(tk, "STRING", o);
561+
} catch (UnsupportedDataTypeException e) {
562+
throw new UnsupportedDataTypeException(tk + ":value 的value不合法!类型必须是 STRING[] !");
563+
}
564+
}
565+
break;
566+
case "URL[]":
567+
type(tk, "ARRAY", rv);
568+
for (Object o : (Collection<?>) rv) {
569+
try {
570+
type(tk, "URL", o);
571+
} catch (UnsupportedDataTypeException e) {
572+
throw new UnsupportedDataTypeException(tk + ":value 的value不合法!类型必须是 URL[] !");
573+
}
574+
}
575+
break;
576+
//目前在业务表中还用不上,单一的类型校验已经够用
577+
// case "JSON":
578+
// try {
579+
// com.alibaba.fastjson.JSON.parse(rv.toString());
580+
// } catch (Exception e) {
581+
// throw new UnsupportedDataTypeException(tk + ":value 的value不合法!类型必须是 JSON !"
582+
// + "也就是 {Object}, [Array] 或 它们对应的字符串 '{Object}', '[Array]' 4种中的一个 !");
583+
// }
584+
// break;
585+
586+
default:
587+
throw new UnsupportedDataTypeException(
588+
"服务器内部错误,类型 " + tv + " 不合法!Request表校验规则中 TYPE:{ key:value } 中的 value 必须是"
589+
+ " [ Boolean, Number, Decimal, String, URL, Object, Array, Boolean[], Number[], Decimal[], String[], URL[] ] 中的一个!");
590+
}
591+
513592
}
593+
514594
}
515595

516596

517-
518-
519597
/**验证值
520598
* @param tk
521599
* @param tv
@@ -695,8 +773,8 @@ public static void verifyRepeat(String table, String key, Object value, long exc
695773
if (value instanceof JSON) {
696774
throw new UnsupportedDataTypeException(key + ":value 中value的类型不能为JSON!");
697775
}
698-
699-
776+
777+
700778
SQLConfig config = creator.createSQLConfig().setMethod(RequestMethod.HEAD).setCount(1).setPage(0);
701779
config.setTable(table);
702780
if (exceptId > 0) {//允许修改自己的属性为该属性原来的值

0 commit comments

Comments
 (0)