-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.ts
211 lines (192 loc) · 4.91 KB
/
index.ts
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
* Matches a JSON object.
*/
export type JsonObject = {[key: string]: JsonValue};
/**
* Matches a JSON array.
*/
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface JsonArray extends Array<JsonValue> {}
/**
* Matches any valid JSON value.
*/
export type JsonValue = string|number|boolean|null|JsonObject|JsonArray;
/**
* @typedef {Object} Value
* @property {string} kind The kind of value. Valid values for this fields are
* - `nullValue`
* - `numberValue`
* - `stringValue`
* - `boolValue`
* - `structValue`
* - `listValue`
* @property {number} [nullValue] Represents a null value, actual field value
* should be `0`.
* @property {number} [numberValue] Represents a number.
* @property {string} [stringValue] Represents a string.
* @property {boolean} [boolValue] Represents a boolean.
* @property {Struct} [structValue] Represents an object.
* @property {ListValue} [listValue] Represents an array of values.
*/
export interface Value {
kind?: string;
nullValue?: number;
numberValue?: number;
stringValue?: string;
boolValue?: boolean;
structValue?: Struct;
listValue?: ListValue;
}
/**
* @typedef {Object} Struct
* @property {Object.<string, Value>} fields The struct fields.
*/
export interface Struct {
fields?: {[key: string]: Value};
}
/**
* @typedef {Object} ListValue
* @property {Value[]} values The list values.
*/
export interface ListValue {
values?: Value[];
}
/**
* Valid `kind` types
*/
enum Kind {
Struct = 'structValue',
List = 'listValue',
Number = 'numberValue',
String = 'stringValue',
Bool = 'boolValue',
Null = 'nullValue'
}
const toString = Object.prototype.toString;
const encoders = {
[typeOf({})]: v => wrap(Kind.Struct, struct.encode(v)),
[typeOf([])]: v => wrap(Kind.List, list.encode(v)),
[typeOf(0)]: v => wrap(Kind.Number, v),
[typeOf('')]: v => wrap(Kind.String, v),
[typeOf(true)]: v => wrap(Kind.Bool, v),
[typeOf(null)]: () => wrap(Kind.Null, 0)
};
function typeOf(value: JsonValue): string {
return toString.call(value);
}
function wrap(kind: Kind, value): Value {
return {kind, [kind]: value};
}
function getKind(value: Value): string | null {
if (value.kind) {
return value.kind;
}
const validKinds = Object.values(Kind);
for (const kind of validKinds) {
if (value.hasOwnProperty(kind)) {
return kind;
}
}
return null;
}
/**
* Used to encode/decode {@link Value} objects.
*/
export const value = {
/**
* Encodes a JSON value into a protobuf {@link Value}.
*
* @param {*} value The JSON value.
* @returns {Value}
*/
encode(value: JsonValue): Value {
const type = typeOf(value);
const 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(value: Value): JsonValue {
const kind = getKind(value);
if (!kind) {
throw new TypeError(`Unable to determine kind for "${value}".`);
}
switch(kind) {
case 'listValue':
return list.decode(value.listValue);
case 'structValue':
return struct.decode(value.structValue);
case 'nullValue':
return null;
default:
return value[kind] as JsonValue;
}
}
};
/**
* Used to encode/decode {@link Struct} objects.
*/
export const struct = {
/**
* Encodes a JSON object into a protobuf {@link Struct}.
*
* @param {Object.<string, *>} value the JSON object.
* @returns {Struct}
*/
encode(json: JsonObject): Struct {
const fields = {};
Object.keys(json).forEach(key => {
// If value is undefined, do not encode it.
if (typeof json[key] === 'undefined') return;
fields[key] = value.encode(json[key]);
});
return {fields};
},
/**
* Decodes a protobuf {@link Struct} into a JSON object.
*
* @param {Struct} struct the protobuf struct.
* @returns {Object.<string, *>}
*/
decode({fields = {}}: Struct): JsonObject {
const json = {};
Object.keys(fields).forEach(key => {
json[key] = value.decode(fields[key]);
});
return json;
}
};
/**
* Used to encode/decode {@link ListValue} objects.
*/
export const list = {
/**
* Encodes an array of JSON values into a protobuf {@link ListValue}.
*
* @param {Array.<*>} values the JSON values.
* @returns {ListValue}
*/
encode(values: JsonArray): ListValue {
return {
values: values.map(value.encode)
};
},
/**
* Decodes a protobuf {@link ListValue} into an array of JSON values.
*
* @param {ListValue} list the protobuf list value.
* @returns {Array.<*>}
*/
decode({values = []}: ListValue): JsonArray {
return values.map(value.decode);
}
};