Skip to content

Commit

Permalink
Other: Traversal example + minimalistic documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Mar 2, 2017
1 parent 7a94453 commit 367d555
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
2 changes: 2 additions & 0 deletions examples/custom-get-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
9 changes: 5 additions & 4 deletions examples/reader-writer.js
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -21,4 +22,4 @@ while (reader.pos < reader.len) {
reader.skipType(/*wireType*/ tag & 7);
break;
}
}
}
3 changes: 2 additions & 1 deletion examples/streaming-rpc.js
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
33 changes: 33 additions & 0 deletions examples/traverse-types.js
Original file line number Diff line number Diff line change
@@ -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.