-
Notifications
You must be signed in to change notification settings - Fork 0
/
value_test.go
3095 lines (2957 loc) · 118 KB
/
value_test.go
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
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2017 Google LLC
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.
*/
package spanner
import (
"bytes"
"encoding/json"
"fmt"
"math"
"math/big"
"reflect"
"strconv"
"testing"
"time"
"cloud.google.com/go/civil"
sppb "cloud.google.com/go/spanner/apiv1/spannerpb"
"github.com/google/go-cmp/cmp"
"github.com/storj/exp-spanner/internal/testutil"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
proto3 "google.golang.org/protobuf/types/known/structpb"
structpb "google.golang.org/protobuf/types/known/structpb"
)
var (
t1 = mustParseTime("2016-11-15T15:04:05.999999999Z")
// Boundaries
t2 = mustParseTime("0001-01-01T00:00:00.000000000Z")
t3 = mustParseTime("9999-12-31T23:59:59.999999999Z")
// Local timezone
t4 = time.Now()
d1 = mustParseDate("2016-11-15")
d2 = mustParseDate("1678-01-01")
)
func mustParseTime(s string) time.Time {
t, err := time.Parse(time.RFC3339Nano, s)
if err != nil {
panic(err)
}
return t
}
func mustParseDate(s string) civil.Date {
d, err := civil.ParseDate(s)
if err != nil {
panic(err)
}
return d
}
type customStructToString struct {
A string
B string
}
// Convert the customStructToString
func (c customStructToString) EncodeSpanner() (interface{}, error) {
return "A-B", nil
}
// Convert to customStructToString
func (c *customStructToString) DecodeSpanner(val interface{}) (err error) {
c.A = "A"
c.B = "B"
return nil
}
type customStructToInt struct {
A int64
B int64
}
// Convert the customStructToInt
func (c customStructToInt) EncodeSpanner() (interface{}, error) {
return 123, nil
}
// Convert to customStructToInt
func (c *customStructToInt) DecodeSpanner(val interface{}) (err error) {
c.A = 1
c.B = 23
return nil
}
type customStructToFloat struct {
A float64
B float64
}
// Convert the customStructToFloat
func (c customStructToFloat) EncodeSpanner() (interface{}, error) {
return 123.123, nil
}
// Convert to customStructToFloat
func (c *customStructToFloat) DecodeSpanner(val interface{}) (err error) {
c.A = 1.23
c.B = 12.3
return nil
}
type customStructToBool struct {
A bool
B bool
}
// Convert the customStructToBool
func (c customStructToBool) EncodeSpanner() (interface{}, error) {
return true, nil
}
// Convert to customStructToBool
func (c *customStructToBool) DecodeSpanner(val interface{}) (err error) {
c.A = true
c.B = false
return nil
}
type customStructToBytes struct {
A []byte
B []byte
}
// Convert the customStructToBytes
func (c customStructToBytes) EncodeSpanner() (interface{}, error) {
return []byte("AB"), nil
}
// Convert to customStructToBytes
func (c *customStructToBytes) DecodeSpanner(val interface{}) (err error) {
c.A = []byte("A")
c.B = []byte("B")
return nil
}
type customStructToTime struct {
A string
B string
}
// Convert the customStructToTime
func (c customStructToTime) EncodeSpanner() (interface{}, error) {
return t1, nil
}
// Convert to customStructToTime
func (c *customStructToTime) DecodeSpanner(val interface{}) (err error) {
c.A = "A"
c.B = "B"
return nil
}
type customStructToDate struct {
A string
B string
}
// Convert the customStructToDate
func (c customStructToDate) EncodeSpanner() (interface{}, error) {
return d1, nil
}
// Convert to customStructToDate
func (c *customStructToDate) DecodeSpanner(val interface{}) (err error) {
c.A = "A"
c.B = "B"
return nil
}
type customStructToNull struct {
val interface{}
}
func (c customStructToNull) EncodeSpanner() (interface{}, error) {
return c.val, nil
}
func (c *customStructToNull) DecodeSpanner(val interface{}) (err error) {
if reflect.ValueOf(val).IsNil() {
return nil
}
return fmt.Errorf("val mismatch: expected nil, got %v", val)
}
// e.g. a clock face time HH:MM
type customArray [4]uint8
// Convert to customArray from structpb.ListValue<structpb.StringValue>
func (c *customArray) DecodeSpanner(val interface{}) error {
listVal, ok := val.(*structpb.ListValue)
if !ok {
return fmt.Errorf("failed to decode customArray: unexpected type of %v", val)
}
asSlice := listVal.AsSlice()
if len(asSlice) != 4 {
return fmt.Errorf("failed to decode customArray: expected array of length 4")
}
for i, vI := range asSlice {
vStr, ok := vI.(string)
if !ok {
return fmt.Errorf("failed to decode customArray: got non string value: %v", vI)
}
vInt, _ := strconv.Atoi(vStr)
c[i] = uint8(vInt)
}
return nil
}
// Test encoding Values.
func TestEncodeValue(t *testing.T) {
type CustomString string
type CustomBytes []byte
type CustomInt64 int64
type CustomBool bool
type CustomFloat64 float64
type CustomTime time.Time
type CustomDate civil.Date
type CustomNumeric big.Rat
type CustomPGNumeric PGNumeric
type CustomPGJSONB PGJsonB
type CustomNullString NullString
type CustomNullInt64 NullInt64
type CustomNullBool NullBool
type CustomNullFloat64 NullFloat64
type CustomNullTime NullTime
type CustomNullDate NullDate
type CustomNullNumeric NullNumeric
type CustomNullJSON NullJSON
type Message struct {
Name string
Body string
Time int64
}
msg := Message{"Alice", "Hello", 1294706395881547000}
jsonStr := `{"Name":"Alice","Body":"Hello","Time":1294706395881547000}`
emptyArrayJSONStr := `[]`
type PtrMessage struct {
Key *string
}
ptrMsg := PtrMessage{}
nullValueJSONStr := `{"Key":null}`
sValue := "abc"
var sNilPtr *string
iValue := int64(7)
var iNilPtr *int64
bValue := true
var bNilPtr *bool
fValue := 3.14
var fNilPtr *float64
f32Value := float32(3.14)
var f32NilPtr *float32
tValue := t1
var tNilPtr *time.Time
dValue := d1
var dNilPtr *civil.Date
numValuePtr := big.NewRat(12345, 1e3)
var numNilPtr *big.Rat
num2ValuePtr := big.NewRat(12345, 1e4)
maxNumValuePtr, _ := (&big.Rat{}).SetString("99999999999999999999999999999.999999999")
minNumValuePtr, _ := (&big.Rat{}).SetString("-99999999999999999999999999999.999999999")
var (
tString = stringType()
tInt = intType()
tBool = boolType()
tFloat = floatType()
tFloat32 = float32Type()
tBytes = bytesType()
tTime = timeType()
tDate = dateType()
tNumeric = numericType()
tJSON = jsonType()
tPGNumeric = pgNumericType()
tPGJsonb = pgJsonbType()
)
for i, test := range []struct {
in interface{}
want *proto3.Value
wantType *sppb.Type
name string
}{
// STRING / STRING ARRAY:
{"abc", stringProto("abc"), tString, "string"},
{NullString{"abc", true}, stringProto("abc"), tString, "NullString with value"},
{NullString{"abc", false}, nullProto(), tString, "NullString with null"},
{&sValue, stringProto("abc"), tString, "*string with value"},
{sNilPtr, nullProto(), tString, "*string with null"},
{[]string(nil), nullProto(), listType(tString), "null []string"},
{[]string{"abc", "bcd"}, listProto(stringProto("abc"), stringProto("bcd")), listType(tString), "[]string"},
{[]NullString{{"abcd", true}, {"xyz", false}}, listProto(stringProto("abcd"), nullProto()), listType(tString), "[]NullString"},
{[]*string{&sValue, sNilPtr}, listProto(stringProto("abc"), nullProto()), listType(tString), "[]*string"},
// BYTES / BYTES ARRAY
{[]byte("foo"), bytesProto([]byte("foo")), tBytes, "[]byte with value"},
{[]byte(nil), nullProto(), tBytes, "null []byte"},
{[][]byte{nil, []byte("ab")}, listProto(nullProto(), bytesProto([]byte("ab"))), listType(tBytes), "[][]byte"},
{[][]byte(nil), nullProto(), listType(tBytes), "null [][]byte"},
// INT64 / INT64 ARRAY
{7, intProto(7), tInt, "int"},
{[]int(nil), nullProto(), listType(tInt), "null []int"},
{[]int{31, 127}, listProto(intProto(31), intProto(127)), listType(tInt), "[]int"},
{int64(81), intProto(81), tInt, "int64"},
{[]int64(nil), nullProto(), listType(tInt), "null []int64"},
{[]int64{33, 129}, listProto(intProto(33), intProto(129)), listType(tInt), "[]int64"},
{NullInt64{11, true}, intProto(11), tInt, "NullInt64 with value"},
{NullInt64{11, false}, nullProto(), tInt, "NullInt64 with null"},
{&iValue, intProto(7), tInt, "*int64 with value"},
{iNilPtr, nullProto(), tInt, "*int64 with null"},
{[]NullInt64{{35, true}, {131, false}}, listProto(intProto(35), nullProto()), listType(tInt), "[]NullInt64"},
{[]*int64{&iValue, iNilPtr}, listProto(intProto(7), nullProto()), listType(tInt), "[]*int64"},
// BOOL / BOOL ARRAY
{true, boolProto(true), tBool, "bool"},
{NullBool{true, true}, boolProto(true), tBool, "NullBool with value"},
{NullBool{true, false}, nullProto(), tBool, "NullBool with null"},
{&bValue, boolProto(true), tBool, "*bool with value"},
{bNilPtr, nullProto(), tBool, "*bool with null"},
{[]bool{true, false}, listProto(boolProto(true), boolProto(false)), listType(tBool), "[]bool"},
{[]NullBool{{true, true}, {true, false}}, listProto(boolProto(true), nullProto()), listType(tBool), "[]NullBool"},
{[]*bool{&bValue, bNilPtr}, listProto(boolProto(true), nullProto()), listType(tBool), "[]*bool"},
// FLOAT64 / FLOAT64 ARRAY
{3.14, floatProto(3.14), tFloat, "float"},
{NullFloat64{3.1415, true}, floatProto(3.1415), tFloat, "NullFloat64 with value"},
{NullFloat64{math.Inf(1), true}, floatProto(math.Inf(1)), tFloat, "NullFloat64 with infinity"},
{NullFloat64{3.14159, false}, nullProto(), tFloat, "NullFloat64 with null"},
{&fValue, floatProto(3.14), tFloat, "*float64 with value"},
{fNilPtr, nullProto(), tFloat, "*float64 with null"},
{[]float64(nil), nullProto(), listType(tFloat), "null []float64"},
{[]float64{3.141, 0.618, math.Inf(-1)}, listProto(floatProto(3.141), floatProto(0.618), floatProto(math.Inf(-1))), listType(tFloat), "[]float64"},
{[]NullFloat64{{3.141, true}, {0.618, false}}, listProto(floatProto(3.141), nullProto()), listType(tFloat), "[]NullFloat64"},
{[]*float64{&fValue, fNilPtr}, listProto(floatProto(3.14), nullProto()), listType(tFloat), "[]NullFloat64"},
// FLOAT32 / FLOAT32 ARRAY
{float32(3.14), float32Proto(3.14), tFloat32, "float32"},
{NullFloat32{3.14, true}, float32Proto(3.14), tFloat32, "NullFloat32 with value"},
{NullFloat32{float32(math.Inf(1)), true}, float32Proto(float32(math.Inf(1))), tFloat32, "NullFloat32 with infinity"},
{NullFloat32{3.14, false}, nullProto(), tFloat32, "NullFloat32 with null"},
{&f32Value, float32Proto(3.14), tFloat32, "*float32 with value"},
{f32NilPtr, nullProto(), tFloat32, "*float32 with null"},
{[]float32(nil), nullProto(), listType(tFloat32), "null []float32"},
{[]float32{3.14, 0.618, float32(math.Inf(-1))}, listProto(float32Proto(3.14), float32Proto(0.618), float32Proto(float32(math.Inf(-1)))), listType(tFloat32), "[]float32"},
{[]NullFloat32{{3.14, true}, {0.618, false}}, listProto(float32Proto(3.14), nullProto()), listType(tFloat32), "[]NullFloat"},
{[]*float32{&f32Value, f32NilPtr}, listProto(float32Proto(3.14), nullProto()), listType(tFloat32), "[]NullFloat32"},
// NUMERIC / NUMERIC ARRAY
{*numValuePtr, numericProto(numValuePtr), tNumeric, "big.Rat"},
{numValuePtr, numericProto(numValuePtr), tNumeric, "*big.Rat"},
{maxNumValuePtr, numericProto(maxNumValuePtr), tNumeric, "max numeric"},
{minNumValuePtr, numericProto(minNumValuePtr), tNumeric, "min numeric"},
{numNilPtr, nullProto(), tNumeric, "*big.Rat with null"},
{NullNumeric{*numValuePtr, true}, numericProto(numValuePtr), tNumeric, "NullNumeric with value"},
{NullNumeric{*numValuePtr, false}, nullProto(), tNumeric, "NullNumeric with null"},
{[]big.Rat(nil), nullProto(), listType(tNumeric), "null []big.Rat"},
{[]big.Rat{*numValuePtr, *num2ValuePtr}, listProto(numericProto(numValuePtr), numericProto(num2ValuePtr)), listType(tNumeric), "[]big.Rat"},
{[]NullNumeric{{*numValuePtr, true}, {*numValuePtr, false}}, listProto(numericProto(numValuePtr), nullProto()), listType(tNumeric), "[]NullNumeric"},
{[]*big.Rat{nil, numValuePtr}, listProto(nullProto(), numericProto(numValuePtr)), listType(tNumeric), "[]*big.Rat"},
{[]*big.Rat(nil), nullProto(), listType(tNumeric), "null []*big.Rat"},
// JSON
{NullJSON{msg, true}, stringProto(jsonStr), tJSON, "NullJSON with value"},
{NullJSON{msg, false}, nullProto(), tJSON, "NullJSON with null"},
{[]NullJSON(nil), nullProto(), listType(tJSON), "null []NullJSON"},
{[]NullJSON{{msg, true}, {msg, false}}, listProto(stringProto(jsonStr), nullProto()), listType(tJSON), "[]NullJSON"},
{NullJSON{[]Message{}, true}, stringProto(emptyArrayJSONStr), tJSON, "a json string with empty array to NullJSON"},
{NullJSON{ptrMsg, true}, stringProto(nullValueJSONStr), tJSON, "a json string with null value to NullJSON"},
// PG JSONB
{PGJsonB{Value: msg, Valid: true}, stringProto(jsonStr), tPGJsonb, "PGJsonB with value"},
{PGJsonB{Value: msg, Valid: false}, nullProto(), tPGJsonb, "PGJsonB with null"},
{[]PGJsonB(nil), nullProto(), listType(tPGJsonb), "null []PGJsonB"},
{[]PGJsonB{{Value: msg, Valid: true}, {Value: msg, Valid: false}}, listProto(stringProto(jsonStr), nullProto()), listType(tPGJsonb), "[]PGJsonB"},
{PGJsonB{Value: []Message{}, Valid: true}, stringProto(emptyArrayJSONStr), tPGJsonb, "a json string with empty array to PGJsonB"},
{PGJsonB{Value: ptrMsg, Valid: true}, stringProto(nullValueJSONStr), tPGJsonb, "a json string with null value to PGJsonB"},
// PG NUMERIC
{PGNumeric{"123.456", true}, stringProto("123.456"), tPGNumeric, "PG Numeric"},
{PGNumeric{Valid: false}, nullProto(), tPGNumeric, "PG Numeric with a null value"},
{[]PGNumeric(nil), nullProto(), listType(tPGNumeric), "null []PGNumeric"},
{[]PGNumeric{{"123.456", true}, {Valid: false}}, listProto(stringProto("123.456"), nullProto()), listType(tPGNumeric), "[]PGNumeric"},
// TIMESTAMP / TIMESTAMP ARRAY
{t1, timeProto(t1), tTime, "time"},
{NullTime{t1, true}, timeProto(t1), tTime, "NullTime with value"},
{NullTime{t1, false}, nullProto(), tTime, "NullTime with null"},
{&tValue, timeProto(t1), tTime, "*time.Time with value"},
{tNilPtr, nullProto(), tTime, "*time.Time with null"},
{[]time.Time(nil), nullProto(), listType(tTime), "null []time"},
{[]time.Time{t1, t2, t3, t4}, listProto(timeProto(t1), timeProto(t2), timeProto(t3), timeProto(t4)), listType(tTime), "[]time"},
{[]NullTime{{t1, true}, {t1, false}}, listProto(timeProto(t1), nullProto()), listType(tTime), "[]NullTime"},
{[]*time.Time{&tValue, tNilPtr}, listProto(timeProto(t1), nullProto()), listType(tTime), "[]*time.Time"},
// DATE / DATE ARRAY
{d1, dateProto(d1), tDate, "date"},
{NullDate{d1, true}, dateProto(d1), tDate, "NullDate with value"},
{NullDate{civil.Date{}, false}, nullProto(), tDate, "NullDate with null"},
{&dValue, dateProto(d1), tDate, "*civil.Date with value"},
{dNilPtr, nullProto(), tDate, "*civil.Date with null"},
{[]civil.Date(nil), nullProto(), listType(tDate), "null []date"},
{[]civil.Date{d1, d2}, listProto(dateProto(d1), dateProto(d2)), listType(tDate), "[]date"},
{[]NullDate{{d1, true}, {civil.Date{}, false}}, listProto(dateProto(d1), nullProto()), listType(tDate), "[]NullDate"},
{[]*civil.Date{&dValue, dNilPtr}, listProto(dateProto(d1), nullProto()), listType(tDate), "[]*civil.Date"},
// GenericColumnValue
{GenericColumnValue{tString, stringProto("abc")}, stringProto("abc"), tString, "GenericColumnValue with value"},
{GenericColumnValue{tString, nullProto()}, nullProto(), tString, "GenericColumnValue with null"},
// not actually valid (stringProto inside int list), but demonstrates pass-through.
{
GenericColumnValue{
Type: listType(tInt),
Value: listProto(intProto(5), nullProto(), stringProto("bcd")),
},
listProto(intProto(5), nullProto(), stringProto("bcd")),
listType(tInt),
"pass-through",
},
// placeholder
{CommitTimestamp, stringProto(commitTimestampPlaceholderString), tTime, "CommitTimestampPlaceholder"},
// CUSTOM STRING / CUSTOM STRING ARRAY
{CustomString("abc"), stringProto("abc"), tString, "CustomString"},
{CustomNullString{"abc", true}, stringProto("abc"), tString, "CustomNullString with value"},
{CustomNullString{"abc", false}, nullProto(), tString, "CustomNullString with null"},
{[]CustomString(nil), nullProto(), listType(tString), "null []CustomString"},
{[]CustomString{"abc", "bcd"}, listProto(stringProto("abc"), stringProto("bcd")), listType(tString), "[]CustomString"},
{[]CustomNullString(nil), nullProto(), listType(tString), "null []NullCustomString"},
{[]CustomNullString{{"abcd", true}, {"xyz", false}}, listProto(stringProto("abcd"), nullProto()), listType(tString), "[]NullCustomString"},
// CUSTOM BYTES / CUSTOM BYTES ARRAY
{CustomBytes("foo"), bytesProto([]byte("foo")), tBytes, "CustomBytes with value"},
{CustomBytes(nil), nullProto(), tBytes, "null CustomBytes"},
{[]CustomBytes{nil, CustomBytes("ab")}, listProto(nullProto(), bytesProto([]byte("ab"))), listType(tBytes), "[]CustomBytes"},
{[]CustomBytes(nil), nullProto(), listType(tBytes), "null []CustomBytes"},
// CUSTOM INT64 / CUSTOM INT64 ARRAY
{CustomInt64(81), intProto(81), tInt, "CustomInt64"},
{[]CustomInt64(nil), nullProto(), listType(tInt), "null []CustomInt64"},
{[]CustomInt64{33, 129}, listProto(intProto(33), intProto(129)), listType(tInt), "[]CustomInt64"},
{CustomNullInt64{11, true}, intProto(11), tInt, "CustomNullInt64 with value"},
{CustomNullInt64{11, false}, nullProto(), tInt, "CustomNullInt64 with null"},
{[]CustomNullInt64(nil), nullProto(), listType(tInt), "null []CustomNullInt64"},
{[]CustomNullInt64{{35, true}, {131, false}}, listProto(intProto(35), nullProto()), listType(tInt), "[]CustomNullInt64"},
// CUSTOM BOOL / CUSTOM BOOL ARRAY
{CustomBool(true), boolProto(true), tBool, "CustomBool"},
{CustomNullBool{true, true}, boolProto(true), tBool, "CustomNullBool with value"},
{CustomNullBool{true, false}, nullProto(), tBool, "CustomNullBool with null"},
{[]CustomBool{true, false}, listProto(boolProto(true), boolProto(false)), listType(tBool), "[]CustomBool"},
{[]CustomNullBool{{true, true}, {true, false}}, listProto(boolProto(true), nullProto()), listType(tBool), "[]CustomNullBool"},
// FLOAT64 / FLOAT64 ARRAY
{CustomFloat64(3.14), floatProto(3.14), tFloat, "CustomFloat64"},
{CustomNullFloat64{3.1415, true}, floatProto(3.1415), tFloat, "CustomNullFloat64 with value"},
{CustomNullFloat64{math.Inf(1), true}, floatProto(math.Inf(1)), tFloat, "CustomNullFloat64 with infinity"},
{CustomNullFloat64{3.14159, false}, nullProto(), tFloat, "CustomNullFloat64 with null"},
{[]CustomFloat64(nil), nullProto(), listType(tFloat), "null []CustomFloat64"},
{[]CustomFloat64{3.141, 0.618, CustomFloat64(math.Inf(-1))}, listProto(floatProto(3.141), floatProto(0.618), floatProto(math.Inf(-1))), listType(tFloat), "[]CustomFloat64"},
{[]CustomNullFloat64(nil), nullProto(), listType(tFloat), "null []CustomNullFloat64"},
{[]CustomNullFloat64{{3.141, true}, {0.618, false}}, listProto(floatProto(3.141), nullProto()), listType(tFloat), "[]CustomNullFloat64"},
// CUSTOM TIMESTAMP / CUSTOM TIMESTAMP ARRAY
{CustomTime(t1), timeProto(t1), tTime, "CustomTime"},
{CustomNullTime{t1, true}, timeProto(t1), tTime, "CustomNullTime with value"},
{CustomNullTime{t1, false}, nullProto(), tTime, "CustomNullTime with null"},
{[]CustomTime(nil), nullProto(), listType(tTime), "null []CustomTime"},
{[]CustomTime{CustomTime(t1), CustomTime(t2), CustomTime(t3), CustomTime(t4)}, listProto(timeProto(t1), timeProto(t2), timeProto(t3), timeProto(t4)), listType(tTime), "[]CustomTime"},
{[]CustomNullTime(nil), nullProto(), listType(tTime), "null []CustomNullTime"},
{[]CustomNullTime{{t1, true}, {t1, false}}, listProto(timeProto(t1), nullProto()), listType(tTime), "[]CustomNullTime"},
// CUSTOM DATE / CUSTOM DATE ARRAY
{CustomDate(d1), dateProto(d1), tDate, "CustomDate"},
{CustomNullDate{d1, true}, dateProto(d1), tDate, "CustomNullDate with value"},
{CustomNullDate{civil.Date{}, false}, nullProto(), tDate, "CustomNullDate with null"},
{[]CustomDate(nil), nullProto(), listType(tDate), "null []CustomDate"},
{[]CustomDate{CustomDate(d1), CustomDate(d2)}, listProto(dateProto(d1), dateProto(d2)), listType(tDate), "[]CustomDate"},
{[]CustomNullDate(nil), nullProto(), listType(tDate), "null []CustomNullDate"},
{[]CustomNullDate{{d1, true}, {civil.Date{}, false}}, listProto(dateProto(d1), nullProto()), listType(tDate), "[]NullDate"},
// CUSTOM STRUCT
{customStructToString{"A", "B"}, stringProto("A-B"), tString, "a struct to string"},
{customStructToInt{1, 23}, intProto(123), tInt, "a struct to int"},
{customStructToFloat{1.23, 12.3}, floatProto(123.123), tFloat, "a struct to float"},
{customStructToBool{true, false}, boolProto(true), tBool, "a struct to bool"},
{customStructToBytes{[]byte("A"), []byte("B")}, bytesProto([]byte("AB")), tBytes, "a struct to bytes"},
{customStructToTime{"A", "B"}, timeProto(tValue), tTime, "a struct to time"},
{customStructToDate{"A", "B"}, dateProto(dValue), tDate, "a struct to date"},
{customStructToNull{val: bNilPtr}, nullProto(), tBool, "a struct to null bool"},
{customStructToNull{val: []byte(nil)}, nullProto(), tBytes, "a struct to null bytes"},
{customStructToNull{val: sNilPtr}, nullProto(), tString, "a struct to null string"},
{customStructToNull{val: iNilPtr}, nullProto(), tInt, "a struct to null int"},
{customStructToNull{val: fNilPtr}, nullProto(), tFloat, "a struct to null float"},
{customStructToNull{val: numNilPtr}, nullProto(), tNumeric, "a struct to null numeric"},
{customStructToNull{val: dNilPtr}, nullProto(), tDate, "a struct to null date"},
{customStructToNull{val: tNilPtr}, nullProto(), tTime, "a struct to null timestamp"},
// CUSTOM NUMERIC / CUSTOM NUMERIC ARRAY
{CustomNumeric(*numValuePtr), numericProto(numValuePtr), tNumeric, "CustomNumeric"},
{CustomNullNumeric{*numValuePtr, true}, numericProto(numValuePtr), tNumeric, "CustomNullNumeric with value"},
{CustomNullNumeric{*numValuePtr, false}, nullProto(), tNumeric, "CustomNullNumeric with null"},
{[]CustomNumeric(nil), nullProto(), listType(tNumeric), "null []CustomNumeric"},
{[]CustomNumeric{CustomNumeric(*numValuePtr), CustomNumeric(*num2ValuePtr)}, listProto(numericProto(numValuePtr), numericProto(num2ValuePtr)), listType(tNumeric), "[]CustomNumeric"},
{[]CustomNullNumeric(nil), nullProto(), listType(tNumeric), "null []CustomNullNumeric"},
{[]CustomNullNumeric{{*numValuePtr, true}, {*num2ValuePtr, false}}, listProto(numericProto(numValuePtr), nullProto()), listType(tNumeric), "[]CustomNullNumeric"},
// CUSTOM JSON
{CustomNullJSON{msg, true}, stringProto(jsonStr), tJSON, "CustomNullJSON with value"},
{CustomNullJSON{msg, false}, nullProto(), tJSON, "CustomNullJSON with null"},
{[]CustomNullJSON(nil), nullProto(), listType(tJSON), "null []CustomNullJSON"},
{[]CustomNullJSON{{msg, true}, {msg, false}}, listProto(stringProto(jsonStr), nullProto()), listType(tJSON), "[]CustomNullJSON"},
// CUSTOM PG NUMERIC
{CustomPGNumeric{"123.456", true}, stringProto("123.456"), tPGNumeric, "PG Numeric"},
{CustomPGNumeric{Valid: false}, nullProto(), tPGNumeric, "PG Numeric with a null value"},
{[]CustomPGNumeric(nil), nullProto(), listType(tPGNumeric), "null []PGNumeric"},
{[]CustomPGNumeric{{"123.456", true}, {Valid: false}}, listProto(stringProto("123.456"), nullProto()), listType(tPGNumeric), "[]PGNumeric"},
// CUSTOM PG JSONB
{CustomPGJSONB{Value: msg, Valid: true}, stringProto(jsonStr), tPGJsonb, "CustomPGJSONB with value"},
{CustomPGJSONB{Value: msg, Valid: false}, nullProto(), tPGJsonb, "CustomPGJSONB with null"},
{[]CustomPGJSONB(nil), nullProto(), listType(tPGJsonb), "null []CustomPGJSONB"},
{[]CustomPGJSONB{{Value: msg, Valid: true}, {Value: msg, Valid: false}}, listProto(stringProto(jsonStr), nullProto()), listType(tPGJsonb), "[]CustomPGJSONB"},
} {
got, gotType, err := encodeValue(test.in)
if err != nil {
t.Fatalf("#%d (%s): got error during encoding: %v, want nil", i, test.name, err)
}
if !testEqual(got, test.want) {
t.Errorf("#%d (%s): got encode result: %v, want %v", i, test.name, got, test.want)
}
if !testEqual(gotType, test.wantType) {
t.Errorf("#%d (%s): got encode type: %v, want %v", i, test.name, gotType, test.wantType)
}
}
}
// Test encoding invalid values.
func TestEncodeInvalidValues(t *testing.T) {
type CustomNumeric big.Rat
invalidNumPtr1 := big.NewRat(11234567891, 1e10)
invalidNumPtr2, _ := (&big.Rat{}).SetString("199999999999999999999999999999.999999999")
// Enable error mode.
oldValue := LossOfPrecisionHandling
defer func() {
// Reset the value to pre-test value
LossOfPrecisionHandling = oldValue
}()
LossOfPrecisionHandling = NumericError
for i, test := range []struct {
desc string
in interface{}
errMsg string
}{
// NUMERIC
{desc: "numeric pointer with invalid scale component", in: invalidNumPtr1, errMsg: "max scale for a numeric is 9. The requested numeric has more"},
{desc: "numeric pointer with invalid whole component", in: invalidNumPtr2, errMsg: "max precision for the whole component of a numeric is 29. The requested numeric has a whole component with precision 30"},
{desc: "numeric with invalid scale component", in: *invalidNumPtr1, errMsg: "max scale for a numeric is 9. The requested numeric has more"},
{desc: "numeric with invalid whole component", in: *invalidNumPtr2, errMsg: "max precision for the whole component of a numeric is 29. The requested numeric has a whole component with precision 30"},
// CUSTOM NUMERIC
{desc: "custom numeric type with invalid scale component", in: CustomNumeric(*invalidNumPtr1), errMsg: "max scale for a numeric is 9. The requested numeric has more"},
{desc: "custom numeric type with invalid whole component", in: CustomNumeric(*invalidNumPtr2), errMsg: "max precision for the whole component of a numeric is 29. The requested numeric has a whole component with precision 30"},
} {
_, _, err := encodeValue(test.in)
if err == nil {
t.Fatalf("#%d (%s): want error during encoding, but got nil", i, test.desc)
}
if err.Error() != test.errMsg {
t.Errorf("#%d (%s): incorrect error message, got %v, want %v", i, test.desc, err, test.errMsg)
}
}
}
type encodeTest struct {
desc string
in interface{}
want *proto3.Value
wantType *sppb.Type
}
func checkStructEncoding(desc string, got *proto3.Value, gotType *sppb.Type,
want *proto3.Value, wantType *sppb.Type, t *testing.T) {
if !testEqual(got, want) {
t.Errorf("Test %s: got encode result: %v, want %v", desc, got, want)
}
if !testEqual(gotType, wantType) {
t.Errorf("Test %s: got encode type: %v, want %v", desc, gotType, wantType)
}
}
// Testcase code
func encodeStructValue(test encodeTest, t *testing.T) {
got, gotType, err := encodeValue(test.in)
if err != nil {
t.Fatalf("Test %s: got error during encoding: %v, want nil", test.desc, err)
}
checkStructEncoding(test.desc, got, gotType, test.want, test.wantType, t)
}
func TestEncodeStructValuePointers(t *testing.T) {
type structf struct {
F int `spanner:"ff2"`
}
nestedStructProto := structType(mkField("ff2", intType()))
type testType struct {
Stringf string
Structf *structf
ArrStructf []*structf
}
testTypeProto := structType(
mkField("Stringf", stringType()),
mkField("Structf", nestedStructProto),
mkField("ArrStructf", listType(nestedStructProto)))
for _, test := range []encodeTest{
{
"Pointer to Go struct with pointers-to-(array)-struct fields.",
&testType{"hello", &structf{50}, []*structf{{30}, {40}}},
listProto(
stringProto("hello"),
listProto(intProto(50)),
listProto(
listProto(intProto(30)),
listProto(intProto(40)))),
testTypeProto,
},
{
"Nil pointer to Go struct representing a NULL struct value.",
(*testType)(nil),
nullProto(),
testTypeProto,
},
{
"Slice of pointers to Go structs with NULL and non-NULL elements.",
[]*testType{
(*testType)(nil),
{"hello", nil, []*structf{nil, {40}}},
{"world", &structf{70}, nil},
},
listProto(
nullProto(),
listProto(
stringProto("hello"),
nullProto(),
listProto(nullProto(), listProto(intProto(40)))),
listProto(
stringProto("world"),
listProto(intProto(70)),
nullProto())),
listType(testTypeProto),
},
{
"Nil slice of pointers to structs representing a NULL array of structs.",
[]*testType(nil),
nullProto(),
listType(testTypeProto),
},
{
"Empty slice of pointers to structs representing an empty array of structs.",
[]*testType{},
listProto(),
listType(testTypeProto),
},
} {
encodeStructValue(test, t)
}
}
func TestEncodeStructValueErrors(t *testing.T) {
type Embedded struct {
A int
}
type embedded struct {
B bool
}
x := 0
for _, test := range []struct {
desc string
in interface{}
wantErr error
}{
{
"Unsupported embedded fields.",
struct{ Embedded }{Embedded{10}},
errUnsupportedEmbeddedStructFields("Embedded"),
},
{
"Unsupported pointer to embedded fields.",
struct{ *Embedded }{&Embedded{10}},
errUnsupportedEmbeddedStructFields("Embedded"),
},
{
"Unsupported embedded + unexported fields.",
struct {
int
*bool
embedded
}{10, nil, embedded{false}},
errUnsupportedEmbeddedStructFields("int"),
},
{
"Unsupported type.",
(**struct{})(nil),
errEncoderUnsupportedType((**struct{})(nil)),
},
{
"Unsupported type.",
3,
errEncoderUnsupportedType(3),
},
{
"Unsupported type.",
&x,
errEncoderUnsupportedType(&x),
},
} {
_, _, got := encodeStruct(test.in)
if got == nil || !testEqual(test.wantErr, got) {
t.Errorf("Test: %s, expected error %v during decoding, got %v", test.desc, test.wantErr, got)
}
}
}
func TestEncodeStructValueArrayStructFields(t *testing.T) {
type structf struct {
Intff int
}
structfType := structType(mkField("Intff", intType()))
for _, test := range []encodeTest{
{
"Unnamed array-of-struct-typed field.",
struct {
Intf int
ArrStructf []structf `spanner:""`
}{10, []structf{{1}, {2}}},
listProto(
intProto(10),
listProto(
listProto(intProto(1)),
listProto(intProto(2)))),
structType(
mkField("Intf", intType()),
mkField("", listType(structfType))),
},
{
"Null array-of-struct-typed field.",
struct {
Intf int
ArrStructf []structf
}{10, []structf(nil)},
listProto(intProto(10), nullProto()),
structType(
mkField("Intf", intType()),
mkField("ArrStructf", listType(structfType))),
},
{
"Array-of-struct-typed field representing empty array.",
struct {
Intf int
ArrStructf []structf
}{10, []structf{}},
listProto(intProto(10), listProto([]*proto3.Value{}...)),
structType(
mkField("Intf", intType()),
mkField("ArrStructf", listType(structfType))),
},
{
"Array-of-struct-typed field with nullable struct elements.",
struct {
Intf int
ArrStructf []*structf
}{
10,
[]*structf{(*structf)(nil), {1}},
},
listProto(
intProto(10),
listProto(
nullProto(),
listProto(intProto(1)))),
structType(
mkField("Intf", intType()),
mkField("ArrStructf", listType(structfType))),
},
} {
encodeStructValue(test, t)
}
}
func TestEncodeStructValueStructFields(t *testing.T) {
type structf struct {
Intff int
}
structfType := structType(mkField("Intff", intType()))
for _, test := range []encodeTest{
{
"Named struct-type field.",
struct {
Intf int
Structf structf
}{10, structf{10}},
listProto(intProto(10), listProto(intProto(10))),
structType(
mkField("Intf", intType()),
mkField("Structf", structfType)),
},
{
"Unnamed struct-type field.",
struct {
Intf int
Structf structf `spanner:""`
}{10, structf{10}},
listProto(intProto(10), listProto(intProto(10))),
structType(
mkField("Intf", intType()),
mkField("", structfType)),
},
{
"Duplicate struct-typed field.",
struct {
Structf1 structf `spanner:""`
Structf2 structf `spanner:""`
}{structf{10}, structf{20}},
listProto(listProto(intProto(10)), listProto(intProto(20))),
structType(
mkField("", structfType),
mkField("", structfType)),
},
{
"Null struct-typed field.",
struct {
Intf int
Structf *structf
}{10, nil},
listProto(intProto(10), nullProto()),
structType(
mkField("Intf", intType()),
mkField("Structf", structfType)),
},
{
"Empty struct-typed field.",
struct {
Intf int
Structf struct{}
}{10, struct{}{}},
listProto(intProto(10), listProto([]*proto3.Value{}...)),
structType(
mkField("Intf", intType()),
mkField("Structf", structType([]*sppb.StructType_Field{}...))),
},
} {
encodeStructValue(test, t)
}
}
func TestEncodeStructValueFieldNames(t *testing.T) {
type embedded struct {
B bool
}
for _, test := range []encodeTest{
{
"Duplicate fields.",
struct {
Field1 int `spanner:"field"`
DupField1 int `spanner:"field"`
}{10, 20},
listProto(intProto(10), intProto(20)),
structType(
mkField("field", intType()),
mkField("field", intType())),
},
{
"Duplicate Fields (different types).",
struct {
IntField int `spanner:"field"`
StringField string `spanner:"field"`
}{10, "abc"},
listProto(intProto(10), stringProto("abc")),
structType(
mkField("field", intType()),
mkField("field", stringType())),
},
{
"Duplicate unnamed fields.",
struct {
Dup int `spanner:""`
Dup1 int `spanner:""`
}{10, 20},
listProto(intProto(10), intProto(20)),
structType(
mkField("", intType()),
mkField("", intType())),
},
{
"Named and unnamed fields.",
struct {
Field string
Field1 int `spanner:""`
Field2 string `spanner:"field"`
}{"abc", 10, "def"},
listProto(stringProto("abc"), intProto(10), stringProto("def")),
structType(
mkField("Field", stringType()),
mkField("", intType()),
mkField("field", stringType())),
},
{
"Ignored unexported fields.",
struct {
Field int
field bool
Field1 string `spanner:"field"`
}{10, false, "abc"},
listProto(intProto(10), stringProto("abc")),
structType(
mkField("Field", intType()),
mkField("field", stringType())),
},
{
"Ignored unexported struct/slice fields.",
struct {
a []*embedded
b []embedded
c embedded
d *embedded
Field1 string `spanner:"field"`
}{nil, nil, embedded{}, nil, "def"},
listProto(stringProto("def")),
structType(
mkField("field", stringType())),
},
} {
encodeStructValue(test, t)
}
}
func TestEncodeStructValueBasicFields(t *testing.T) {
type CustomString string
type CustomBytes []byte
type CustomInt64 int64
type CustomBool bool
type CustomFloat64 float64
type CustomFloat32 float32
type CustomTime time.Time
type CustomDate civil.Date
type CustomNullString NullString
type CustomNullInt64 NullInt64
type CustomNullBool NullBool
type CustomNullFloat64 NullFloat64
type CustomNullFloat32 NullFloat32
type CustomNullTime NullTime
type CustomNullDate NullDate
sValue := "abc"
iValue := int64(300)
bValue := false
fValue := 3.45
f32Value := float32(3.14)
tValue := t1
dValue := d1
StructTypeProto := structType(
mkField("Stringf", stringType()),
mkField("Intf", intType()),
mkField("Boolf", boolType()),
mkField("Floatf", floatType()),
mkField("Float32f", float32Type()),
mkField("Bytef", bytesType()),
mkField("Timef", timeType()),
mkField("Datef", dateType()))
for _, test := range []encodeTest{
{
"Basic types.",
struct {
Stringf string
Intf int
Boolf bool
Floatf float64
Float32f float32
Bytef []byte
Timef time.Time
Datef civil.Date
}{"abc", 300, false, 3.45, float32(3.14), []byte("foo"), t1, d1},
listProto(
stringProto("abc"),
intProto(300),
boolProto(false),
floatProto(3.45),
float32Proto(3.14),
bytesProto([]byte("foo")),
timeProto(t1),
dateProto(d1)),
StructTypeProto,
},
{
"Pointers to basic types.",
struct {