Skip to content

Commit 2130bc9

Browse files
committed
Docs: Extended traverse-types example, see #693
1 parent 6e81fcb commit 2130bc9

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

examples/traverse-types.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/*eslint-disable strict, no-console*/
55
var protobuf = require(".."); // require("protobufjs");
66

7+
// traverse-types.proto
78
var proto = "syntax=\"proto3\";\
89
package example;\
910
message Foo {\
@@ -17,17 +18,25 @@ message Bar {\
1718
}\
1819
}";
1920

21+
// the following is loading a string.
22+
// in a real application, it'd be more like protobuf.load("traverse-types.proto", ...)
23+
protobuf.parse.filename = "traverse-types.proto";
24+
var root = protobuf.parse(proto).root;
25+
2026
function traverseTypes(current, fn) {
21-
if (current instanceof protobuf.Type)
27+
if (current instanceof protobuf.Type) // and/or protobuf.Enum, protobuf.Service etc.
2228
fn(current);
2329
if (current.nestedArray)
2430
current.nestedArray.forEach(function(nested) {
2531
traverseTypes(nested, fn);
2632
});
2733
}
2834

29-
var root = protobuf.parse(proto).root;
30-
3135
traverseTypes(root, function(type) {
32-
console.log(type.fullName);
36+
console.log(
37+
type.constructor.className + " " + type.name
38+
+ "\n fully qualified name: " + type.fullName
39+
+ "\n defined in: " + type.filename
40+
+ "\n parent: " + type.parent + " in " + type.parent.filename
41+
);
3342
});

0 commit comments

Comments
 (0)