fix: 引擎层处理GString序列化+解析失败时matched=false
This commit is contained in:
@@ -55,25 +55,32 @@ public class GroovyScriptEngine {
|
||||
|
||||
private Map<String, Object> buildResult(Map<String, Object> result, Object scriptResult) {
|
||||
if (scriptResult instanceof Map) {
|
||||
result.put("matched", true);
|
||||
Map<String, Object> m = (Map<String, Object>) scriptResult;
|
||||
result.put("pageName", m.getOrDefault("pageName", "Groovy脚本"));
|
||||
String pageName = toString(m.get("pageName"));
|
||||
result.put("pageName", pageName != null ? pageName : "Groovy脚本");
|
||||
|
||||
if (m.containsKey("fields")) {
|
||||
result.put("extracted", List.of(m.get("fields")));
|
||||
result.put("matched", true);
|
||||
result.put("extracted", List.of(stringifyValues(m.get("fields"))));
|
||||
result.put("rowCount", 1);
|
||||
result.put("fieldCount", ((Map<?, ?>) m.get("fields")).size());
|
||||
} else if (m.containsKey("extracted")) {
|
||||
result.put("matched", true);
|
||||
List<?> ex = (List<?>) m.get("extracted");
|
||||
result.put("extracted", ex);
|
||||
result.put("rowCount", ex.size());
|
||||
result.put("fieldCount", !ex.isEmpty() && ex.get(0) instanceof Map
|
||||
? ((Map<?, ?>) ex.get(0)).size() : 0);
|
||||
} else {
|
||||
result.put("extracted", List.of(m));
|
||||
// 无 fields/extracted → 可能是错误返回或简单结果
|
||||
boolean isError = "解析失败".equals(pageName);
|
||||
result.put("matched", !isError);
|
||||
result.put("extracted", List.of(stringifyValues(m)));
|
||||
result.put("rowCount", 1);
|
||||
result.put("fieldCount", m.size());
|
||||
}
|
||||
result.put("message", m.getOrDefault("message", "解析成功"));
|
||||
result.put("message", toString(m.get("message")));
|
||||
if (result.get("message") == null) result.put("message", "解析成功");
|
||||
} else if (scriptResult instanceof List) {
|
||||
result.put("matched", true);
|
||||
result.put("pageName", "Groovy脚本");
|
||||
@@ -98,6 +105,21 @@ public class GroovyScriptEngine {
|
||||
}
|
||||
|
||||
/** 从 Groovy 异常中提取含行号的错误信息 */
|
||||
private String toString(Object val) {
|
||||
return val != null ? val.toString() : null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Map<String, Object> stringifyValues(Object obj) {
|
||||
if (!(obj instanceof Map)) return Map.of();
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
for (Map.Entry<?, ?> e : ((Map<?, ?>) obj).entrySet()) {
|
||||
Object v = e.getValue();
|
||||
result.put(e.getKey().toString(), v != null ? v.toString() : null);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private String extractGroovyError(Exception e) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(e.getMessage() != null ? e.getMessage() : e.getClass().getSimpleName());
|
||||
|
||||
Reference in New Issue
Block a user