Skip to content

Commit

Permalink
Post-merge; Reader/Writer example
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 15, 2016
1 parent 8d7bb1f commit c079c90
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 21 deletions.
27 changes: 27 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down
23 changes: 16 additions & 7 deletions dist/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/runtime/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/runtime/protobuf.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified dist/runtime/protobuf.min.js.gz
Binary file not shown.
20 changes: 20 additions & 0 deletions examples/reader-writer.js
@@ -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;
}
}
18 changes: 12 additions & 6 deletions src/field.js
Expand Up @@ -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)}
Expand Down Expand Up @@ -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;
};
8 changes: 7 additions & 1 deletion types/protobuf.js.d.ts
@@ -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" {

/**
Expand Down Expand Up @@ -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)}
Expand Down

0 comments on commit c079c90

Please sign in to comment.