Permalink
Browse files

Post-merge; Reader/Writer example

  • Loading branch information...
dcodeIO committed Dec 15, 2016
1 parent 8d7bb1f commit c079c900e2d61c63d5508eafacbd00163d377482
@@ -166,6 +166,33 @@ var message = new AwesomeMessage({ awesomeField: "AwesomeString" });
Custom classes are automatically populated with static `encode`, `encodeDelimited`, `decode`, `decodeDelimited` and `verify` methods and reference their reflected type via the `$type` property. Note that there are no methods (just `$type`) on instances by default as method names might conflict with field names.
### Using the Reader/Writer interface directly
While meant for the adventurous, it's also possible to use the Reader/Writer interface directly to build custom encoders and decoders that work accross modern to ancient browsers and, of course, node:
```js
var writer = protobuf.Writer.create();
var buffer = writer
.int32(/* id */ 1 << 3 | /* wireType */ 2)
.string("hello world!")
.finish();
var reader = protobuf.Reader.create(buffer);
while (reader.pos < reader.len) {
var tag = reader.int32();
switch (/* id */ tag >>> 3) {
case 1:
console.log(reader.string());
break;
default:
reader.skipType(/* wireType */ tag & 7);
break;
}
}
```
You can take pretty much any generated code snippet as a reference. Easy ways to obtain these are either setting `protobuf.util.codegen.verbose = true` while watching the magic as it happens, or simply inspecting [generated static code](https://github.com/dcodeIO/protobuf.js#command-line).
### Using services
```protobuf

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN +52 Bytes (100%) dist/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
BIN +0 Bytes (100%) dist/runtime/protobuf.min.js.gz
Binary file not shown.
@@ -0,0 +1,20 @@
var protobuf = require("..");
var writer = protobuf.Writer.create();
var buffer = writer
.int32(1 << 3 | 2) // id 1, wireType 2
.string("hello world!")
.finish();
var reader = protobuf.Reader.create(buffer);
while (reader.pos < reader.len) {
var tag = reader.int32();
switch (tag>>>3) {
case 1:
console.log(reader.string());
break;
default:
reader.skipType(tag&7);
break;
}
}
@@ -119,6 +119,12 @@ function Field(name, id, type, rule, extend, options) {
*/
this.long = util.Long ? types.long[type] !== undefined : false;
/**
* Whether this field's value is a buffer.
* @type {boolean}
*/
this.bytes = type === "bytes";
/**
* Resolved type if not a basic type.
* @type {?(Type|Enum)}
@@ -268,20 +274,20 @@ FieldPrototype.resolve = function resolve() {
*/
FieldPrototype.jsonConvert = function(value, options) {
if (options) {
if (value instanceof Message)
return value.asJSON(options);
if (this.resolvedType instanceof Enum && options["enum"] === String) // eslint-disable-line dot-notation
return this.resolvedType.getValuesById()[value];
else if (this.long && options.long)
if (options.long && this.long)
return options.long === Number
? typeof value === "number"
? value
: util.Long.fromValue(value).toNumber()
? value
: util.LongBits.from(value).toNumber(this.type.charAt(0) === "u")
: util.Long.fromValue(value, this.type.charAt(0) === "u").toString();
else if (options.bytes && this.type === "bytes")
if (options.bytes && this.bytes)
return options.bytes === Array
? Array.prototype.slice.call(value)
: util.base64.encode(value, 0, value.length);
else if(value instanceof Message)
return value.asJSON(options);
}
return value;
};
@@ -1,5 +1,5 @@
// $> pbts --name protobufjs --out types/protobuf.js.d.ts src
// Generated Thu, 15 Dec 2016 16:49:02 UTC
// Generated Thu, 15 Dec 2016 17:47:18 UTC
declare module "protobufjs" {
/**
@@ -306,6 +306,12 @@ declare module "protobufjs" {
*/
long: boolean;
/**
* Whether this field's value is a buffer.
* @type {boolean}
*/
bytes: boolean;
/**
* Resolved type if not a basic type.
* @type {?(Type|Enum)}

0 comments on commit c079c90

Please sign in to comment.