-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathcomp_google_protobuf_wrappers.js
More file actions
67 lines (56 loc) · 1.85 KB
/
Copy pathcomp_google_protobuf_wrappers.js
File metadata and controls
67 lines (56 loc) · 1.85 KB
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
67
var tape = require("tape");
var protobuf = require("..");
var root = new protobuf.Root().addJSON(protobuf.common["google/protobuf/wrappers.proto"].nested).addJSON({
Foo: {
fields: {
stringValue: {
id: 1,
type: "google.protobuf.StringValue"
},
int64Value: {
id: 2,
type: "google.protobuf.Int64Value"
},
boolValue: {
id: 3,
type: "google.protobuf.BoolValue"
},
bytesValue: {
id: 4,
type: "google.protobuf.BytesValue"
}
}
}
}).resolveAll();
var Foo = root.lookupType("Foo"),
StringValue = root.lookupType("google.protobuf.StringValue");
tape.test("google.protobuf wrapper types", function(test) {
var foo = Foo.fromObject({
stringValue: "abc",
int64Value: "123",
boolValue: false,
bytesValue: "AQI="
});
test.same(Foo.toObject(foo, { json: true, bytes: String, longs: String }), {
stringValue: "abc",
int64Value: "123",
boolValue: false,
bytesValue: "AQI="
}, "should convert wrapper fields using their JSON scalar representation");
foo = Foo.fromObject({
stringValue: {
value: "abc"
}
});
test.same(Foo.toObject(foo), {
stringValue: {
value: "abc"
}
}, "should preserve the existing object representation without json conversion");
var stringValue = StringValue.fromObject("abc");
test.same(StringValue.toObject(stringValue), {
value: "abc"
}, "should preserve direct wrapper objects without json conversion");
test.equal(StringValue.toObject(stringValue, { json: true }), "abc", "should unwrap direct wrapper objects with json conversion");
test.end();
});