protobuf.js
protobuf.js copied to clipboard
How can I get comments from *.proto file?
//---------------------------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
nodejs 12.*
I met this too, when I use node v10 I could still get comments with 6.8.8
@564064202 oh I found it.
If you use protobufjs.parse(rawstr, { alternateCommentMode: true }), you can find the object under nested has comments
@564064202 oh I found it.
If you use
protobufjs.parse(rawstr, { alternateCommentMode: true }), you can find the object undernestedhascomments
@JennieJi thanks,Could you tell me what do you use it for?
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
@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
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