-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathcodec.js
276 lines (226 loc) · 5.55 KB
/
codec.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*!
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
var codec = module.exports;
var Buffer = require('safe-buffer').Buffer;
var commonGrpc = require('@google-cloud/common-grpc');
var is = require('is');
function SpannerDate(value) {
if (arguments.length > 1) {
throw new TypeError(
[
'The spanner.date function accepts a Date object or a',
"single argument parseable by Date's constructor.",
].join(' ')
);
}
if (is.undefined(value)) {
value = new Date();
}
this.value = new Date(value).toJSON().replace(/T.+/, '');
}
codec.SpannerDate = SpannerDate;
function Float(value) {
this.value = value;
}
Float.prototype.valueOf = function() {
return parseFloat(this.value);
};
codec.Float = Float;
function Int(value) {
this.value = value.toString();
}
Int.prototype.valueOf = function() {
var number = Number(this.value);
if (number > Number.MAX_SAFE_INTEGER) {
throw new Error('Integer ' + this.value + ' is out of bounds.');
}
return number;
};
codec.Int = Int;
/**
* Re-decode after the generic gRPC decoding step.
*
* @private
*/
function decode(value, field) {
function decodeValue_(decoded, type) {
if (is.null(decoded)) {
return null;
}
switch (type.code) {
case 'BYTES': {
decoded = Buffer.from(decoded, 'base64');
break;
}
case 'FLOAT64': {
decoded = new codec.Float(decoded);
break;
}
case 'INT64': {
decoded = new codec.Int(decoded);
break;
}
case 'TIMESTAMP': // falls through
case 'DATE': {
decoded = new Date(decoded);
break;
}
case 'ARRAY': {
decoded = decoded.map(function(value) {
return decodeValue_(value, type.arrayElementType);
});
break;
}
case 'STRUCT': {
var formattedRow = [];
var serializedRow = {};
var fields = type.structType.fields;
fields.forEach(function(field, index) {
var value = decoded[field.name] || decoded[index];
var column = {
name: field.name,
value: decodeValue_(value, field.type),
};
formattedRow.push(column);
if (column.name) {
serializedRow[column.name] = column.value;
}
});
Object.defineProperty(formattedRow, 'toJSON', {
enumerable: false,
value: function() {
return serializedRow;
},
});
decoded = formattedRow;
break;
}
}
return decoded;
}
return decodeValue_(commonGrpc.Service.decodeValue_(value), field.type);
}
codec.decode = decode;
/**
* Encode a value in the format the API expects.
*
* @private
*/
function encode(value) {
function preEncode(value) {
var numberShouldBeStringified =
(!(value instanceof Float) && is.int(value)) ||
value instanceof Int ||
is.infinite(value) ||
Number.isNaN(value);
if (is.date(value)) {
value = value.toJSON();
} else if (
value instanceof SpannerDate ||
value instanceof Float ||
value instanceof Int
) {
value = value.value;
} else if (Buffer.isBuffer(value)) {
value = value.toString('base64');
} else if (is.array(value)) {
value = value.map(preEncode);
} else if (is.object(value) && is.fn(value.hasOwnProperty)) {
for (var prop in value) {
if (value.hasOwnProperty(prop)) {
value[prop] = preEncode(value[prop]);
}
}
}
if (numberShouldBeStringified) {
value = value.toString();
}
return value;
}
return commonGrpc.Service.encodeValue_(preEncode(value));
}
codec.encode = encode;
/**
* Get the corresponding Spanner data type.
*
* @private
*
* @param {*} field - The field value.
* @return {string}
*
* @example
* Database.getType_(NaN);
* // 'float64'
*/
function getType(field) {
if (is.bool(field)) {
return 'bool';
}
var isSpecialNumber =
is.infinite(field) || (is.number(field) && isNaN(field));
if (is.decimal(field) || isSpecialNumber || field instanceof Float) {
return 'float64';
}
if (is.number(field) || field instanceof Int) {
return 'int64';
}
if (is.string(field)) {
return 'string';
}
if (Buffer.isBuffer(field)) {
return 'bytes';
}
if (is.date(field)) {
return 'timestamp';
}
if (field instanceof SpannerDate) {
return 'date';
}
if (is.array(field)) {
var child;
for (var i = 0; i < field.length; i++) {
child = field[i];
if (!is.nil(child)) {
break;
}
}
return {
type: 'array',
child: getType(child),
};
}
return 'unspecified';
}
codec.getType = getType;
/**
* A list of available Spanner types. The index of said type in Array aligns
* with the type code that query params require.
*
* @private
*/
var TYPES = [
'unspecified',
'bool',
'int64',
'float64',
'timestamp',
'date',
'string',
'bytes',
'array',
];
codec.TYPES = TYPES;