Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved fromObject wrapper for google.protobuf.Any. #768

Merged
merged 1 commit into from
Apr 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});