Skip to content

Commit

Permalink
New: Generate named constructors for runtime messages, see #588
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Jan 3, 2017
1 parent 196fdc0 commit cba46c3
Show file tree
Hide file tree
Showing 18 changed files with 90 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ npm-debug.*
node_modules/
docs/
coverage/
sandbox/
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ npm-debug.*
node_modules/
docs/
coverage/
sandbox/
examples/
src/util/aspromise/
src/util/base64/
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ protobuf.js

**Protocol Buffers** are a language-neutral, platform-neutral, extensible way of serializing structured data for use in communications protocols, data storage, and more, originally designed at Google ([see](https://developers.google.com/protocol-buffers/)).

**protobuf.js** is a pure JavaScript implementation for node and the browser. It efficiently encodes plain objects and custom classes and works out of the box with .proto files.
**protobuf.js** is a pure JavaScript implementation with TypeScript support for node and the browser. It efficiently encodes plain objects and custom classes and works out of the box with .proto files.

[travis-image]: https://img.shields.io/travis/dcodeIO/protobuf.js.svg
[travis-url]: https://travis-ci.org/dcodeIO/protobuf.js
Expand Down
31 changes: 24 additions & 7 deletions dist/noparse/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/noparse/protobuf.js.map

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/noparse/protobuf.min.js

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

31 changes: 24 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.

8 changes: 4 additions & 4 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 dist/runtime/protobuf.js

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

4 changes: 2 additions & 2 deletions 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.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "protobufjs",
"version": "6.4.0",
"version": "6.4.1",
"description": "Protocol Buffers for JavaScript (& TypeScript).",
"author": "Daniel Wirtz <dcode+protobufjs@dcode.io>",
"license": "BSD-3-Clause",
Expand Down
27 changes: 22 additions & 5 deletions src/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ function create(type, ctor) {
if (typeof ctor !== "function")
throw TypeError("ctor must be a function");
} else
ctor = (function(MessageCtor) {
return function Message(properties) {
MessageCtor.call(this, properties);
};
})(Message);
ctor = util.codegen("p")("return ctor.call(this,p)").eof(type.name, {
ctor: Message
});

// Let's pretend...
ctor.constructor = Class;
Expand Down Expand Up @@ -97,6 +95,15 @@ Class.create = create;
// Static methods on Message are instance methods on Class and vice versa.
Class.prototype = Message;

/**
* Creates a message from a JSON object by converting strings and numbers to their respective field types.
* @name Class#from
* @function
* @param {Object.<string,*>} object JSON object
* @param {MessageConversionOptions} [options] Options
* @returns {Message} Message instance
*/

/**
* Encodes a message of this type.
* @name Class#encode
Expand Down Expand Up @@ -138,3 +145,13 @@ Class.prototype = Message;
* @param {Message|Object} message Message or plain object to verify
* @returns {?string} `null` if valid, otherwise the reason why it is not
*/

/**
* Converts an object or runtime message of this type.
* @name Class#convert
* @function
* @param {Message|Object} source Source object or runtime message
* @param {ConverterImpl} impl Converter implementation to use, i.e. {@link converters.json} or {@link converters.message}
* @param {Object.<string,*>} [options] Conversion options
* @returns {Message|Object} Converted object or runtime message
*/

0 comments on commit cba46c3

Please sign in to comment.