public SqlJsonImplementation(Object jsonObject, Field field) {
    this.jsonObject = jsonObject;
    if (jsonObject instanceof String) field.setType("string");
    else if (jsonObject instanceof Number) field.setType("number");
    else if (jsonObject instanceof Boolean) field.setType("boolean");
    else if (jsonObject instanceof List) field.setType("array");
    // map should be json as well
    else field.setType("json");

    this.field = field;
  }
Esempio n. 2
0
 private List<Field> parseFields(List nodes, DocResult outerDoc, Node parent) {
   List<Field> fields = new ArrayList<Field>(nodes.size());
   for (Element node : (List<Element>) nodes) {
     Field field = new Field();
     String name = node.attributeValue("name");
     field.setName(name);
     field.setParent(parent);
     DocResult docResult = parseDoc(node, field);
     String type = node.attributeValue("type");
     field.setType(type);
     field.setRequired(!"optional".equals(node.attributeValue("required")));
     String index = node.attributeValue("index");
     if (index != null) {
       field.setIndex(Integer.parseInt(index));
     }
     String nodeName = node.getName();
     if ("const".equals(nodeName)) { // 常量
       field.setValue(node.element("value").getStringValue());
     } else if ("item".equals(nodeName)) { // 枚举项
       String value = node.attributeValue("value");
       field.setIndex(Integer.parseInt(value)); // 设置枚举项的index为对应value
       field.setValue(value);
     } else if ("ex".equals(nodeName)) { // 异常
       Doc fieldDoc = field.getDoc();
       if (fieldDoc == DocResult.NULL) {
         field.setDoc(fieldDoc = new Doc());
       }
       for (Map.Entry<String, String> entry : docResult.getErrors().entrySet()) { // 将doc中的异常信息写入属性
         fieldDoc.putTag(entry.getKey(), entry.getValue());
       }
       if (outerDoc != null) { // 方法文档不为空
         String outerEx = outerDoc.getError(name); // 读取方法文档中的异常描述
         if (StringUtils.isEmpty(fieldDoc.getDesc()) && outerEx != null) { // 如果自己的文档没有注释,则设置为方法的注释
           fieldDoc.setDesc(outerEx);
         }
         if ("Type.AnyException"
             .equals(type)) { // 如果是通用的AnyException,则将外部定义的类似@error 315 无效用户状态写入属性
           for (Map.Entry<String, String> entry : outerDoc.getErrors().entrySet()) {
             String key = entry.getKey();
             if (!key.endsWith("Exception")) { // 若果是Exception说明不是一个异常代码
               fieldDoc.putTag(entry.getKey(), entry.getValue());
             }
           }
         }
       }
     } else {
       field.setValue(node.attributeValue("default"));
     }
     if (outerDoc != null) {
       Doc outerFieldDoc = outerDoc.getFidleDoc(field.getName()); // 判断外部是否定义了字段的描述文档
       if (outerFieldDoc != null) {
         Doc fieldDoc = field.getDoc();
         if (fieldDoc == DocResult.NULL) {
           field.setDoc(outerFieldDoc);
         } else {
           if (StringUtils.isEmpty(fieldDoc.getDesc())) {
             fieldDoc.setDesc(outerFieldDoc.getDesc());
           }
           for (Map.Entry<String, String> entry : outerFieldDoc.getTags().entrySet()) {
             fieldDoc.putTagIfAbsent(entry.getKey(), entry.getValue()); // 默认内部注视优先
           }
         }
       }
     }
     fields.add(field);
   }
   return fields;
 }