使用 Thrift 合约中的自定义注释生成 json

问题描述 投票:0回答:1

我需要帮助从 Thrift 合约生成 json。

例如我有这样一个entities.thrift

# Entity name
struct Entity {
    1:  string id, # Comment for id field
    2:  string name, # Comment for name field
    3:  string description # Comment for description
}

结果我想得到 entities.json

{
  "name": "entities",
  "doc": "Entity name\n",
  "namespaces": {

  },
  "includes": [
  ],
  "enums": [
  ],
  "typedefs": [
  ],
  "structs": [
    {
      "name": "Entity",
      "doc": "Entity name\n",
      "isException": false,
      "isUnion": false,
      "fields": [
        {
          "key": 1,
          "name": "id",
          "typeId": "string",
          "doc": "Comment for id field\n",
          "required": "req_out"
        },
        {
          "key": 2,
          "name": "name",
          "typeId": "string",
          "doc": "Comment for name field\n",
          "required": "req_out"
        },
        {
          "key": 3,
          "name": "description",
          "typeId": "string",
          "doc": "Comment for description field\n",
          "required": "req_out"
        }
      ]
    }
  ],
  "constants": [
  ],
  "services": [
  ]
}

我在https://github.com/apache/thrift/blob/master/compiler/cpp/src/thrift/thriftl.ll

中添加了这样的unixcomment实现
{unixcomment} {
    char* text = strdup(yytext);
    if (g_parse_mode == PROGRAM) {
        clear_doctext();
        g_doctext = strdup(yytext + 1);
        g_doctext = clean_up_doctext(g_doctext);
        g_doctext_lineno = yylineno;
        if ((g_program_doctext_candidate == nullptr) && (g_program_doctext_status == INVALID)) {
            g_program_doctext_candidate = strdup(g_doctext);
            g_program_doctext_lineno = g_doctext_lineno;
            g_program_doctext_status = STILL_CANDIDATE;
            pdebug("%s","program doctext set to STILL_CANDIDATE");
        }
    }
}

但结果我得到了这个entities.json(字段上的注释偏移了1):

{
  "name": "entities",
  "doc": "Entity name\n",
  "namespaces": {

  },
  "includes": [
  ],
  "enums": [
  ],
  "typedefs": [
  ],
  "structs": [
    {
      "name": "Entity",
      "doc": "Entity name\n",
      "isException": false,
      "isUnion": false,
      "fields": [
        {
          "key": 1,
          "name": "id",
          "typeId": "string",
          "required": "req_out"
        },
        {
          "key": 2,
          "name": "name",
          "typeId": "string",
          "doc": "Comment for id field\n",
          "required": "req_out"
        },
        {
          "key": 3,
          "name": "description",
          "typeId": "string",
          "doc": "Comment for name field\n",
          "required": "req_out"
        }
      ]
    }
  ],
  "constants": [
  ],
  "services": [
  ]
}

``
c bison thrift
1个回答
0
投票

通常的文档文本注释需要放在之前来源:

/** Struct insanity */
struct Insanity  {

  /** This is doc for field 1 */
  1: map<Numberz, UserId> userMap,

  /** And this is doc for field 2 */
  2: list<Xtruct> xtructs
}

我猜你的扩展中的差一就是因为这个。

PS:示例取自DocTest.thrift

© www.soinside.com 2019 - 2024. All rights reserved.