Skip to content

Commit

Permalink
Improved fromObject wrapper for google.protobuf.Any.
Browse files Browse the repository at this point in the history
Now fromObject() can read back objects created with toObject()
containing google.protobuf.Any messages.
  • Loading branch information
manni83 committed Apr 20, 2017
1 parent 0e471a2 commit 8f7111c
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 8f7111c

Please sign in to comment.