Skip to content

Commit

Permalink
Unified proto/reflection/classes/static encoding API to always return…
Browse files Browse the repository at this point in the history
… a writer
  • Loading branch information
dcodeIO committed Dec 4, 2016
1 parent f153d01 commit 09865d0
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 45 deletions.
22 changes: 7 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Features
--------
* Lightning fast through [runtime code generation](#performance)
* Exhaustive [browser support](#compatibility)
* Managed [TypeScript support](#usage-with-typescript)
* Managed [TypeScript definitions](#usage-with-typescript)
* Elaborate [API documentation](#documentation)
* Convenient [CLI utilities](#command-line)
* Seamless [browserify integration](#browserify-integration)
Expand Down Expand Up @@ -102,17 +102,19 @@ protobuf.load("awesome.proto", function(err, root) {
// Create a new message
var message = AwesomeMessage.create({ awesomeField: "AwesomeString" });

// Encode a message (note that reflection encodes to a writer and we need to call finish)
// Encode a message
var buffer = AwesomeMessage.encode(message).finish();
// ... do something with buffer

// Or, encode a plain object (note that reflection encodes to a writer and we need to call finish)
// Or, encode a plain object
var buffer = AwesomeMessage.encode({ awesomeField: "AwesomeString" }).finish();
// ... do something with buffer

// Decode a buffer
var message = AwesomeMessage.decode(buffer);
// ... do something with message

// If your application uses length-delimited buffers, there is also encodeDelimited and decodeDelimited.
});
```

Expand Down Expand Up @@ -154,17 +156,7 @@ protobuf.inherits(AwesomeMessage, root.lookup("awesomepackage.AwesomeMessage") /

var message = new AwesomeMessage({ awesomeField: "AwesomeString" });

// Encode a message (note that classes encode to a buffer directly)
var buffer = AwesomeMessage.encode(message);
// ... do something with buffer

// Or, encode a plain object (note that classes encode to a buffer directly)
var buffer = AwesomeMessage.encode({ awesomeField: "AwesomeString" });
// ... do something with buffer

// Decode a buffer
var message = AwesomeMessage.decode(buffer);
// ... do something with message
// Continue at "Encode a message" above
```

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.
Expand Down Expand Up @@ -358,7 +350,7 @@ $> npm run types

### Browserify integration

protobuf.js integrates nicely into your browserify build-process. There are a few possible tweaks, though:
protobuf.js integrates into any browserify build-process. There are a few possible tweaks:

* If performance is a concern or IE8 support is required, you should make sure to exclude the browserified `buffer` module and let protobuf.js do its thing with Uint8Array/Array instead.
* If you do not need int64 support, you can exclude the `long` module.
Expand Down
4 changes: 2 additions & 2 deletions cli/targets/proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ function buildRoot(root) {
} else
break;
} while (true);
if (pkg.length)
out.push("package " + pkg.join(".") + ";", "");
out.push('syntax = "proto' + syntax + '";');
if (pkg.length)
out.push("", "package " + pkg.join(".") + ";");

buildOptions(ptr);
ptr.nestedArray.forEach(build);
Expand Down
10 changes: 5 additions & 5 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.

4 changes: 2 additions & 2 deletions google/protobuf/descriptor.proto
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package google.protobuf;

syntax = "proto2";

package google.protobuf;

message FileDescriptorSet {

repeated FileDescriptorProto file = 1;
Expand Down
13 changes: 3 additions & 10 deletions runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,11 @@
*/
var runtime = exports;

/**
* @alias Reader
*/
/** @alias Reader */
runtime.Reader = require("./src/reader");

/**
* @alias Writer
*/
/** @alias Writer */
runtime.Writer = require("./src/writer");

/**
* Runtime utility.
* @memberof runtime
*/
/** @alias util */
runtime.util = require("./src/util/runtime");
8 changes: 4 additions & 4 deletions src/inherits.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ function inherits(clazz, type, options) {
* @function
* @param {Prototype|Object} message Message to encode
* @param {Writer} [writer] Writer to use
* @returns {Uint8Array} Encoded message
* @returns {Writer} Writer
*/
encode: {
value: function encode(message, writer) {
return this.$type.encode(message, writer).finish();
return this.$type.encode(message, writer);
}
},

Expand All @@ -75,11 +75,11 @@ function inherits(clazz, type, options) {
* @function
* @param {Prototype|Object} message Message to encode
* @param {Writer} [writer] Writer to use
* @returns {Uint8Array} Encoded message
* @returns {Writer} Writer
*/
encodeDelimited: {
value: function encodeDelimited(message, writer) {
return this.$type.encodeDelimited(message, writer).finish();
return this.$type.encodeDelimited(message, writer);
}
},

Expand Down
4 changes: 2 additions & 2 deletions tests/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ tape.test("google.protobuf.Any class", function(test) {

test.test("should encode", function(test) {

buf = Any.encode(any);
buf = Any.encode(any).finish();

test.equal(buf[0] , 1 << 3 | 2, "a tag with id 1, wire type 2");
test.equal(buf[1] , 9 , "a field length of 9");
Expand All @@ -55,7 +55,7 @@ tape.test("google.protobuf.Any class", function(test) {

test.test("should encodeDelimited", function(test) {

buf = Any.encodeDelimited(any);
buf = Any.encodeDelimited(any).finish();

test.equal(buf[0] , 13 , "a length of 13");
test.equal(buf[1] , 1 << 3 | 2, "a tag with id 1, wire type 2");
Expand Down

0 comments on commit 09865d0

Please sign in to comment.