-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
traverse-types.js
42 lines (37 loc) · 1.18 KB
/
traverse-types.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// this example demonstrates how to traverse through a root instance by calling a custom function
// for each message type within.
/*eslint-disable strict, no-console*/
var protobuf = require(".."); // require("protobufjs");
// traverse-types.proto
var proto = "syntax=\"proto3\";\
package example;\
message Foo {\
string a = 1;\
}\
message Bar {\
uint32 b = 1;\
\
message Inner {\
bytes c = 1;\
}\
}";
// the following is loading a string.
// in a real application, it'd be more like protobuf.load("traverse-types.proto", ...)
protobuf.parse.filename = "traverse-types.proto";
var root = protobuf.parse(proto).root;
function traverseTypes(current, fn) {
if (current instanceof protobuf.Type) // and/or protobuf.Enum, protobuf.Service etc.
fn(current);
if (current.nestedArray)
current.nestedArray.forEach(function(nested) {
traverseTypes(nested, fn);
});
}
traverseTypes(root, function(type) {
console.log(
type.constructor.className + " " + type.name
+ "\n fully qualified name: " + type.fullName
+ "\n defined in: " + type.filename
+ "\n parent: " + type.parent + " in " + type.parent.filename
);
});