|
@@ -0,0 +1,88 @@
|
|
|
|
|
+package com.malk.utils;
|
|
|
|
|
+
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.Collections;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 第三方响应 result 字段类型守卫工具
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>统一处理 VenR 子类(DDR / DDR_New / YDR / INTPR 等)的 {@code getResult()} / {@code getData()}
|
|
|
|
|
+ * 返回值——通常是 {@code Object},需要 instanceof + cast。本工具把"判类型 + 强转 + 容错默认值"
|
|
|
|
|
+ * 收敛为单方法调用,避免 Client 实现里散落 4-5 行的样板。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>典型用法:</p>
|
|
|
|
|
+ * <pre>
|
|
|
|
|
+ * DDR_New r = ...assertResult(...);
|
|
|
|
|
+ * Map data = UtilRespMapper.asMap(r.getResult());
|
|
|
|
|
+ * List<String> ids = UtilRespMapper.asStringList(r.getResult());
|
|
|
|
|
+ * List<Map<String, Object>> rows = UtilRespMapper.asMapList(r.getResult());
|
|
|
|
|
+ * </pre>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>所有方法在 {@code result} 为 null 或类型不符时返回**安全默认值**(空集合 / null / 0 / false),
|
|
|
|
|
+ * 不抛异常——调用方已通过 assertSuccess 处理过失败响应,此处只关心成功响应的类型分发。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+public abstract class UtilRespMapper {
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Map<String, Object> 守卫;不符返回 emptyMap
|
|
|
|
|
+ */
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
|
+ public static Map<String, Object> asMap(Object result) {
|
|
|
|
|
+ if (result instanceof Map) {
|
|
|
|
|
+ return (Map<String, Object>) result;
|
|
|
|
|
+ }
|
|
|
|
|
+ return Collections.emptyMap();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * List<Map<String, Object>> 守卫(数据行集合,常见于查询接口的 records / items)
|
|
|
|
|
+ */
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
|
+ public static List<Map<String, Object>> asMapList(Object result) {
|
|
|
|
|
+ if (!(result instanceof List)) {
|
|
|
|
|
+ return Collections.emptyList();
|
|
|
|
|
+ }
|
|
|
|
|
+ // fixme: 走 JSON round-trip 是为了把内部 LinkedHashMap/JSONObject 统一成 Map<String,Object>
|
|
|
|
|
+ return JSON.parseArray(JSON.toJSONString(result), Map.class).stream()
|
|
|
|
|
+ .map(m -> (Map<String, Object>) m)
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * List<String> 守卫(常见于 ID 列表 / 简单字符串数组)
|
|
|
|
|
+ */
|
|
|
|
|
+ public static List<String> asStringList(Object result) {
|
|
|
|
|
+ if (!(result instanceof List)) {
|
|
|
|
|
+ return Collections.emptyList();
|
|
|
|
|
+ }
|
|
|
|
|
+ return JSON.parseArray(JSON.toJSONString(result), String.class);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 字符串守卫;result 为 null 返回 null(非 "null" 字符串)
|
|
|
|
|
+ */
|
|
|
|
|
+ public static String asString(Object result) {
|
|
|
|
|
+ return result == null ? null : String.valueOf(result);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 整数守卫;非 Number 返回 defaultVal
|
|
|
|
|
+ */
|
|
|
|
|
+ public static int asInt(Object result, int defaultVal) {
|
|
|
|
|
+ if (result instanceof Number) {
|
|
|
|
|
+ return ((Number) result).intValue();
|
|
|
|
|
+ }
|
|
|
|
|
+ return defaultVal;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 布尔守卫;接受 Boolean.TRUE 或字面量 "true"(大小写忽略)
|
|
|
|
|
+ */
|
|
|
|
|
+ public static boolean asBool(Object result) {
|
|
|
|
|
+ return Boolean.TRUE.equals(result) || "true".equalsIgnoreCase(String.valueOf(result));
|
|
|
|
|
+ }
|
|
|
|
|
+}
|