From 367d55523a3ae88f21d47aa96447ec3e943d4620 Mon Sep 17 00:00:00 2001 From: dcodeIO Date: Thu, 2 Mar 2017 16:22:54 +0100 Subject: [PATCH] Other: Traversal example + minimalistic documentation --- examples/custom-get-set.js | 2 ++ examples/reader-writer.js | 9 +++++---- examples/streaming-rpc.js | 3 ++- examples/traverse-types.js | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 examples/traverse-types.js diff --git a/examples/custom-get-set.js b/examples/custom-get-set.js index 656aa4523..73833abb3 100644 --- a/examples/custom-get-set.js +++ b/examples/custom-get-set.js @@ -11,10 +11,12 @@ message MyMessage {\ var root = protobuf.parse(proto, { keepCase: true }).root; // or use Root#load +// converts a string from underscore notation to camel case function toCamelCase(str) { return str.substring(0,1) + str.substring(1).replace(/_([a-z])(?=[a-z]|$)/g, function($0, $1) { return $1.toUpperCase(); }); } +// adds a virtual alias property function addAliasProperty(type, name, aliasName) { if (aliasName !== name) Object.defineProperty(type.ctor.prototype, aliasName, { diff --git a/examples/reader-writer.js b/examples/reader-writer.js index 834e4ed0b..7361b79f0 100644 --- a/examples/reader-writer.js +++ b/examples/reader-writer.js @@ -1,7 +1,8 @@ -/*eslint-disable strict, no-console*/ +// this example demonstrates how to use the reader/writer interface directly to read and write the +// protobuf wire format. -var protobuf = require("../runtime"); -// protobuf = require("protobufjs/runtime"); +/*eslint-disable strict, no-console*/ +var protobuf = require("../runtime"); // require("protobufjs/runtime"); // writing var buffer = protobuf.Writer.create() @@ -21,4 +22,4 @@ while (reader.pos < reader.len) { reader.skipType(/*wireType*/ tag & 7); break; } -} \ No newline at end of file +} diff --git a/examples/streaming-rpc.js b/examples/streaming-rpc.js index fd4c65181..f3c0c2317 100644 --- a/examples/streaming-rpc.js +++ b/examples/streaming-rpc.js @@ -1,5 +1,6 @@ -/*eslint-disable strict, no-console*/ +// this example demonstrates how to implement streaming rpc for services. +/*eslint-disable strict, no-console*/ var protobuf = require(".."); // Load a definition with services: diff --git a/examples/traverse-types.js b/examples/traverse-types.js new file mode 100644 index 000000000..e4dbea444 --- /dev/null +++ b/examples/traverse-types.js @@ -0,0 +1,33 @@ +// 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"); + +var proto = "syntax=\"proto3\";\ +package example;\ +message Foo {\ + string a = 1;\ +}\ +message Bar {\ + uint32 b = 1;\ + \ + message Inner {\ + bytes c = 1;\ + }\ +}"; + +function traverseTypes(current, fn) { + if (current instanceof protobuf.Type) + 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); +});