protobuf.js icon indicating copy to clipboard operation
protobuf.js copied to clipboard

How can I get comments from *.proto file?

Open introspection3 opened this issue 6 years ago • 8 comments
trafficstars

//---------------------------proto file----------------------- syntax = "proto3";

package Greet;

// The greeting service definition. service Greeter { // Sends a greeting rpc SayHello (HelloRequest) returns (HelloReply) {} rpc SayHello2 (HelloRequest) returns (HelloReply) {} }

// The request message containing the user's name. message HelloRequest { // Sends a greeting string name = 1; int64 number=2;

}

// The response message containing the greetings. message HelloReply { string message = 1; int64 number=2; }

//---------------------------my js code ----------------------- let protobuf = require("protobufjs"); let pPath="./dist/protos/awesome.proto"; protobuf.load(pPath) .then(function (root:any) {
console.log(root);//no comments !!!! });

var pbjs = require("protobufjs/cli/pbjs"); // or require("protobufjs/cli").pbjs / .pbts

pbjs.main(["--comments --target", "json", pPath], function (err:any, output:any) { if (err) throw err; console.log(output);//no comments !!! });

========================result=====================

{ "nested": { "Greet": { "nested": { "Greeter": { "methods": { "SayHello": { "requestType": "HelloRequest", "responseType": "HelloReply" }, "SayHello2": { "requestType": "HelloRequest", "responseType": "HelloReply" } } }, "HelloRequest": { "fields": { "name": { "type": "string", "id": 1 }, "number": { "type": "int64", "id": 2 } } }, "HelloReply": { "fields": { "message": { "type": "string", "id": 1 }, "number": { "type": "int64", "id": 2 } } } } } } } index.ts:15

introspection3 avatar Aug 06 '19 01:08 introspection3

nodejs 12.*

introspection3 avatar Aug 06 '19 01:08 introspection3

I met this too, when I use node v10 I could still get comments with 6.8.8

JennieJi avatar Mar 06 '20 02:03 JennieJi

@564064202 oh I found it.

If you use protobufjs.parse(rawstr, { alternateCommentMode: true }), you can find the object under nested has comments

JennieJi avatar Mar 06 '20 07:03 JennieJi

@564064202 oh I found it.

If you use protobufjs.parse(rawstr, { alternateCommentMode: true }), you can find the object under nested has comments

@JennieJi thanks,Could you tell me what do you use it for?

introspection3 avatar Mar 06 '20 13:03 introspection3

Hi @564064202 I am trying to start a thin node API server directly from the protobuf files server guys provided, some mappings are wrote in comments

JennieJi avatar Mar 07 '20 10:03 JennieJi

@564064202 @JennieJi I tried the option either, but haven't got the comments node. Anything wrong with my code of parser.js? Thanks.

parser.js

const protobuf = require('protobufjs')
const path = require('path')
const fs = require('fs').promises

const Root = protobuf.Root

const source = path.join(__dirname, './test.proto')
const target = path.join(process.cwd(), 'parse-result.json')

async function parse(s, t, root) {
  const rawStr = await fs.readFile(s, 'utf-8')
  const result = protobuf.parse(rawStr, root, { alternateCommentMode: true })
  await fs.writeFile(t, JSON.stringify(result.root.nested, null, 2))
}

;(async () => {
  await parse(source, target, new Root(['Greet']))
})()

test.proto

syntax = "proto3";

package Greet;

// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
rpc SayHello2 (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
// Sends a greeting
string name = 1;
int64 number=2;

}

// The response message containing the greetings.
message HelloReply {
string message = 1;
int64 number=2;
}

parse-result.json

{
  "Greet": {
    "nested": {
      "Greeter": {
        "methods": {
          "SayHello": {
            "requestType": "HelloRequest",
            "responseType": "HelloReply"
          },
          "SayHello2": {
            "requestType": "HelloRequest",
            "responseType": "HelloReply"
          }
        }
      },
      "HelloRequest": {
        "fields": {
          "name": {
            "type": "string",
            "id": 1
          },
          "number": {
            "type": "int64",
            "id": 2
          }
        }
      },
      "HelloReply": {
        "fields": {
          "message": {
            "type": "string",
            "id": 1
          },
          "number": {
            "type": "int64",
            "id": 2
          }
        }
      }
    }
  }
}

btw: platform: win10 node: v12.17.0 protobufjs: v6.10.1

wangpin34 avatar Oct 19 '20 04:10 wangpin34

  const filePath = path.join(__dirname, '../protos/xx.proto');

  const root = new protobuf.Root();
  const res = await root.load(filePath, { keepCase: true,  alternateCommentMode: true, preferTrailingComment: true });  
  const json = res.toJSON({ keepComments: true });

toJSON() should set keepComments: true

Zhou-Bill avatar Jun 24 '24 07:06 Zhou-Bill