From 2130bc97e44567e766ea8efacb365383c909dbd4 Mon Sep 17 00:00:00 2001 From: dcodeIO Date: Mon, 6 Mar 2017 16:24:31 +0100 Subject: [PATCH] Docs: Extended traverse-types example, see #693 --- examples/traverse-types.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/examples/traverse-types.js b/examples/traverse-types.js index e4dbea444..567dfff12 100644 --- a/examples/traverse-types.js +++ b/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,8 +18,13 @@ 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) { @@ -26,8 +32,11 @@ function traverseTypes(current, 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 + ); });