/
comp_google_protobuf_any.js
66 lines (56 loc) · 2.01 KB
/
comp_google_protobuf_any.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
var tape = require("tape");
var protobuf = require("..");
var root = protobuf.Root.fromJSON({
nested: {
Foo: {
fields: {
foo: {
id: 1,
type: "google.protobuf.Any"
}
}
},
Bar: {
fields: {
bar: {
id: 1,
type: "string"
}
}
}
}
}).addJSON(protobuf.common["google/protobuf/any.proto"].nested).resolveAll();
var Any = root.lookupType("protobuf.Any"),
Foo = root.lookupType(".Foo"),
Bar = root.lookupType(".Bar");
tape.test("google.protobuf.Any", function(test) {
var foo = Foo.fromObject({
foo: {
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");
var obj = Foo.toObject(foo);
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": "type.googleapis.com/Bar", bar: "a" }, "should decode explicitly Any in toObject if requested");
foo = Foo.fromObject({
foo: {
"@type": ".Bar",
bar: "a"
}
});
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");
var baz = Foo.fromObject({
foo: {
type_url: "type.someurl.com/Bar",
value: [1 << 3 | 2, 1, 97] // value = "a"
}
});
obj = Foo.toObject(baz, { json: true });
test.same(obj.foo, { "@type": "type.someurl.com/Bar", bar: "a" }, "should keep prefix in type url");
test.end();
});