-1

I need help with generating json from a thrift contract.

For example, I have such a 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
}

And as a result I want to get 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": [
  ]
}

I have added such an implementation for unixcomment in https://github.com/apache/thrift/blob/master/compiler/cpp/src/thrift/thriftl.ll

{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");
        }
    }
}

But as a result I get this entities.json (Comments on fields are offset by 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": [
  ]
}

``
273K
  • 29,503
  • 10
  • 41
  • 64
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Aug 27 '23 at 11:49

0 Answers0