-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how can i get the message define name from .proto? #693
Comments
Ideally, you'd just traverse through the root instance by calling a function with your logic for each message type within: function traverseTypes(current, fn) {
if (current instanceof protobuf.Type)
fn(current);
if (current.nestedArray)
current.nestedArray.forEach(function(nested) {
traverseTypes(nested, fn);
});
} Example: var root = protobuf.loadSync(...);
traverseTypes(root, function(type) {
console.log(type.fullName);
}); See also: API of protobuf.Type Also created an example. |
@dcodeIO that's not what i really want, your code above will display all the message names that a .proto file have(or imports). but i only want display the single name without any imports(for example i only want display the name: 'AcceptVoipRequest'). |
A var source = ...;
var re = /\bmessage\s+(\w+) /g;
var match;
while (match = re.exec(source)) {
console.log(match[1]);
} or to annotate the messages you'd like to expose in some way understood by reflection, so you can filter. message AcceptVoipRequest {
option someOption = true;
Target target = 1; // 目标
} traverseTypes(root, function(type) {
if (type.options && type.options.someOption)
console.log(type.fullName);
}); Additionally, reflection objects (messages, enums, services etc.) also have a traverseTypes(root, function(type) {
if (/\bAcceptVoipRequest\.proto$/.test(type.filename))
console.log(type.fullName);
}); Likewise, all loaded files are present within See also: Updated example |
@dcodeIO thanks a lot for your patience and help. Examine the exact file or the filename is fit for me. I finally solved this problem by examine the filename and fllow some rules of convention。 |
protobuf.js version: <6.6.4>
i want to collect all the message names in a map, like below:
The text was updated successfully, but these errors were encountered: