Skip to content

Commit

Permalink
Merge pull request #768 from manni83/protobuf-any-wrapper
Browse files Browse the repository at this point in the history
Improved fromObject wrapper for google.protobuf.Any.
  • Loading branch information
dcodeIO authored Apr 20, 2017
2 parents 0e471a2 + 8f7111c commit b596607
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/wrappers.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ wrappers[".google.protobuf.Any"] = {
if (object && object["@type"]) {
var type = this.lookup(object["@type"]);
/* istanbul ignore else */
if (type)
return type.fromObject(object);
if (type) {
var obj = this.create({
type_url: object["@type"],
value: type.encode(object).finish()
});
return obj;
}
}

return this.fromObject(object);
Expand Down
7 changes: 2 additions & 5 deletions tests/comp_google_protobuf_any.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,8 @@ tape.test("google.protobuf.Any", function(test) {
bar: "a"
}
});
test.ok(foo.foo instanceof Bar.ctor, "should unwrap wrapped Bar in fromObject");
test.same(foo.foo, { bar: "a" }, "should unwrap wrapper Bar in fromObject properly");

obj = Foo.toObject(foo);
test.same(obj.foo, { "@type": ".Bar", bar: "a" }, "should wrap Bar in toObject properly");
test.ok(foo.foo instanceof Any.ctor, "should convert to Any in fromObject");
test.same(foo.foo, { type_url: ".Bar", value: [10, 1, 97] }, "should have correct Any object when converted with fromObject");

test.end();
});

0 comments on commit b596607

Please sign in to comment.