Skip to content

Commit

Permalink
Merge pull request #831 from manni83/protobuf-any-wrapper2
Browse files Browse the repository at this point in the history
Fixes for the Protobuf any wrapper
  • Loading branch information
dcodeIO authored Jun 13, 2017
2 parents 8c6a597 + c712447 commit e8dc6ce
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
10 changes: 7 additions & 3 deletions src/wrappers.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,15 @@ wrappers[".google.protobuf.Any"] = {
if (object && object["@type"]) {
var type = this.lookup(object["@type"]);
/* istanbul ignore else */
if (type)
if (type) {
// type_url does not accept leading "."
var type_url = object["@type"].charAt(0) === "." ?
object["@type"].substr(1) : object["@type"];
return this.create({
type_url: object["@type"],
value: type.encode(object).finish()
type_url: type_url,
value: type.encode(type.fromObject(object)).finish()
});
}
}

return this.fromObject(object);
Expand Down
8 changes: 4 additions & 4 deletions tests/comp_google_protobuf_any.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ tape.test("google.protobuf.Any", function(test) {

var foo = Foo.fromObject({
foo: {
type_url: ".Bar",
type_url: "Bar",
value: [1 << 3 | 2, 1, 97] // value = "a"
}
});
test.ok(foo.foo instanceof Any.ctor, "should keep explicit Any in fromObject");
test.same(foo.foo, { type_url: ".Bar", value: [10, 1, 97] }, "should keep explicit Any in fromObject properly");
test.same(foo.foo, { type_url: "Bar", value: [10, 1, 97] }, "should keep explicit Any in fromObject properly");

var obj = Foo.toObject(foo);
test.same(obj.foo, { type_url: ".Bar", value: [10, 1, 97] }, "should keep explicit Any in toObject properly");
test.same(obj.foo, { type_url: "Bar", value: [10, 1, 97] }, "should keep explicit Any in toObject properly");

obj = Foo.toObject(foo, { json: true });
test.same(obj.foo, { "@type": ".Bar", bar: "a" }, "should decode explicitly Any in toObject if requested");
Expand All @@ -51,7 +51,7 @@ tape.test("google.protobuf.Any", function(test) {
}
});
test.ok(foo.foo instanceof Any.ctor, "should convert to Any in fromObject");
test.same(foo.foo, { type_url: ".Bar", value: protobuf.util.newBuffer([10, 1, 97]) }, "should have correct Any object when converted with fromObject");
test.same(foo.foo, { type_url: "Bar", value: protobuf.util.newBuffer([10, 1, 97]) }, "should have correct Any object when converted with fromObject");

test.end();
});

0 comments on commit e8dc6ce

Please sign in to comment.