Skip to content

Commit 367d555

Browse files
committed
Other: Traversal example + minimalistic documentation
1 parent 7a94453 commit 367d555

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

examples/custom-get-set.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ message MyMessage {\
1111

1212
var root = protobuf.parse(proto, { keepCase: true }).root; // or use Root#load
1313

14+
// converts a string from underscore notation to camel case
1415
function toCamelCase(str) {
1516
return str.substring(0,1) + str.substring(1).replace(/_([a-z])(?=[a-z]|$)/g, function($0, $1) { return $1.toUpperCase(); });
1617
}
1718

19+
// adds a virtual alias property
1820
function addAliasProperty(type, name, aliasName) {
1921
if (aliasName !== name)
2022
Object.defineProperty(type.ctor.prototype, aliasName, {

examples/reader-writer.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
/*eslint-disable strict, no-console*/
1+
// this example demonstrates how to use the reader/writer interface directly to read and write the
2+
// protobuf wire format.
23

3-
var protobuf = require("../runtime");
4-
// protobuf = require("protobufjs/runtime");
4+
/*eslint-disable strict, no-console*/
5+
var protobuf = require("../runtime"); // require("protobufjs/runtime");
56

67
// writing
78
var buffer = protobuf.Writer.create()
@@ -21,4 +22,4 @@ while (reader.pos < reader.len) {
2122
reader.skipType(/*wireType*/ tag & 7);
2223
break;
2324
}
24-
}
25+
}

examples/streaming-rpc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
/*eslint-disable strict, no-console*/
1+
// this example demonstrates how to implement streaming rpc for services.
22

3+
/*eslint-disable strict, no-console*/
34
var protobuf = require("..");
45

56
// Load a definition with services:

examples/traverse-types.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// this example demonstrates how to traverse through a root instance by calling a custom function
2+
// for each message type within.
3+
4+
/*eslint-disable strict, no-console*/
5+
var protobuf = require(".."); // require("protobufjs");
6+
7+
var proto = "syntax=\"proto3\";\
8+
package example;\
9+
message Foo {\
10+
string a = 1;\
11+
}\
12+
message Bar {\
13+
uint32 b = 1;\
14+
\
15+
message Inner {\
16+
bytes c = 1;\
17+
}\
18+
}";
19+
20+
function traverseTypes(current, fn) {
21+
if (current instanceof protobuf.Type)
22+
fn(current);
23+
if (current.nestedArray)
24+
current.nestedArray.forEach(function(nested) {
25+
traverseTypes(nested, fn);
26+
});
27+
}
28+
29+
var root = protobuf.parse(proto).root;
30+
31+
traverseTypes(root, function(type) {
32+
console.log(type.fullName);
33+
});

0 commit comments

Comments
 (0)