-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathstructJson.js
150 lines (150 loc) · 4.28 KB
/
structJson.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
"use strict";
var _a;
exports.__esModule = true;
exports.list = exports.struct = exports.value = void 0;
/**
* Valid `kind` types
*/
var Kind;
(function (Kind) {
Kind["Struct"] = "structValue";
Kind["List"] = "listValue";
Kind["Number"] = "numberValue";
Kind["String"] = "stringValue";
Kind["Bool"] = "boolValue";
Kind["Null"] = "nullValue";
})(Kind || (Kind = {}));
var toString = Object.prototype.toString;
var encoders = (_a = {},
_a[typeOf({})] = function (v) { return wrap(Kind.Struct, exports.struct.encode(v)); },
_a[typeOf([])] = function (v) { return wrap(Kind.List, exports.list.encode(v)); },
_a[typeOf(0)] = function (v) { return wrap(Kind.Number, v); },
_a[typeOf('')] = function (v) { return wrap(Kind.String, v); },
_a[typeOf(true)] = function (v) { return wrap(Kind.Bool, v); },
_a[typeOf(null)] = function () { return wrap(Kind.Null, 0); },
_a);
function typeOf(value) {
return toString.call(value);
}
function wrap(kind, value) {
var _a;
return _a = { kind: kind }, _a[kind] = value, _a;
}
function getKind(value) {
if (value.kind) {
return value.kind;
}
var validKinds = Object.values(Kind);
for (var _i = 0, validKinds_1 = validKinds; _i < validKinds_1.length; _i++) {
var kind = validKinds_1[_i];
if (value.hasOwnProperty(kind)) {
return kind;
}
}
return null;
}
/**
* Used to encode/decode {@link Value} objects.
*/
exports.value = {
/**
* Encodes a JSON value into a protobuf {@link Value}.
*
* @param {*} value The JSON value.
* @returns {Value}
*/
encode: function (value) {
var type = typeOf(value);
var encoder = encoders[type];
if (typeof encoder !== 'function') {
throw new TypeError("Unable to infer type for \"" + value + "\".");
}
return encoder(value);
},
/**
* Decodes a protobuf {@link Value} into a JSON value.
*
* @throws {TypeError} If unable to determine value `kind`.
*
* @param {Value} value the protobuf value.
* @returns {*}
*/
decode: function (value) {
var kind = getKind(value);
if (!kind) {
throw new TypeError("Unable to determine kind for \"" + value + "\".");
}
switch (kind) {
case 'listValue':
return exports.list.decode(value.listValue);
case 'structValue':
return exports.struct.decode(value.structValue);
case 'nullValue':
return null;
default:
return value[kind];
}
}
};
/**
* Used to encode/decode {@link Struct} objects.
*/
exports.struct = {
/**
* Encodes a JSON object into a protobuf {@link Struct}.
*
* @param {Object.<string, *>} value the JSON object.
* @returns {Struct}
*/
encode: function (json) {
var fields = {};
Object.keys(json).forEach(function (key) {
// If value is undefined, do not encode it.
if (typeof json[key] === 'undefined')
return;
fields[key] = exports.value.encode(json[key]);
});
return { fields: fields };
},
/**
* Decodes a protobuf {@link Struct} into a JSON object.
*
* @param {Struct} struct the protobuf struct.
* @returns {Object.<string, *>}
*/
decode: function (_a) {
var fields = _a.fields || {};
var json = {};
Object.keys(fields).forEach(function (key) {
json[key] = exports.value.decode(fields[key]);
});
return json;
}
};
/**
* Used to encode/decode {@link ListValue} objects.
*/
exports.list = {
/**
* Encodes an array of JSON values into a protobuf {@link ListValue}.
*
* @param {Array.<*>} values the JSON values.
* @returns {ListValue}
*/
encode: function (values) {
return {
values: (values || []).map(exports.value.encode)
};
},
/**
* Decodes a protobuf {@link ListValue} into an array of JSON values.
*
* @param {ListValue} list the protobuf list value.
* @returns {Array.<*>}
*/
decode: function (_a) {
var values = _a.values || []
return values.map(exports.value.decode);
}
};
//# sourceMappingURL=index.js.map