-
Notifications
You must be signed in to change notification settings - Fork 100
/
codec.ts
911 lines (730 loc) · 25 KB
/
codec.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
/*!
* 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.
*/
import * as assert from 'assert';
import {before, beforeEach, afterEach, describe, it} from 'mocha';
import * as proxyquire from 'proxyquire';
import * as sinon from 'sinon';
import {Big} from 'big.js';
import {PreciseDate} from '@google-cloud/precise-date';
import {GrpcService} from '../src/common-grpc/service';
import {google} from '../protos/protos';
describe('codec', () => {
let codec;
const sandbox = sinon.createSandbox();
before(() => {
codec = proxyquire('../src/codec.js', {
'./common-grpc/service': {GrpcService},
}).codec;
});
beforeEach(() => {
sandbox.stub(GrpcService, 'encodeValue_').callsFake(value => value);
sandbox.stub(GrpcService, 'decodeValue_').callsFake(value => value);
});
afterEach(() => sandbox.restore());
describe('SpannerDate', () => {
describe('instantiation', () => {
it('should accept date strings', () => {
const date = new codec.SpannerDate('3-22-1986');
const json = date.toJSON();
assert.strictEqual(json, '1986-03-22');
});
it('should default to the current local date', () => {
const date = new codec.SpannerDate();
const today = new Date();
const year = today.getFullYear();
const month = today.getMonth();
const day = today.getDate();
const expected = new codec.SpannerDate(year, month, day);
assert.deepStrictEqual(date, expected);
});
it('should interpret ISO date strings as local time', () => {
const date = new codec.SpannerDate('1986-03-22');
const json = date.toJSON();
assert.strictEqual(json, '1986-03-22');
});
it('should accept y/m/d number values', () => {
const date = new codec.SpannerDate(1986, 2, 22);
const json = date.toJSON();
assert.strictEqual(json, '1986-03-22');
});
it('should accept year zero in y/m/d number values', () => {
const d = new codec.SpannerDate(null!);
const date = new codec.SpannerDate(0, 2, 22);
const json = date.toJSON();
assert.ok(d);
assert.strictEqual(json, '1900-03-22');
});
it('should truncate additional date fields', () => {
const truncated = new codec.SpannerDate(1986, 2, 22, 4, 8, 10);
const expected = new codec.SpannerDate(1986, 2, 22);
assert.deepStrictEqual(truncated, expected);
});
});
describe('toJSON', () => {
let date: Date;
beforeEach(() => {
date = new codec.SpannerDate();
sandbox.stub(date, 'getFullYear').returns(1999);
sandbox.stub(date, 'getMonth').returns(11);
sandbox.stub(date, 'getDate').returns(31);
});
it('should return the spanner date string', () => {
const json = date.toJSON();
assert.strictEqual(json, '1999-12-31');
});
it('should pad single digit months', () => {
(date.getMonth as sinon.SinonStub).returns(8);
const json = date.toJSON();
assert.strictEqual(json, '1999-09-31');
});
it('should pad single digit dates', () => {
(date.getDate as sinon.SinonStub).returns(3);
const json = date.toJSON();
assert.strictEqual(json, '1999-12-03');
});
});
});
describe('Float', () => {
it('should store the value', () => {
const value = 8;
const float = new codec.Float(value);
assert.strictEqual(float.value, value);
});
it('should return as a float', () => {
const value = '8.2';
const float = new codec.Float(value);
assert.strictEqual(float.valueOf(), Number(value));
assert.strictEqual(float + 2, Number(value) + 2);
});
});
describe('Int', () => {
it('should stringify the value', () => {
const value = 8;
const int = new codec.Int(value);
assert.strictEqual(int.value, '8');
});
it('should return as a number', () => {
const value = 8;
const int = new codec.Int(value);
assert.strictEqual(int.valueOf(), 8);
assert.strictEqual(int + 2, 10);
});
it('should throw if number is out of bounds', () => {
const value = '9223372036854775807';
const int = new codec.Int(value);
assert.throws(() => {
int.valueOf();
}, new RegExp('Integer ' + value + ' is out of bounds.'));
});
});
describe('Numeric', () => {
it('should store value as a string', () => {
const value = '8.01911';
const numeric = new codec.Numeric(value);
assert.strictEqual(numeric.value, '8.01911');
});
it('should return as a Big', () => {
const value = '8.01911';
const numeric = new codec.Numeric(value);
const expected = new Big(value);
assert.ok(numeric.valueOf().eq(expected));
});
it('toJSON', () => {
const value = '8.01911';
const numeric = new codec.Numeric(value);
assert.strictEqual(numeric.toJSON(), value);
});
});
describe('Struct', () => {
describe('toJSON', () => {
it('should covert the struct to JSON', () => {
const struct = new codec.Struct();
const options = {};
const fakeJson = {};
(sandbox.stub(codec, 'convertFieldsToJson') as sinon.SinonStub)
.withArgs(struct, options)
.returns(fakeJson);
assert.strictEqual(struct.toJSON(options), fakeJson);
});
});
describe('fromArray', () => {
it('should wrap the array in a struct', () => {
const fields = [{name: 'name', value: 'value'}];
const struct = codec.Struct.fromArray(fields);
assert(struct instanceof codec.Struct);
fields.forEach((field, i) => {
assert.strictEqual(struct[i], field);
});
});
});
describe('fromJSON', () => {
it('should covert json to a struct', () => {
const json = {a: 'b', c: 'd'};
const expected = [
{name: 'a', value: 'b'},
{name: 'c', value: 'd'},
];
const struct = codec.Struct.fromJSON(json);
assert(struct instanceof codec.Struct);
expected.forEach((field, i) => {
assert.deepStrictEqual(struct[i], field);
});
});
});
});
describe('convertFieldsToJson', () => {
const ROW = [
{
name: 'name',
value: 'value',
},
];
it('should not require options', () => {
assert.doesNotThrow(() => codec.convertFieldsToJson(ROW));
});
it('should return serialized rows', () => {
const json = codec.convertFieldsToJson(ROW);
assert.deepStrictEqual(json, {name: 'value'});
});
it('should not return nameless values', () => {
const row = [
{
value: 'value',
},
];
const json = codec.convertFieldsToJson(row);
assert.deepStrictEqual(json, {});
});
describe('structs', () => {
it('should not wrap structs by default', () => {
const options = {wrapNumbers: false, wrapStructs: false};
const fakeStructJson = {};
const struct = new codec.Struct();
const stub = sandbox.stub(struct, 'toJSON').returns(fakeStructJson);
const row = [{name: 'Struct', value: struct}];
const json = codec.convertFieldsToJson(row, options);
assert.strictEqual(json.Struct, fakeStructJson);
assert.deepStrictEqual(stub.lastCall.args[0], options);
});
it('should wrap structs with option', () => {
const value = 3.3;
const expectedStruct = codec.Struct.fromJSON({Number: value});
const struct = codec.Struct.fromJSON({Number: new codec.Float(value)});
const row = [{name: 'Struct', value: struct}];
const json = codec.convertFieldsToJson(row, {wrapStructs: true});
assert.deepStrictEqual(json.Struct, expectedStruct);
});
});
describe('numbers', () => {
it('should not wrap numbers by default', () => {
const row = [
{
name: 'Number',
value: new codec.Int(3),
},
];
const json = codec.convertFieldsToJson(row);
assert.strictEqual(typeof json.Number, 'number');
assert.strictEqual(json.Number, 3);
});
it('should wrap numbers with option', () => {
const int = new codec.Int(3);
const row = [
{
name: 'Number',
value: int,
},
];
const json = codec.convertFieldsToJson(row, {wrapNumbers: true});
assert(json.Number instanceof codec.Int);
assert.deepStrictEqual(json.Number, int);
});
it('should throw an error if number is out of bounds', () => {
const int = new codec.Int('9223372036854775807');
const row = [
{
name: 'Number',
value: int,
},
];
assert.throws(() => {
codec.convertFieldsToJson(row);
}, new RegExp('Serializing column "Number" encountered an error'));
});
});
describe('arrays', () => {
it('should not wrap numbers by default', () => {
const value = 3;
const row = [
{
name: 'List',
value: [new codec.Int(value)],
},
];
const json = codec.convertFieldsToJson(row);
assert.deepStrictEqual(json.List, [value]);
});
it('should wrap numbers with option', () => {
const value = new codec.Int(3);
const row = [{name: 'List', value: [value]}];
const json = codec.convertFieldsToJson(row, {wrapNumbers: true});
assert.deepStrictEqual(json.List, [value]);
});
it('should not wrap structs by default', () => {
const struct = new codec.Struct();
const expectedStruct = {a: 'b', c: 'd'};
sandbox.stub(struct, 'toJSON').returns(expectedStruct);
const row = [{name: 'List', value: [struct]}];
const json = codec.convertFieldsToJson(row);
assert.deepStrictEqual(json.List, [expectedStruct]);
});
it('should wrap structs with option', () => {
const expectedStruct = codec.Struct.fromJSON({a: 'b', c: 'd'});
const row = [{name: 'List', value: [expectedStruct]}];
const json = codec.convertFieldsToJson(row, {wrapStructs: true});
assert.deepStrictEqual(json.List, [expectedStruct]);
});
});
});
describe('decode', () => {
// Does not require any special decoding.
const BYPASS_FIELD = {
code: 'not-real-code',
};
it('should return the same value if not a special type', () => {
const value = {};
const decoded = codec.decode(value, BYPASS_FIELD);
assert.strictEqual(decoded, value);
});
it('should return null values as null', () => {
(GrpcService.decodeValue_ as sinon.SinonStub).returns(null);
const decoded = codec.decode(null, BYPASS_FIELD);
assert.strictEqual(decoded, null);
});
it('should decode BYTES', () => {
const expected = Buffer.from('bytes value');
const encoded = expected.toString('base64');
const decoded = codec.decode(encoded, {
code: google.spanner.v1.TypeCode.BYTES,
});
assert.deepStrictEqual(decoded, expected);
});
it('should decode FLOAT64', () => {
const value = 'Infinity';
const decoded = codec.decode(value, {
code: google.spanner.v1.TypeCode.FLOAT64,
});
assert(decoded instanceof codec.Float);
assert.strictEqual(decoded.value, value);
});
it('should decode INT64', () => {
const value = '64';
const decoded = codec.decode(value, {
code: google.spanner.v1.TypeCode.INT64,
});
assert(decoded instanceof codec.Int);
assert.strictEqual(decoded.value, value);
});
it('should decode NUMERIC', () => {
const value = '8.01911';
const decoded = codec.decode(value, {
code: google.spanner.v1.TypeCode.NUMERIC,
});
assert(decoded instanceof codec.Numeric);
assert.strictEqual(decoded.value, value);
});
it('should decode TIMESTAMP', () => {
const value = new Date();
const expected = new PreciseDate(value.getTime());
const decoded = codec.decode(value.toJSON(), {
code: google.spanner.v1.TypeCode.TIMESTAMP,
});
assert.deepStrictEqual(decoded, expected);
});
it('should decode DATE', () => {
const value = new Date();
const expected = new codec.SpannerDate(value.toISOString());
const decoded = codec.decode(value.toJSON(), {
code: google.spanner.v1.TypeCode.DATE,
});
assert.deepStrictEqual(decoded, expected);
});
it('should decode ARRAY and inner members', () => {
const value = ['1'];
const decoded = codec.decode(value, {
code: google.spanner.v1.TypeCode.ARRAY,
arrayElementType: {
code: google.spanner.v1.TypeCode.INT64,
},
});
assert(decoded[0] instanceof codec.Int);
});
it('should decode STRUCT and inner members', () => {
const value = {
fieldName: '1',
};
const decoded = codec.decode(value, {
code: google.spanner.v1.TypeCode.STRUCT,
structType: {
fields: [
{
name: 'fieldName',
type: {
code: google.spanner.v1.TypeCode.INT64,
},
},
],
},
});
const expectedStruct = new codec.Struct({
name: 'fieldName',
value: new codec.Int(value.fieldName),
});
assert(decoded instanceof codec.Struct);
assert.deepStrictEqual(decoded, expectedStruct);
});
});
describe('encode', () => {
it('should return the value from the common encoder', () => {
const value = {};
const defaultEncodedValue = {};
(GrpcService.encodeValue_ as sinon.SinonStub)
.withArgs(value)
.returns(defaultEncodedValue);
const encoded = codec.encode(value);
assert.strictEqual(encoded, defaultEncodedValue);
});
it('should encode BYTES', () => {
const value = Buffer.from('bytes value');
const encoded = codec.encode(value);
assert.strictEqual(encoded, value.toString('base64'));
});
it('should encode structs', () => {
const value = codec.Struct.fromJSON({a: 'b', c: 'd'});
const encoded = codec.encode(value);
assert.deepStrictEqual(encoded, ['b', 'd']);
});
it('should stringify Infinity', () => {
const value = Infinity;
const encoded = codec.encode(value);
assert.strictEqual(encoded, value.toString());
});
it('should stringify -Infinity', () => {
const value = -Infinity;
const encoded = codec.encode(value);
assert.strictEqual(encoded, value.toString());
});
it('should stringify NaN', () => {
const value = NaN;
const encoded = codec.encode(value);
assert.strictEqual(encoded, value.toString());
});
it('should stringify INT64', () => {
const value = 5;
const encoded = codec.encode(value);
assert.strictEqual(encoded, value.toString());
});
it('should stringify NUMERIC', () => {
const value = new codec.Numeric('8.01911');
const encoded = codec.encode(value);
assert.strictEqual(encoded, value.value);
});
it('should encode ARRAY and inner members', () => {
const value = [5];
const encoded = codec.encode(value);
assert.deepStrictEqual(encoded, [
value.toString(), // (tests that it is stringified)
]);
});
it('should encode TIMESTAMP', () => {
const value = new PreciseDate();
const encoded = codec.encode(value);
assert.strictEqual(encoded, value.toJSON());
});
it('should encode DATE', () => {
const value = new codec.SpannerDate();
const encoded = codec.encode(value);
assert.strictEqual(encoded, value.toJSON());
});
it('should encode INT64', () => {
const value = new codec.Int(10);
const encoded = codec.encode(value);
assert.strictEqual(encoded, '10');
});
it('should encode FLOAT64', () => {
const value = new codec.Float(10);
const encoded = codec.encode(value);
assert.strictEqual(encoded, 10);
});
});
describe('getType', () => {
it('should determine if the value is a boolean', () => {
assert.deepStrictEqual(codec.getType(true), {type: 'bool'});
});
it('should determine if the value is a float', () => {
assert.deepStrictEqual(codec.getType(NaN), {type: 'float64'});
assert.deepStrictEqual(codec.getType(Infinity), {type: 'float64'});
assert.deepStrictEqual(codec.getType(-Infinity), {type: 'float64'});
assert.deepStrictEqual(codec.getType(2.2), {type: 'float64'});
assert.deepStrictEqual(codec.getType(new codec.Float(1.1)), {
type: 'float64',
});
});
it('should determine if the value is an int', () => {
assert.deepStrictEqual(codec.getType(1234), {type: 'int64'});
assert.deepStrictEqual(codec.getType(new codec.Int(1)), {type: 'int64'});
});
it('should determine if the value is numeric', () => {
assert.deepStrictEqual(codec.getType(new codec.Numeric('8.01911')), {
type: 'numeric',
});
});
it('should determine if the value is a string', () => {
assert.deepStrictEqual(codec.getType('abc'), {type: 'string'});
});
it('should determine if the value is bytes', () => {
assert.deepStrictEqual(codec.getType(Buffer.from('abc')), {
type: 'bytes',
});
});
it('should determine if the value is a date', () => {
assert.deepStrictEqual(codec.getType(new codec.SpannerDate()), {
type: 'date',
});
});
it('should determine if the value is a timestamp', () => {
assert.deepStrictEqual(codec.getType(new PreciseDate()), {
type: 'timestamp',
});
});
it('should accept a plain date object as a timestamp', () => {
assert.deepStrictEqual(codec.getType(new Date()), {type: 'timestamp'});
});
it('should determine if the value is a struct', () => {
const struct = codec.Struct.fromJSON({a: 'b'});
const type = codec.getType(struct);
assert.deepStrictEqual(type, {
type: 'struct',
fields: [{name: 'a', type: 'string'}],
});
});
it('should attempt to determine arrays and their values', () => {
assert.deepStrictEqual(codec.getType([Infinity]), {
type: 'array',
child: {
type: 'float64',
},
});
});
it('should return unspecified for unknown values', () => {
assert.deepStrictEqual(codec.getType(null), {type: 'unspecified'});
assert.deepStrictEqual(codec.getType([null]), {
type: 'array',
child: {
type: 'unspecified',
},
});
});
});
describe('convertToListValue', () => {
beforeEach(() => {
sandbox.stub(codec, 'encode').callsFake(value => {
return {stringValue: value};
});
});
it('should map values to encoded versions', () => {
const actual = ['hi', 'bye'];
const expected = {
values: [{stringValue: 'hi'}, {stringValue: 'bye'}],
};
const converted = codec.convertToListValue(actual);
assert.deepStrictEqual(converted, expected);
});
it('should convert a single value to a list value', () => {
const actual = 'hi';
const expected = {
values: [{stringValue: 'hi'}],
};
const converted = codec.convertToListValue(actual);
assert.deepStrictEqual(converted, expected);
});
});
describe('convertMsToProtoTimestamp', () => {
it('should convert ms to google.protobuf.Timestamp', () => {
const ms = 5000.00001;
const expected = {
nanos: 10,
seconds: 5,
};
const converted = codec.convertMsToProtoTimestamp(ms);
assert.deepStrictEqual(converted, expected);
});
});
describe('convertProtoTimestampToDate', () => {
it('should convert google.protobuf.Timestamp to Date', () => {
const timestamp = {nanos: 10, seconds: 5};
const expected = new Date(5000.00001);
const converted = codec.convertProtoTimestampToDate(timestamp);
assert.deepStrictEqual(converted, expected);
});
});
describe('createTypeObject', () => {
it('should convert strings to the corresponding type', () => {
const typeMap = {
unspecified: {
code:
google.spanner.v1.TypeCode[
google.spanner.v1.TypeCode.TYPE_CODE_UNSPECIFIED
],
},
bool: {
code: google.spanner.v1.TypeCode[google.spanner.v1.TypeCode.BOOL],
},
int64: {
code: google.spanner.v1.TypeCode[google.spanner.v1.TypeCode.INT64],
},
float64: {
code: google.spanner.v1.TypeCode[google.spanner.v1.TypeCode.FLOAT64],
},
timestamp: {
code:
google.spanner.v1.TypeCode[google.spanner.v1.TypeCode.TIMESTAMP],
},
date: {
code: google.spanner.v1.TypeCode[google.spanner.v1.TypeCode.DATE],
},
string: {
code: google.spanner.v1.TypeCode[google.spanner.v1.TypeCode.STRING],
},
bytes: {
code: google.spanner.v1.TypeCode[google.spanner.v1.TypeCode.BYTES],
},
array: {
code: google.spanner.v1.TypeCode[google.spanner.v1.TypeCode.ARRAY],
arrayElementType: {
code:
google.spanner.v1.TypeCode[
google.spanner.v1.TypeCode.TYPE_CODE_UNSPECIFIED
],
},
},
struct: {
code: google.spanner.v1.TypeCode[google.spanner.v1.TypeCode.STRUCT],
structType: {fields: []},
},
};
Object.keys(typeMap).forEach(key => {
const type = codec.createTypeObject(key);
assert.deepStrictEqual(type, typeMap[key]);
});
});
it('should default to unspecified for unknown types', () => {
const type = codec.createTypeObject('unicorn');
assert.deepStrictEqual(type, {
code:
google.spanner.v1.TypeCode[
google.spanner.v1.TypeCode.TYPE_CODE_UNSPECIFIED
],
});
});
it('should set the arrayElementType', () => {
const type = codec.createTypeObject({
type: 'array',
child: 'bool',
});
assert.deepStrictEqual(type, {
code: google.spanner.v1.TypeCode[google.spanner.v1.TypeCode.ARRAY],
arrayElementType: {
code: google.spanner.v1.TypeCode[google.spanner.v1.TypeCode.BOOL],
},
});
});
it('should set the struct fields', () => {
const type = codec.createTypeObject({
type: 'struct',
fields: [
{name: 'boolKey', type: 'bool'},
{name: 'intKey', type: 'int64'},
],
});
assert.deepStrictEqual(type, {
code: google.spanner.v1.TypeCode[google.spanner.v1.TypeCode.STRUCT],
structType: {
fields: [
{
name: 'boolKey',
type: {
code:
google.spanner.v1.TypeCode[google.spanner.v1.TypeCode.BOOL],
},
},
{
name: 'intKey',
type: {
code:
google.spanner.v1.TypeCode[google.spanner.v1.TypeCode.INT64],
},
},
],
},
});
});
it('should handle nested structs', () => {
const type = codec.createTypeObject({
type: 'struct',
fields: [
{
name: 'nestedStruct',
type: 'struct',
fields: [
{
type: 'bool',
name: 'boolKey',
},
],
},
],
});
assert.deepStrictEqual(type, {
code: google.spanner.v1.TypeCode[google.spanner.v1.TypeCode.STRUCT],
structType: {
fields: [
{
name: 'nestedStruct',
type: {
code:
google.spanner.v1.TypeCode[google.spanner.v1.TypeCode.STRUCT],
structType: {
fields: [
{
name: 'boolKey',
type: {
code:
google.spanner.v1.TypeCode[
google.spanner.v1.TypeCode.BOOL
],
},
},
],
},
},
},
],
},
});
});
});
});