Skip to content

Commit

Permalink
Docs: Extended traverse-types example, see #693
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Mar 6, 2017
1 parent 6e81fcb commit 2130bc9
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions examples/traverse-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {\
Expand All @@ -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.