-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathwrappers.js
170 lines (145 loc) · 5.36 KB
/
wrappers.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"use strict";
/**
* Wrappers for common types.
* @type {Object.<string,IWrapper>}
* @const
*/
var wrappers = exports;
var Message = require("./message");
/**
* From object converter part of an {@link IWrapper}.
* @typedef WrapperFromObjectConverter
* @type {function}
* @param {Object.<string,*>} object Plain object
* @returns {Message<{}>} Message instance
* @this Type
*/
/**
* To object converter part of an {@link IWrapper}.
* @typedef WrapperToObjectConverter
* @type {function}
* @param {Message<{}>} message Message instance
* @param {IConversionOptions} [options] Conversion options
* @returns {Object.<string,*>} Plain object
* @this Type
*/
/**
* Common type wrapper part of {@link wrappers}.
* @interface IWrapper
* @property {WrapperFromObjectConverter} [fromObject] From object converter
* @property {WrapperToObjectConverter} [toObject] To object converter
*/
// Custom wrapper for Any
wrappers[".google.protobuf.Any"] = {
fromObject: function(object) {
// unwrap value type if mapped
if (object && object["@type"]) {
var type = this.lookup(object["@type"]);
/* istanbul ignore else */
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: type_url,
value: type.encode(type.fromObject(object)).finish()
});
}
}
return this.fromObject(object);
},
toObject: function(message, options) {
// decode value if requested and unmapped
if (options && options.json && message.type_url && message.value) {
var type = this.lookup(message.type_url);
/* istanbul ignore else */
if (type)
message = type.decode(message.value);
}
// wrap value if unmapped
if (!(message instanceof this.ctor) && message instanceof Message) {
var object = message.$type.toObject(message, options);
object["@type"] = message.$type.fullName;
return object;
}
return this.toObject(message, options);
}
};
// Custom wrapper for ListValue
wrappers[".google.protobuf.ListValue"] = {
fromObject: function(object) {
var Value = this.lookup("google.protobuf.Value");
return this.create({values: object.map(Value.fromObject)});
},
toObject: function(message /*, options */) {
var Value = this.lookup("google.protobuf.Value");
return message.values.map(Value.toObject);
}
};
// Custom wrapper for Value
wrappers[".google.protobuf.Value"] = {
// given a plain javascript scalar or object, return a protobuf Value
fromObject: function(object) {
var Struct = this.lookup("google.protobuf.Struct");
var NullValue = this.lookup("google.protobuf.NullValue");
var ListValue = this.lookup("google.protobuf.ListValue");
var valueDef;
if (object === null) {
valueDef = {nullValue: NullValue.values.NULL_VALUE};
} else if (typeof object === "number") {
valueDef = {numberValue: object};
} else if (typeof object === "string") {
valueDef = {stringValue: object};
} else if (typeof object === "boolean") {
valueDef = {boolValue: object};
} else if (Array.isArray(object)) {
valueDef = {listValue: ListValue.fromObject(object)};
} else if (typeof object === "object") {
valueDef = {structValue: Struct.fromObject(object)};
} else {
return valueDef = {nullValue: 0};
}
return this.create(valueDef);
},
toObject: function(message, options) {
var Struct = this.lookup("google.protobuf.Struct");
var ListValue = this.lookup("google.protobuf.ListValue");
var object;
if (message.kind === "nullValue") {
object = null;
} else if (message.kind === "numberValue") {
object = message.numberValue;
} else if (message.kind === "stringValue") {
object = message.stringValue;
} else if (message.kind === "boolValue") {
object = message.boolValue;
} else if (message.kind === "structValue") {
object = Struct.toObject(message.structValue, options);
} else if (message.kind === "listValue") {
object = ListValue.toObject(message.listValue, options);
}
return object;
}
};
// Custom wrapper for Struct
wrappers[".google.protobuf.Struct"] = {
// given a plain javascript object, return a protobuf Struct object
fromObject: function(object) {
var Value = this.lookup("google.protobuf.Value");
var structDef = {fields: {}};
Object.keys(object).forEach(function (k) {
structDef.fields[k] = Value.fromObject(object[k]);
});
return this.create(structDef);
},
// given a protobuf Struct object, return a plain JS object
toObject: function(message, options) {
var Value = this.lookup("google.protobuf.Value");
var object = {};
var fields = message.fields;
Object.keys(fields).forEach(function (k) {
object[k] = Value.toObject(fields[k], options);
});
return object;
}
};