Permalink
Browse files

Other: Traversal example + minimalistic documentation

  • Loading branch information...
dcodeIO committed Mar 2, 2017
1 parent 7a94453 commit 367d55523a3ae88f21d47aa96447ec3e943d4620
Showing with 42 additions and 5 deletions.
  1. +2 −0 examples/custom-get-set.js
  2. +5 −4 examples/reader-writer.js
  3. +2 −1 examples/streaming-rpc.js
  4. +33 −0 examples/traverse-types.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, {
@@ -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;
}
}
}
@@ -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:
@@ -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);
});

0 comments on commit 367d555

Please sign in to comment.