Permalink
Browse files

Docs: Extended traverse-types example, see #693

  • Loading branch information...
dcodeIO committed Mar 6, 2017
1 parent 6e81fcb commit 2130bc97e44567e766ea8efacb365383c909dbd4
Showing with 13 additions and 4 deletions.
  1. +13 −4 examples/traverse-types.js
@@ -4,6 +4,7 @@
/*eslint-disable strict, no-console*/
var protobuf = require(".."); // require("protobufjs");
// traverse-types.proto
var proto = "syntax=\"proto3\";\
package example;\
message Foo {\
@@ -17,17 +18,25 @@ message Bar {\
}\
}";
// 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)
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);
});
}
var root = protobuf.parse(proto).root;
traverseTypes(root, function(type) {
console.log(type.fullName);
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
);
});

0 comments on commit 2130bc9

Please sign in to comment.