Skip to content

Commit

Permalink
Other: In fromObject, check if object is already a runtime message, see
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Jan 19, 2017
1 parent ea3f3b4 commit 799d030
Show file tree
Hide file tree
Showing 24 changed files with 194 additions and 47 deletions.
12 changes: 6 additions & 6 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,12 @@ function beautifyCode(code) {

function buildFunction(type, functionName, gen, scope) {
var code = gen.str(functionName)
.replace(/\(this.ctor\)/g, " $root" + type.fullName) // types: construct directly instead of using reflected ctor
.replace(/(types\[\d+])(\.values)/g, "$1") // enums: use types[N] instead of reflected types[N].values
.replace(/\b(?!\.)Writer\b/g, "$Writer") // use common aliases instead of binding through an iife
.replace(/\b(?!\.)Reader\b/g, "$Reader") // "
.replace(/\b(?!\.)util\.\b/g, "$util.") // "
.replace(/\b(?!\.)types\[\b/g, "$types["); // "
.replace(/this\.ctor/g, " $root" + type.fullName) // types: construct directly instead of using reflected ctor
.replace(/(types\[\d+])(\.values)/g, "$1") // enums: use types[N] instead of reflected types[N].values
.replace(/\b(?!\.)Writer\b/g, "$Writer") // use common aliases instead of binding through an iife
.replace(/\b(?!\.)Reader\b/g, "$Reader") // "
.replace(/\b(?!\.)util\.\b/g, "$util.") // "
.replace(/\b(?!\.)types\[\b/g, "$types["); // "

if (config.beautify)
code = beautifyCode(code);
Expand Down
15 changes: 9 additions & 6 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.

6 changes: 3 additions & 3 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.

15 changes: 9 additions & 6 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.5.2",
"version": "6.5.3",
"versionScheme": "~",
"description": "Protocol Buffers for JavaScript (& TypeScript).",
"author": "Daniel Wirtz <[email protected]>",
Expand Down
9 changes: 6 additions & 3 deletions src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,13 @@ function genValuePartial_fromObject(gen, field, fieldIndex, prop) {
converter.fromObject = function fromObject(mtype) {
/* eslint-disable no-unexpected-multiline, block-scoped-var, no-redeclare */
var fields = mtype.fieldsArray;
if (!fields.length)
return util.codegen()("return new(this.ctor)");
var gen = util.codegen("d")
("var m=new(this.ctor)");
("if(d instanceof this.ctor)")
("return d");
if (!fields.length) return gen
("return new this.ctor");
gen
("var m=new this.ctor");
for (var i = 0; i < fields.length; ++i) {
var field = fields[i].resolve(),
prop = util.safeProp(field.name);
Expand Down
2 changes: 1 addition & 1 deletion src/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function decoder(mtype) {
var gen = util.codegen("r", "l")
("if(!(r instanceof Reader))")
("r=Reader.create(r)")
("var c=l===undefined?r.len:r.pos+l,m=new(this.ctor)")
("var c=l===undefined?r.len:r.pos+l,m=new this.ctor")
("while(r.pos<c){")
("var t=r.uint32()");
if (mtype.group) gen
Expand Down
2 changes: 2 additions & 0 deletions tests/comp_typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ var buf = Hello.encode(hello.foo()).finish();

var hello2 = Hello.decode(buf) as Hello;
console.log(hello2.foo().toObject());

export var utf8 = protobuf.util.utf8;
6 changes: 5 additions & 1 deletion tests/data/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ $root.Test1 = (function() {
* @returns {Test1} Test1
*/
Test1.fromObject = function fromObject(object) {
if (object instanceof $root.Test1)
return object;
var message = new $root.Test1();
if (object.field1 !== undefined && object.field1 !== null)
message.field1 = String(object.field1);
Expand Down Expand Up @@ -309,7 +311,9 @@ $root.Test2 = (function() {
* @param {Object.<string,*>} object Plain object
* @returns {Test2} Test2
*/
Test2.fromObject = function fromObject() {
Test2.fromObject = function fromObject(object) {
if (object instanceof $root.Test2)
return object;
return new $root.Test2();
};

Expand Down
2 changes: 2 additions & 0 deletions tests/data/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ $root.Message = (function() {
* @returns {Message} Message
*/
Message.fromObject = function fromObject(object) {
if (object instanceof $root.Message)
return object;
var message = new $root.Message();
if (object.stringVal !== undefined && object.stringVal !== null)
message.stringVal = String(object.stringVal);
Expand Down
8 changes: 8 additions & 0 deletions tests/data/mapbox/vector_tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ $root.vector_tile = (function() {
* @returns {vector_tile.Tile} Tile
*/
Tile.fromObject = function fromObject(object) {
if (object instanceof $root.vector_tile.Tile)
return object;
var message = new $root.vector_tile.Tile();
if (object.layers) {
message.layers = [];
Expand Down Expand Up @@ -411,6 +413,8 @@ $root.vector_tile = (function() {
* @returns {vector_tile.Tile.Value} Value
*/
Value.fromObject = function fromObject(object) {
if (object instanceof $root.vector_tile.Tile.Value)
return object;
var message = new $root.vector_tile.Tile.Value();
if (object.stringValue !== undefined && object.stringValue !== null)
message.stringValue = String(object.stringValue);
Expand Down Expand Up @@ -727,6 +731,8 @@ $root.vector_tile = (function() {
* @returns {vector_tile.Tile.Feature} Feature
*/
Feature.fromObject = function fromObject(object) {
if (object instanceof $root.vector_tile.Tile.Feature)
return object;
var message = new $root.vector_tile.Tile.Feature();
if (object.id !== undefined && object.id !== null)
if ($util.Long)
Expand Down Expand Up @@ -1043,6 +1049,8 @@ $root.vector_tile = (function() {
* @returns {vector_tile.Tile.Layer} Layer
*/
Layer.fromObject = function fromObject(object) {
if (object instanceof $root.vector_tile.Tile.Layer)
return object;
var message = new $root.vector_tile.Tile.Layer();
if (object.version !== undefined && object.version !== null)
message.version = object.version >>> 0;
Expand Down
4 changes: 4 additions & 0 deletions tests/data/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,8 @@ $root.Package = (function() {
* @returns {Package} Package
*/
Package.fromObject = function fromObject(object) {
if (object instanceof $root.Package)
return object;
var message = new $root.Package();
if (object.name !== undefined && object.name !== null)
message.name = String(object.name);
Expand Down Expand Up @@ -738,6 +740,8 @@ $root.Package = (function() {
* @returns {Package.Repository} Repository
*/
Repository.fromObject = function fromObject(object) {
if (object instanceof $root.Package.Repository)
return object;
var message = new $root.Package.Repository();
if (object.type !== undefined && object.type !== null)
message.type = String(object.type);
Expand Down
4 changes: 4 additions & 0 deletions tests/data/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ $root.MyRequest = (function() {
* @returns {MyRequest} MyRequest
*/
MyRequest.fromObject = function fromObject(object) {
if (object instanceof $root.MyRequest)
return object;
var message = new $root.MyRequest();
if (object.path !== undefined && object.path !== null)
message.path = String(object.path);
Expand Down Expand Up @@ -373,6 +375,8 @@ $root.MyResponse = (function() {
* @returns {MyResponse} MyResponse
*/
MyResponse.fromObject = function fromObject(object) {
if (object instanceof $root.MyResponse)
return object;
var message = new $root.MyResponse();
if (object.status !== undefined && object.status !== null)
message.status = object.status | 0;
Expand Down
Loading

0 comments on commit 799d030

Please sign in to comment.