1/*
2Copyright 2017 Google LLC
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package spanner
18
19import (
20	"bytes"
21	"encoding/base64"
22	"fmt"
23	"math"
24	"math/big"
25	"reflect"
26	"strconv"
27	"time"
28
29	"cloud.google.com/go/civil"
30	"cloud.google.com/go/internal/fields"
31	"github.com/golang/protobuf/proto"
32	proto3 "github.com/golang/protobuf/ptypes/struct"
33	sppb "google.golang.org/genproto/googleapis/spanner/v1"
34	"google.golang.org/grpc/codes"
35)
36
37const (
38	// nullString is returned by the String methods of NullableValues when the
39	// underlying database value is null.
40	nullString                       = "<null>"
41	commitTimestampPlaceholderString = "spanner.commit_timestamp()"
42
43	// NumericPrecisionDigits is the maximum number of digits in a NUMERIC
44	// value.
45	NumericPrecisionDigits = 38
46
47	// NumericScaleDigits is the maximum number of digits after the decimal
48	// point in a NUMERIC value.
49	NumericScaleDigits = 9
50)
51
52// NumericString returns a string representing a *big.Rat in a format compatible
53// with Spanner SQL. It returns a floating-point literal with 9 digits after the
54// decimal point.
55func NumericString(r *big.Rat) string {
56	return r.FloatString(NumericScaleDigits)
57}
58
59var (
60	// CommitTimestamp is a special value used to tell Cloud Spanner to insert
61	// the commit timestamp of the transaction into a column. It can be used in
62	// a Mutation, or directly used in InsertStruct or InsertMap. See
63	// ExampleCommitTimestamp. This is just a placeholder and the actual value
64	// stored in this variable has no meaning.
65	CommitTimestamp = commitTimestamp
66	commitTimestamp = time.Unix(0, 0).In(time.FixedZone("CommitTimestamp placeholder", 0xDB))
67
68	jsonNullBytes = []byte("null")
69)
70
71// Encoder is the interface implemented by a custom type that can be encoded to
72// a supported type by Spanner. A code example:
73//
74//   type customField struct {
75//       Prefix string
76//       Suffix string
77//   }
78//
79//   // Convert a customField value to a string
80//   func (cf customField) EncodeSpanner() (interface{}, error) {
81//       var b bytes.Buffer
82//       b.WriteString(cf.Prefix)
83//       b.WriteString("-")
84//       b.WriteString(cf.Suffix)
85//       return b.String(), nil
86//   }
87type Encoder interface {
88	EncodeSpanner() (interface{}, error)
89}
90
91// Decoder is the interface implemented by a custom type that can be decoded
92// from a supported type by Spanner. A code example:
93//
94//   type customField struct {
95//       Prefix string
96//       Suffix string
97//   }
98//
99//   // Convert a string to a customField value
100//   func (cf *customField) DecodeSpanner(val interface{}) (err error) {
101//       strVal, ok := val.(string)
102//       if !ok {
103//           return fmt.Errorf("failed to decode customField: %v", val)
104//       }
105//       s := strings.Split(strVal, "-")
106//       if len(s) > 1 {
107//           cf.Prefix = s[0]
108//           cf.Suffix = s[1]
109//       }
110//       return nil
111//   }
112type Decoder interface {
113	DecodeSpanner(input interface{}) error
114}
115
116// NullableValue is the interface implemented by all null value wrapper types.
117type NullableValue interface {
118	// IsNull returns true if the underlying database value is null.
119	IsNull() bool
120}
121
122// NullInt64 represents a Cloud Spanner INT64 that may be NULL.
123type NullInt64 struct {
124	Int64 int64 // Int64 contains the value when it is non-NULL, and zero when NULL.
125	Valid bool  // Valid is true if Int64 is not NULL.
126}
127
128// IsNull implements NullableValue.IsNull for NullInt64.
129func (n NullInt64) IsNull() bool {
130	return !n.Valid
131}
132
133// String implements Stringer.String for NullInt64
134func (n NullInt64) String() string {
135	if !n.Valid {
136		return nullString
137	}
138	return fmt.Sprintf("%v", n.Int64)
139}
140
141// MarshalJSON implements json.Marshaler.MarshalJSON for NullInt64.
142func (n NullInt64) MarshalJSON() ([]byte, error) {
143	if n.Valid {
144		return []byte(fmt.Sprintf("%v", n.Int64)), nil
145	}
146	return jsonNullBytes, nil
147}
148
149// UnmarshalJSON implements json.Unmarshaler.UnmarshalJSON for NullInt64.
150func (n *NullInt64) UnmarshalJSON(payload []byte) error {
151	if payload == nil {
152		return fmt.Errorf("payload should not be nil")
153	}
154	if bytes.Equal(payload, jsonNullBytes) {
155		n.Int64 = int64(0)
156		n.Valid = false
157		return nil
158	}
159	num, err := strconv.ParseInt(string(payload), 10, 64)
160	if err != nil {
161		return fmt.Errorf("payload cannot be converted to int64: got %v", string(payload))
162	}
163	n.Int64 = num
164	n.Valid = true
165	return nil
166}
167
168// NullString represents a Cloud Spanner STRING that may be NULL.
169type NullString struct {
170	StringVal string // StringVal contains the value when it is non-NULL, and an empty string when NULL.
171	Valid     bool   // Valid is true if StringVal is not NULL.
172}
173
174// IsNull implements NullableValue.IsNull for NullString.
175func (n NullString) IsNull() bool {
176	return !n.Valid
177}
178
179// String implements Stringer.String for NullString
180func (n NullString) String() string {
181	if !n.Valid {
182		return nullString
183	}
184	return n.StringVal
185}
186
187// MarshalJSON implements json.Marshaler.MarshalJSON for NullString.
188func (n NullString) MarshalJSON() ([]byte, error) {
189	if n.Valid {
190		return []byte(fmt.Sprintf("%q", n.StringVal)), nil
191	}
192	return jsonNullBytes, nil
193}
194
195// UnmarshalJSON implements json.Unmarshaler.UnmarshalJSON for NullString.
196func (n *NullString) UnmarshalJSON(payload []byte) error {
197	if payload == nil {
198		return fmt.Errorf("payload should not be nil")
199	}
200	if bytes.Equal(payload, jsonNullBytes) {
201		n.StringVal = ""
202		n.Valid = false
203		return nil
204	}
205	payload, err := trimDoubleQuotes(payload)
206	if err != nil {
207		return err
208	}
209	n.StringVal = string(payload)
210	n.Valid = true
211	return nil
212}
213
214// NullFloat64 represents a Cloud Spanner FLOAT64 that may be NULL.
215type NullFloat64 struct {
216	Float64 float64 // Float64 contains the value when it is non-NULL, and zero when NULL.
217	Valid   bool    // Valid is true if Float64 is not NULL.
218}
219
220// IsNull implements NullableValue.IsNull for NullFloat64.
221func (n NullFloat64) IsNull() bool {
222	return !n.Valid
223}
224
225// String implements Stringer.String for NullFloat64
226func (n NullFloat64) String() string {
227	if !n.Valid {
228		return nullString
229	}
230	return fmt.Sprintf("%v", n.Float64)
231}
232
233// MarshalJSON implements json.Marshaler.MarshalJSON for NullFloat64.
234func (n NullFloat64) MarshalJSON() ([]byte, error) {
235	if n.Valid {
236		return []byte(fmt.Sprintf("%v", n.Float64)), nil
237	}
238	return jsonNullBytes, nil
239}
240
241// UnmarshalJSON implements json.Unmarshaler.UnmarshalJSON for NullFloat64.
242func (n *NullFloat64) UnmarshalJSON(payload []byte) error {
243	if payload == nil {
244		return fmt.Errorf("payload should not be nil")
245	}
246	if bytes.Equal(payload, jsonNullBytes) {
247		n.Float64 = float64(0)
248		n.Valid = false
249		return nil
250	}
251	num, err := strconv.ParseFloat(string(payload), 64)
252	if err != nil {
253		return fmt.Errorf("payload cannot be converted to float64: got %v", string(payload))
254	}
255	n.Float64 = num
256	n.Valid = true
257	return nil
258}
259
260// NullBool represents a Cloud Spanner BOOL that may be NULL.
261type NullBool struct {
262	Bool  bool // Bool contains the value when it is non-NULL, and false when NULL.
263	Valid bool // Valid is true if Bool is not NULL.
264}
265
266// IsNull implements NullableValue.IsNull for NullBool.
267func (n NullBool) IsNull() bool {
268	return !n.Valid
269}
270
271// String implements Stringer.String for NullBool
272func (n NullBool) String() string {
273	if !n.Valid {
274		return nullString
275	}
276	return fmt.Sprintf("%v", n.Bool)
277}
278
279// MarshalJSON implements json.Marshaler.MarshalJSON for NullBool.
280func (n NullBool) MarshalJSON() ([]byte, error) {
281	if n.Valid {
282		return []byte(fmt.Sprintf("%v", n.Bool)), nil
283	}
284	return jsonNullBytes, nil
285}
286
287// UnmarshalJSON implements json.Unmarshaler.UnmarshalJSON for NullBool.
288func (n *NullBool) UnmarshalJSON(payload []byte) error {
289	if payload == nil {
290		return fmt.Errorf("payload should not be nil")
291	}
292	if bytes.Equal(payload, jsonNullBytes) {
293		n.Bool = false
294		n.Valid = false
295		return nil
296	}
297	b, err := strconv.ParseBool(string(payload))
298	if err != nil {
299		return fmt.Errorf("payload cannot be converted to bool: got %v", string(payload))
300	}
301	n.Bool = b
302	n.Valid = true
303	return nil
304}
305
306// NullTime represents a Cloud Spanner TIMESTAMP that may be null.
307type NullTime struct {
308	Time  time.Time // Time contains the value when it is non-NULL, and a zero time.Time when NULL.
309	Valid bool      // Valid is true if Time is not NULL.
310}
311
312// IsNull implements NullableValue.IsNull for NullTime.
313func (n NullTime) IsNull() bool {
314	return !n.Valid
315}
316
317// String implements Stringer.String for NullTime
318func (n NullTime) String() string {
319	if !n.Valid {
320		return nullString
321	}
322	return n.Time.Format(time.RFC3339Nano)
323}
324
325// MarshalJSON implements json.Marshaler.MarshalJSON for NullTime.
326func (n NullTime) MarshalJSON() ([]byte, error) {
327	if n.Valid {
328		return []byte(fmt.Sprintf("%q", n.String())), nil
329	}
330	return jsonNullBytes, nil
331}
332
333// UnmarshalJSON implements json.Unmarshaler.UnmarshalJSON for NullTime.
334func (n *NullTime) UnmarshalJSON(payload []byte) error {
335	if payload == nil {
336		return fmt.Errorf("payload should not be nil")
337	}
338	if bytes.Equal(payload, jsonNullBytes) {
339		n.Time = time.Time{}
340		n.Valid = false
341		return nil
342	}
343	payload, err := trimDoubleQuotes(payload)
344	if err != nil {
345		return err
346	}
347	s := string(payload)
348	t, err := time.Parse(time.RFC3339Nano, s)
349	if err != nil {
350		return fmt.Errorf("payload cannot be converted to time.Time: got %v", string(payload))
351	}
352	n.Time = t
353	n.Valid = true
354	return nil
355}
356
357// NullDate represents a Cloud Spanner DATE that may be null.
358type NullDate struct {
359	Date  civil.Date // Date contains the value when it is non-NULL, and a zero civil.Date when NULL.
360	Valid bool       // Valid is true if Date is not NULL.
361}
362
363// IsNull implements NullableValue.IsNull for NullDate.
364func (n NullDate) IsNull() bool {
365	return !n.Valid
366}
367
368// String implements Stringer.String for NullDate
369func (n NullDate) String() string {
370	if !n.Valid {
371		return nullString
372	}
373	return n.Date.String()
374}
375
376// MarshalJSON implements json.Marshaler.MarshalJSON for NullDate.
377func (n NullDate) MarshalJSON() ([]byte, error) {
378	if n.Valid {
379		return []byte(fmt.Sprintf("%q", n.String())), nil
380	}
381	return jsonNullBytes, nil
382}
383
384// UnmarshalJSON implements json.Unmarshaler.UnmarshalJSON for NullDate.
385func (n *NullDate) UnmarshalJSON(payload []byte) error {
386	if payload == nil {
387		return fmt.Errorf("payload should not be nil")
388	}
389	if bytes.Equal(payload, jsonNullBytes) {
390		n.Date = civil.Date{}
391		n.Valid = false
392		return nil
393	}
394	payload, err := trimDoubleQuotes(payload)
395	if err != nil {
396		return err
397	}
398	s := string(payload)
399	t, err := civil.ParseDate(s)
400	if err != nil {
401		return fmt.Errorf("payload cannot be converted to civil.Date: got %v", string(payload))
402	}
403	n.Date = t
404	n.Valid = true
405	return nil
406}
407
408// NullNumeric represents a Cloud Spanner Numeric that may be NULL.
409type NullNumeric struct {
410	Numeric big.Rat // Numeric contains the value when it is non-NULL, and a zero big.Rat when NULL.
411	Valid   bool    // Valid is true if Numeric is not NULL.
412}
413
414// IsNull implements NullableValue.IsNull for NullNumeric.
415func (n NullNumeric) IsNull() bool {
416	return !n.Valid
417}
418
419// String implements Stringer.String for NullNumeric
420func (n NullNumeric) String() string {
421	if !n.Valid {
422		return nullString
423	}
424	return fmt.Sprintf("%v", NumericString(&n.Numeric))
425}
426
427// MarshalJSON implements json.Marshaler.MarshalJSON for NullNumeric.
428func (n NullNumeric) MarshalJSON() ([]byte, error) {
429	if n.Valid {
430		return []byte(fmt.Sprintf("%q", NumericString(&n.Numeric))), nil
431	}
432	return jsonNullBytes, nil
433}
434
435// UnmarshalJSON implements json.Unmarshaler.UnmarshalJSON for NullNumeric.
436func (n *NullNumeric) UnmarshalJSON(payload []byte) error {
437	if payload == nil {
438		return fmt.Errorf("payload should not be nil")
439	}
440	if bytes.Equal(payload, jsonNullBytes) {
441		n.Numeric = big.Rat{}
442		n.Valid = false
443		return nil
444	}
445	payload, err := trimDoubleQuotes(payload)
446	if err != nil {
447		return err
448	}
449	s := string(payload)
450	val, ok := (&big.Rat{}).SetString(s)
451	if !ok {
452		return fmt.Errorf("payload cannot be converted to big.Rat: got %v", string(payload))
453	}
454	n.Numeric = *val
455	n.Valid = true
456	return nil
457}
458
459// NullRow represents a Cloud Spanner STRUCT that may be NULL.
460// See also the document for Row.
461// Note that NullRow is not a valid Cloud Spanner column Type.
462type NullRow struct {
463	Row   Row  // Row contains the value when it is non-NULL, and a zero Row when NULL.
464	Valid bool // Valid is true if Row is not NULL.
465}
466
467// GenericColumnValue represents the generic encoded value and type of the
468// column.  See google.spanner.v1.ResultSet proto for details.  This can be
469// useful for proxying query results when the result types are not known in
470// advance.
471//
472// If you populate a GenericColumnValue from a row using Row.Column or related
473// methods, do not modify the contents of Type and Value.
474type GenericColumnValue struct {
475	Type  *sppb.Type
476	Value *proto3.Value
477}
478
479// Decode decodes a GenericColumnValue. The ptr argument should be a pointer
480// to a Go value that can accept v.
481func (v GenericColumnValue) Decode(ptr interface{}) error {
482	return decodeValue(v.Value, v.Type, ptr)
483}
484
485// NewGenericColumnValue creates a GenericColumnValue from Go value that is
486// valid for Cloud Spanner.
487func newGenericColumnValue(v interface{}) (*GenericColumnValue, error) {
488	value, typ, err := encodeValue(v)
489	if err != nil {
490		return nil, err
491	}
492	return &GenericColumnValue{Value: value, Type: typ}, nil
493}
494
495// errTypeMismatch returns error for destination not having a compatible type
496// with source Cloud Spanner type.
497func errTypeMismatch(srcCode, elCode sppb.TypeCode, dst interface{}) error {
498	s := srcCode.String()
499	if srcCode == sppb.TypeCode_ARRAY {
500		s = fmt.Sprintf("%v[%v]", srcCode, elCode)
501	}
502	return spannerErrorf(codes.InvalidArgument, "type %T cannot be used for decoding %s", dst, s)
503}
504
505// errNilSpannerType returns error for nil Cloud Spanner type in decoding.
506func errNilSpannerType() error {
507	return spannerErrorf(codes.FailedPrecondition, "unexpected nil Cloud Spanner data type in decoding")
508}
509
510// errNilSrc returns error for decoding from nil proto value.
511func errNilSrc() error {
512	return spannerErrorf(codes.FailedPrecondition, "unexpected nil Cloud Spanner value in decoding")
513}
514
515// errNilDst returns error for decoding into nil interface{}.
516func errNilDst(dst interface{}) error {
517	return spannerErrorf(codes.InvalidArgument, "cannot decode into nil type %T", dst)
518}
519
520// errNilArrElemType returns error for input Cloud Spanner data type being a array but without a
521// non-nil array element type.
522func errNilArrElemType(t *sppb.Type) error {
523	return spannerErrorf(codes.FailedPrecondition, "array type %v is with nil array element type", t)
524}
525
526func errUnsupportedEmbeddedStructFields(fname string) error {
527	return spannerErrorf(codes.InvalidArgument, "Embedded field: %s. Embedded and anonymous fields are not allowed "+
528		"when converting Go structs to Cloud Spanner STRUCT values. To create a STRUCT value with an "+
529		"unnamed field, use a `spanner:\"\"` field tag.", fname)
530}
531
532// errDstNotForNull returns error for decoding a SQL NULL value into a destination which doesn't
533// support NULL values.
534func errDstNotForNull(dst interface{}) error {
535	return spannerErrorf(codes.InvalidArgument, "destination %T cannot support NULL SQL values", dst)
536}
537
538// errBadEncoding returns error for decoding wrongly encoded types.
539func errBadEncoding(v *proto3.Value, err error) error {
540	return spannerErrorf(codes.FailedPrecondition, "%v wasn't correctly encoded: <%v>", v, err)
541}
542
543func parseNullTime(v *proto3.Value, p *NullTime, code sppb.TypeCode, isNull bool) error {
544	if p == nil {
545		return errNilDst(p)
546	}
547	if code != sppb.TypeCode_TIMESTAMP {
548		return errTypeMismatch(code, sppb.TypeCode_TYPE_CODE_UNSPECIFIED, p)
549	}
550	if isNull {
551		*p = NullTime{}
552		return nil
553	}
554	x, err := getStringValue(v)
555	if err != nil {
556		return err
557	}
558	y, err := time.Parse(time.RFC3339Nano, x)
559	if err != nil {
560		return errBadEncoding(v, err)
561	}
562	p.Valid = true
563	p.Time = y
564	return nil
565}
566
567// decodeValue decodes a protobuf Value into a pointer to a Go value, as
568// specified by sppb.Type.
569func decodeValue(v *proto3.Value, t *sppb.Type, ptr interface{}) error {
570	if v == nil {
571		return errNilSrc()
572	}
573	if t == nil {
574		return errNilSpannerType()
575	}
576	code := t.Code
577	acode := sppb.TypeCode_TYPE_CODE_UNSPECIFIED
578	if code == sppb.TypeCode_ARRAY {
579		if t.ArrayElementType == nil {
580			return errNilArrElemType(t)
581		}
582		acode = t.ArrayElementType.Code
583	}
584	_, isNull := v.Kind.(*proto3.Value_NullValue)
585
586	// Do the decoding based on the type of ptr.
587	switch p := ptr.(type) {
588	case nil:
589		return errNilDst(nil)
590	case *string:
591		if p == nil {
592			return errNilDst(p)
593		}
594		if code != sppb.TypeCode_STRING {
595			return errTypeMismatch(code, acode, ptr)
596		}
597		if isNull {
598			return errDstNotForNull(ptr)
599		}
600		x, err := getStringValue(v)
601		if err != nil {
602			return err
603		}
604		*p = x
605	case *NullString, **string:
606		if p == nil {
607			return errNilDst(p)
608		}
609		if code != sppb.TypeCode_STRING {
610			return errTypeMismatch(code, acode, ptr)
611		}
612		if isNull {
613			switch sp := ptr.(type) {
614			case *NullString:
615				*sp = NullString{}
616			case **string:
617				*sp = nil
618			}
619			break
620		}
621		x, err := getStringValue(v)
622		if err != nil {
623			return err
624		}
625		switch sp := ptr.(type) {
626		case *NullString:
627			sp.Valid = true
628			sp.StringVal = x
629		case **string:
630			*sp = &x
631		}
632	case *[]NullString, *[]*string:
633		if p == nil {
634			return errNilDst(p)
635		}
636		if acode != sppb.TypeCode_STRING {
637			return errTypeMismatch(code, acode, ptr)
638		}
639		if isNull {
640			switch sp := ptr.(type) {
641			case *[]NullString:
642				*sp = nil
643			case *[]*string:
644				*sp = nil
645			}
646			break
647		}
648		x, err := getListValue(v)
649		if err != nil {
650			return err
651		}
652		switch sp := ptr.(type) {
653		case *[]NullString:
654			y, err := decodeNullStringArray(x)
655			if err != nil {
656				return err
657			}
658			*sp = y
659		case *[]*string:
660			y, err := decodeStringPointerArray(x)
661			if err != nil {
662				return err
663			}
664			*sp = y
665		}
666	case *[]string:
667		if p == nil {
668			return errNilDst(p)
669		}
670		if acode != sppb.TypeCode_STRING {
671			return errTypeMismatch(code, acode, ptr)
672		}
673		if isNull {
674			*p = nil
675			break
676		}
677		x, err := getListValue(v)
678		if err != nil {
679			return err
680		}
681		y, err := decodeStringArray(x)
682		if err != nil {
683			return err
684		}
685		*p = y
686	case *[]byte:
687		if p == nil {
688			return errNilDst(p)
689		}
690		if code != sppb.TypeCode_BYTES {
691			return errTypeMismatch(code, acode, ptr)
692		}
693		if isNull {
694			*p = nil
695			break
696		}
697		x, err := getStringValue(v)
698		if err != nil {
699			return err
700		}
701		y, err := base64.StdEncoding.DecodeString(x)
702		if err != nil {
703			return errBadEncoding(v, err)
704		}
705		*p = y
706	case *[][]byte:
707		if p == nil {
708			return errNilDst(p)
709		}
710		if acode != sppb.TypeCode_BYTES {
711			return errTypeMismatch(code, acode, ptr)
712		}
713		if isNull {
714			*p = nil
715			break
716		}
717		x, err := getListValue(v)
718		if err != nil {
719			return err
720		}
721		y, err := decodeByteArray(x)
722		if err != nil {
723			return err
724		}
725		*p = y
726	case *int64:
727		if p == nil {
728			return errNilDst(p)
729		}
730		if code != sppb.TypeCode_INT64 {
731			return errTypeMismatch(code, acode, ptr)
732		}
733		if isNull {
734			return errDstNotForNull(ptr)
735		}
736		x, err := getStringValue(v)
737		if err != nil {
738			return err
739		}
740		y, err := strconv.ParseInt(x, 10, 64)
741		if err != nil {
742			return errBadEncoding(v, err)
743		}
744		*p = y
745	case *NullInt64, **int64:
746		if p == nil {
747			return errNilDst(p)
748		}
749		if code != sppb.TypeCode_INT64 {
750			return errTypeMismatch(code, acode, ptr)
751		}
752		if isNull {
753			switch sp := ptr.(type) {
754			case *NullInt64:
755				*sp = NullInt64{}
756			case **int64:
757				*sp = nil
758			}
759			break
760		}
761		x, err := getStringValue(v)
762		if err != nil {
763			return err
764		}
765		y, err := strconv.ParseInt(x, 10, 64)
766		if err != nil {
767			return errBadEncoding(v, err)
768		}
769		switch sp := ptr.(type) {
770		case *NullInt64:
771			sp.Valid = true
772			sp.Int64 = y
773		case **int64:
774			*sp = &y
775		}
776	case *[]NullInt64, *[]*int64:
777		if p == nil {
778			return errNilDst(p)
779		}
780		if acode != sppb.TypeCode_INT64 {
781			return errTypeMismatch(code, acode, ptr)
782		}
783		if isNull {
784			switch sp := ptr.(type) {
785			case *[]NullInt64:
786				*sp = nil
787			case *[]*int64:
788				*sp = nil
789			}
790			break
791		}
792		x, err := getListValue(v)
793		if err != nil {
794			return err
795		}
796		switch sp := ptr.(type) {
797		case *[]NullInt64:
798			y, err := decodeNullInt64Array(x)
799			if err != nil {
800				return err
801			}
802			*sp = y
803		case *[]*int64:
804			y, err := decodeInt64PointerArray(x)
805			if err != nil {
806				return err
807			}
808			*sp = y
809		}
810	case *[]int64:
811		if p == nil {
812			return errNilDst(p)
813		}
814		if acode != sppb.TypeCode_INT64 {
815			return errTypeMismatch(code, acode, ptr)
816		}
817		if isNull {
818			*p = nil
819			break
820		}
821		x, err := getListValue(v)
822		if err != nil {
823			return err
824		}
825		y, err := decodeInt64Array(x)
826		if err != nil {
827			return err
828		}
829		*p = y
830	case *bool:
831		if p == nil {
832			return errNilDst(p)
833		}
834		if code != sppb.TypeCode_BOOL {
835			return errTypeMismatch(code, acode, ptr)
836		}
837		if isNull {
838			return errDstNotForNull(ptr)
839		}
840		x, err := getBoolValue(v)
841		if err != nil {
842			return err
843		}
844		*p = x
845	case *NullBool, **bool:
846		if p == nil {
847			return errNilDst(p)
848		}
849		if code != sppb.TypeCode_BOOL {
850			return errTypeMismatch(code, acode, ptr)
851		}
852		if isNull {
853			switch sp := ptr.(type) {
854			case *NullBool:
855				*sp = NullBool{}
856			case **bool:
857				*sp = nil
858			}
859			break
860		}
861		x, err := getBoolValue(v)
862		if err != nil {
863			return err
864		}
865		switch sp := ptr.(type) {
866		case *NullBool:
867			sp.Valid = true
868			sp.Bool = x
869		case **bool:
870			*sp = &x
871		}
872	case *[]NullBool, *[]*bool:
873		if p == nil {
874			return errNilDst(p)
875		}
876		if acode != sppb.TypeCode_BOOL {
877			return errTypeMismatch(code, acode, ptr)
878		}
879		if isNull {
880			switch sp := ptr.(type) {
881			case *[]NullBool:
882				*sp = nil
883			case *[]*bool:
884				*sp = nil
885			}
886			break
887		}
888		x, err := getListValue(v)
889		if err != nil {
890			return err
891		}
892		switch sp := ptr.(type) {
893		case *[]NullBool:
894			y, err := decodeNullBoolArray(x)
895			if err != nil {
896				return err
897			}
898			*sp = y
899		case *[]*bool:
900			y, err := decodeBoolPointerArray(x)
901			if err != nil {
902				return err
903			}
904			*sp = y
905		}
906	case *[]bool:
907		if p == nil {
908			return errNilDst(p)
909		}
910		if acode != sppb.TypeCode_BOOL {
911			return errTypeMismatch(code, acode, ptr)
912		}
913		if isNull {
914			*p = nil
915			break
916		}
917		x, err := getListValue(v)
918		if err != nil {
919			return err
920		}
921		y, err := decodeBoolArray(x)
922		if err != nil {
923			return err
924		}
925		*p = y
926	case *float64:
927		if p == nil {
928			return errNilDst(p)
929		}
930		if code != sppb.TypeCode_FLOAT64 {
931			return errTypeMismatch(code, acode, ptr)
932		}
933		if isNull {
934			return errDstNotForNull(ptr)
935		}
936		x, err := getFloat64Value(v)
937		if err != nil {
938			return err
939		}
940		*p = x
941	case *NullFloat64, **float64:
942		if p == nil {
943			return errNilDst(p)
944		}
945		if code != sppb.TypeCode_FLOAT64 {
946			return errTypeMismatch(code, acode, ptr)
947		}
948		if isNull {
949			switch sp := ptr.(type) {
950			case *NullFloat64:
951				*sp = NullFloat64{}
952			case **float64:
953				*sp = nil
954			}
955			break
956		}
957		x, err := getFloat64Value(v)
958		if err != nil {
959			return err
960		}
961		switch sp := ptr.(type) {
962		case *NullFloat64:
963			sp.Valid = true
964			sp.Float64 = x
965		case **float64:
966			*sp = &x
967		}
968	case *[]NullFloat64, *[]*float64:
969		if p == nil {
970			return errNilDst(p)
971		}
972		if acode != sppb.TypeCode_FLOAT64 {
973			return errTypeMismatch(code, acode, ptr)
974		}
975		if isNull {
976			switch sp := ptr.(type) {
977			case *[]NullFloat64:
978				*sp = nil
979			case *[]*float64:
980				*sp = nil
981			}
982			break
983		}
984		x, err := getListValue(v)
985		if err != nil {
986			return err
987		}
988		switch sp := ptr.(type) {
989		case *[]NullFloat64:
990			y, err := decodeNullFloat64Array(x)
991			if err != nil {
992				return err
993			}
994			*sp = y
995		case *[]*float64:
996			y, err := decodeFloat64PointerArray(x)
997			if err != nil {
998				return err
999			}
1000			*sp = y
1001		}
1002	case *[]float64:
1003		if p == nil {
1004			return errNilDst(p)
1005		}
1006		if acode != sppb.TypeCode_FLOAT64 {
1007			return errTypeMismatch(code, acode, ptr)
1008		}
1009		if isNull {
1010			*p = nil
1011			break
1012		}
1013		x, err := getListValue(v)
1014		if err != nil {
1015			return err
1016		}
1017		y, err := decodeFloat64Array(x)
1018		if err != nil {
1019			return err
1020		}
1021		*p = y
1022	case *big.Rat:
1023		if code != sppb.TypeCode_NUMERIC {
1024			return errTypeMismatch(code, acode, ptr)
1025		}
1026		if isNull {
1027			return errDstNotForNull(ptr)
1028		}
1029		x := v.GetStringValue()
1030		y, ok := (&big.Rat{}).SetString(x)
1031		if !ok {
1032			return errUnexpectedNumericStr(x)
1033		}
1034		*p = *y
1035	case *NullNumeric:
1036		if p == nil {
1037			return errNilDst(p)
1038		}
1039		if code != sppb.TypeCode_NUMERIC {
1040			return errTypeMismatch(code, acode, ptr)
1041		}
1042		if isNull {
1043			*p = NullNumeric{}
1044			break
1045		}
1046		x := v.GetStringValue()
1047		y, ok := (&big.Rat{}).SetString(x)
1048		if !ok {
1049			return errUnexpectedNumericStr(x)
1050		}
1051		*p = NullNumeric{*y, true}
1052	case **big.Rat:
1053		if p == nil {
1054			return errNilDst(p)
1055		}
1056		if code != sppb.TypeCode_NUMERIC {
1057			return errTypeMismatch(code, acode, ptr)
1058		}
1059		if isNull {
1060			*p = nil
1061			break
1062		}
1063		x := v.GetStringValue()
1064		y, ok := (&big.Rat{}).SetString(x)
1065		if !ok {
1066			return errUnexpectedNumericStr(x)
1067		}
1068		*p = y
1069	case *[]NullNumeric, *[]*big.Rat:
1070		if p == nil {
1071			return errNilDst(p)
1072		}
1073		if acode != sppb.TypeCode_NUMERIC {
1074			return errTypeMismatch(code, acode, ptr)
1075		}
1076		if isNull {
1077			switch sp := ptr.(type) {
1078			case *[]NullNumeric:
1079				*sp = nil
1080			case *[]*big.Rat:
1081				*sp = nil
1082			}
1083			break
1084		}
1085		x, err := getListValue(v)
1086		if err != nil {
1087			return err
1088		}
1089		switch sp := ptr.(type) {
1090		case *[]NullNumeric:
1091			y, err := decodeNullNumericArray(x)
1092			if err != nil {
1093				return err
1094			}
1095			*sp = y
1096		case *[]*big.Rat:
1097			y, err := decodeNumericPointerArray(x)
1098			if err != nil {
1099				return err
1100			}
1101			*sp = y
1102		}
1103	case *[]big.Rat:
1104		if p == nil {
1105			return errNilDst(p)
1106		}
1107		if acode != sppb.TypeCode_NUMERIC {
1108			return errTypeMismatch(code, acode, ptr)
1109		}
1110		if isNull {
1111			*p = nil
1112			break
1113		}
1114		x, err := getListValue(v)
1115		if err != nil {
1116			return err
1117		}
1118		y, err := decodeNumericArray(x)
1119		if err != nil {
1120			return err
1121		}
1122		*p = y
1123	case *time.Time:
1124		var nt NullTime
1125		if isNull {
1126			return errDstNotForNull(ptr)
1127		}
1128		err := parseNullTime(v, &nt, code, isNull)
1129		if err != nil {
1130			return err
1131		}
1132		*p = nt.Time
1133	case *NullTime:
1134		err := parseNullTime(v, p, code, isNull)
1135		if err != nil {
1136			return err
1137		}
1138	case **time.Time:
1139		var nt NullTime
1140		if isNull {
1141			*p = nil
1142			break
1143		}
1144		err := parseNullTime(v, &nt, code, isNull)
1145		if err != nil {
1146			return err
1147		}
1148		*p = &nt.Time
1149	case *[]NullTime, *[]*time.Time:
1150		if p == nil {
1151			return errNilDst(p)
1152		}
1153		if acode != sppb.TypeCode_TIMESTAMP {
1154			return errTypeMismatch(code, acode, ptr)
1155		}
1156		if isNull {
1157			switch sp := ptr.(type) {
1158			case *[]NullTime:
1159				*sp = nil
1160			case *[]*time.Time:
1161				*sp = nil
1162			}
1163			break
1164		}
1165		x, err := getListValue(v)
1166		if err != nil {
1167			return err
1168		}
1169		switch sp := ptr.(type) {
1170		case *[]NullTime:
1171			y, err := decodeNullTimeArray(x)
1172			if err != nil {
1173				return err
1174			}
1175			*sp = y
1176		case *[]*time.Time:
1177			y, err := decodeTimePointerArray(x)
1178			if err != nil {
1179				return err
1180			}
1181			*sp = y
1182		}
1183	case *[]time.Time:
1184		if p == nil {
1185			return errNilDst(p)
1186		}
1187		if acode != sppb.TypeCode_TIMESTAMP {
1188			return errTypeMismatch(code, acode, ptr)
1189		}
1190		if isNull {
1191			*p = nil
1192			break
1193		}
1194		x, err := getListValue(v)
1195		if err != nil {
1196			return err
1197		}
1198		y, err := decodeTimeArray(x)
1199		if err != nil {
1200			return err
1201		}
1202		*p = y
1203	case *civil.Date:
1204		if p == nil {
1205			return errNilDst(p)
1206		}
1207		if code != sppb.TypeCode_DATE {
1208			return errTypeMismatch(code, acode, ptr)
1209		}
1210		if isNull {
1211			return errDstNotForNull(ptr)
1212		}
1213		x, err := getStringValue(v)
1214		if err != nil {
1215			return err
1216		}
1217		y, err := civil.ParseDate(x)
1218		if err != nil {
1219			return errBadEncoding(v, err)
1220		}
1221		*p = y
1222	case *NullDate, **civil.Date:
1223		if p == nil {
1224			return errNilDst(p)
1225		}
1226		if code != sppb.TypeCode_DATE {
1227			return errTypeMismatch(code, acode, ptr)
1228		}
1229		if isNull {
1230			switch sp := ptr.(type) {
1231			case *NullDate:
1232				*sp = NullDate{}
1233			case **civil.Date:
1234				*sp = nil
1235			}
1236			break
1237		}
1238		x, err := getStringValue(v)
1239		if err != nil {
1240			return err
1241		}
1242		y, err := civil.ParseDate(x)
1243		if err != nil {
1244			return errBadEncoding(v, err)
1245		}
1246		switch sp := ptr.(type) {
1247		case *NullDate:
1248			sp.Valid = true
1249			sp.Date = y
1250		case **civil.Date:
1251			*sp = &y
1252		}
1253	case *[]NullDate, *[]*civil.Date:
1254		if p == nil {
1255			return errNilDst(p)
1256		}
1257		if acode != sppb.TypeCode_DATE {
1258			return errTypeMismatch(code, acode, ptr)
1259		}
1260		if isNull {
1261			switch sp := ptr.(type) {
1262			case *[]NullDate:
1263				*sp = nil
1264			case *[]*civil.Date:
1265				*sp = nil
1266			}
1267			break
1268		}
1269		x, err := getListValue(v)
1270		if err != nil {
1271			return err
1272		}
1273		switch sp := ptr.(type) {
1274		case *[]NullDate:
1275			y, err := decodeNullDateArray(x)
1276			if err != nil {
1277				return err
1278			}
1279			*sp = y
1280		case *[]*civil.Date:
1281			y, err := decodeDatePointerArray(x)
1282			if err != nil {
1283				return err
1284			}
1285			*sp = y
1286		}
1287	case *[]civil.Date:
1288		if p == nil {
1289			return errNilDst(p)
1290		}
1291		if acode != sppb.TypeCode_DATE {
1292			return errTypeMismatch(code, acode, ptr)
1293		}
1294		if isNull {
1295			*p = nil
1296			break
1297		}
1298		x, err := getListValue(v)
1299		if err != nil {
1300			return err
1301		}
1302		y, err := decodeDateArray(x)
1303		if err != nil {
1304			return err
1305		}
1306		*p = y
1307	case *[]NullRow:
1308		if p == nil {
1309			return errNilDst(p)
1310		}
1311		if acode != sppb.TypeCode_STRUCT {
1312			return errTypeMismatch(code, acode, ptr)
1313		}
1314		if isNull {
1315			*p = nil
1316			break
1317		}
1318		x, err := getListValue(v)
1319		if err != nil {
1320			return err
1321		}
1322		y, err := decodeRowArray(t.ArrayElementType.StructType, x)
1323		if err != nil {
1324			return err
1325		}
1326		*p = y
1327	case *GenericColumnValue:
1328		*p = GenericColumnValue{Type: t, Value: v}
1329	default:
1330		// Check if the pointer is a custom type that implements spanner.Decoder
1331		// interface.
1332		if decodedVal, ok := ptr.(Decoder); ok {
1333			x, err := getGenericValue(t, v)
1334			if err != nil {
1335				return err
1336			}
1337			return decodedVal.DecodeSpanner(x)
1338		}
1339
1340		// Check if the pointer is a variant of a base type.
1341		decodableType := getDecodableSpannerType(ptr, true)
1342		if decodableType != spannerTypeUnknown {
1343			if isNull && !decodableType.supportsNull() {
1344				return errDstNotForNull(ptr)
1345			}
1346			return decodableType.decodeValueToCustomType(v, t, acode, ptr)
1347		}
1348
1349		// Check if the proto encoding is for an array of structs.
1350		if !(code == sppb.TypeCode_ARRAY && acode == sppb.TypeCode_STRUCT) {
1351			return errTypeMismatch(code, acode, ptr)
1352		}
1353		vp := reflect.ValueOf(p)
1354		if !vp.IsValid() {
1355			return errNilDst(p)
1356		}
1357		if !isPtrStructPtrSlice(vp.Type()) {
1358			// The container is not a slice of struct pointers.
1359			return fmt.Errorf("the container is not a slice of struct pointers: %v", errTypeMismatch(code, acode, ptr))
1360		}
1361		// Only use reflection for nil detection on slow path.
1362		// Also, IsNil panics on many types, so check it after the type check.
1363		if vp.IsNil() {
1364			return errNilDst(p)
1365		}
1366		if isNull {
1367			// The proto Value is encoding NULL, set the pointer to struct
1368			// slice to nil as well.
1369			vp.Elem().Set(reflect.Zero(vp.Elem().Type()))
1370			break
1371		}
1372		x, err := getListValue(v)
1373		if err != nil {
1374			return err
1375		}
1376		if err = decodeStructArray(t.ArrayElementType.StructType, x, p); err != nil {
1377			return err
1378		}
1379	}
1380	return nil
1381}
1382
1383// decodableSpannerType represents the Go types that a value from a Spanner
1384// database can be converted to.
1385type decodableSpannerType uint
1386
1387const (
1388	spannerTypeUnknown decodableSpannerType = iota
1389	spannerTypeInvalid
1390	spannerTypeNonNullString
1391	spannerTypeByteArray
1392	spannerTypeNonNullInt64
1393	spannerTypeNonNullBool
1394	spannerTypeNonNullFloat64
1395	spannerTypeNonNullNumeric
1396	spannerTypeNonNullTime
1397	spannerTypeNonNullDate
1398	spannerTypeNullString
1399	spannerTypeNullInt64
1400	spannerTypeNullBool
1401	spannerTypeNullFloat64
1402	spannerTypeNullTime
1403	spannerTypeNullDate
1404	spannerTypeNullNumeric
1405	spannerTypeArrayOfNonNullString
1406	spannerTypeArrayOfByteArray
1407	spannerTypeArrayOfNonNullInt64
1408	spannerTypeArrayOfNonNullBool
1409	spannerTypeArrayOfNonNullFloat64
1410	spannerTypeArrayOfNonNullNumeric
1411	spannerTypeArrayOfNonNullTime
1412	spannerTypeArrayOfNonNullDate
1413	spannerTypeArrayOfNullString
1414	spannerTypeArrayOfNullInt64
1415	spannerTypeArrayOfNullBool
1416	spannerTypeArrayOfNullFloat64
1417	spannerTypeArrayOfNullNumeric
1418	spannerTypeArrayOfNullTime
1419	spannerTypeArrayOfNullDate
1420)
1421
1422// supportsNull returns true for the Go types that can hold a null value from
1423// Spanner.
1424func (d decodableSpannerType) supportsNull() bool {
1425	switch d {
1426	case spannerTypeNonNullString, spannerTypeNonNullInt64, spannerTypeNonNullBool, spannerTypeNonNullFloat64, spannerTypeNonNullTime, spannerTypeNonNullDate, spannerTypeNonNullNumeric:
1427		return false
1428	default:
1429		return true
1430	}
1431}
1432
1433// The following list of types represent the struct types that represent a
1434// specific Spanner data type in Go. If a pointer to one of these types is
1435// passed to decodeValue, the client library will decode one column value into
1436// the struct. For pointers to all other struct types, the client library will
1437// treat it as a generic struct that should contain a field for each column in
1438// the result set that is being decoded.
1439
1440var typeOfNonNullTime = reflect.TypeOf(time.Time{})
1441var typeOfNonNullDate = reflect.TypeOf(civil.Date{})
1442var typeOfNonNullNumeric = reflect.TypeOf(big.Rat{})
1443var typeOfNullString = reflect.TypeOf(NullString{})
1444var typeOfNullInt64 = reflect.TypeOf(NullInt64{})
1445var typeOfNullBool = reflect.TypeOf(NullBool{})
1446var typeOfNullFloat64 = reflect.TypeOf(NullFloat64{})
1447var typeOfNullTime = reflect.TypeOf(NullTime{})
1448var typeOfNullDate = reflect.TypeOf(NullDate{})
1449var typeOfNullNumeric = reflect.TypeOf(NullNumeric{})
1450
1451// getDecodableSpannerType returns the corresponding decodableSpannerType of
1452// the given pointer.
1453func getDecodableSpannerType(ptr interface{}, isPtr bool) decodableSpannerType {
1454	var val reflect.Value
1455	var kind reflect.Kind
1456	if isPtr {
1457		val = reflect.Indirect(reflect.ValueOf(ptr))
1458	} else {
1459		val = reflect.ValueOf(ptr)
1460	}
1461	kind = val.Kind()
1462	if kind == reflect.Invalid {
1463		return spannerTypeInvalid
1464	}
1465	switch kind {
1466	case reflect.Invalid:
1467		return spannerTypeInvalid
1468	case reflect.String:
1469		return spannerTypeNonNullString
1470	case reflect.Int64:
1471		return spannerTypeNonNullInt64
1472	case reflect.Bool:
1473		return spannerTypeNonNullBool
1474	case reflect.Float64:
1475		return spannerTypeNonNullFloat64
1476	case reflect.Ptr:
1477		t := val.Type()
1478		if t.ConvertibleTo(typeOfNullNumeric) {
1479			return spannerTypeNullNumeric
1480		}
1481	case reflect.Struct:
1482		t := val.Type()
1483		if t.ConvertibleTo(typeOfNonNullNumeric) {
1484			return spannerTypeNonNullNumeric
1485		}
1486		if t.ConvertibleTo(typeOfNonNullTime) {
1487			return spannerTypeNonNullTime
1488		}
1489		if t.ConvertibleTo(typeOfNonNullDate) {
1490			return spannerTypeNonNullDate
1491		}
1492		if t.ConvertibleTo(typeOfNullString) {
1493			return spannerTypeNullString
1494		}
1495		if t.ConvertibleTo(typeOfNullInt64) {
1496			return spannerTypeNullInt64
1497		}
1498		if t.ConvertibleTo(typeOfNullBool) {
1499			return spannerTypeNullBool
1500		}
1501		if t.ConvertibleTo(typeOfNullFloat64) {
1502			return spannerTypeNullFloat64
1503		}
1504		if t.ConvertibleTo(typeOfNullTime) {
1505			return spannerTypeNullTime
1506		}
1507		if t.ConvertibleTo(typeOfNullDate) {
1508			return spannerTypeNullDate
1509		}
1510		if t.ConvertibleTo(typeOfNullNumeric) {
1511			return spannerTypeNullNumeric
1512		}
1513	case reflect.Slice:
1514		kind := val.Type().Elem().Kind()
1515		switch kind {
1516		case reflect.Invalid:
1517			return spannerTypeUnknown
1518		case reflect.String:
1519			return spannerTypeArrayOfNonNullString
1520		case reflect.Uint8:
1521			return spannerTypeByteArray
1522		case reflect.Int64:
1523			return spannerTypeArrayOfNonNullInt64
1524		case reflect.Bool:
1525			return spannerTypeArrayOfNonNullBool
1526		case reflect.Float64:
1527			return spannerTypeArrayOfNonNullFloat64
1528		case reflect.Ptr:
1529			t := val.Type().Elem()
1530			if t.ConvertibleTo(typeOfNullNumeric) {
1531				return spannerTypeArrayOfNullNumeric
1532			}
1533		case reflect.Struct:
1534			t := val.Type().Elem()
1535			if t.ConvertibleTo(typeOfNonNullNumeric) {
1536				return spannerTypeArrayOfNonNullNumeric
1537			}
1538			if t.ConvertibleTo(typeOfNonNullTime) {
1539				return spannerTypeArrayOfNonNullTime
1540			}
1541			if t.ConvertibleTo(typeOfNonNullDate) {
1542				return spannerTypeArrayOfNonNullDate
1543			}
1544			if t.ConvertibleTo(typeOfNullString) {
1545				return spannerTypeArrayOfNullString
1546			}
1547			if t.ConvertibleTo(typeOfNullInt64) {
1548				return spannerTypeArrayOfNullInt64
1549			}
1550			if t.ConvertibleTo(typeOfNullBool) {
1551				return spannerTypeArrayOfNullBool
1552			}
1553			if t.ConvertibleTo(typeOfNullFloat64) {
1554				return spannerTypeArrayOfNullFloat64
1555			}
1556			if t.ConvertibleTo(typeOfNullTime) {
1557				return spannerTypeArrayOfNullTime
1558			}
1559			if t.ConvertibleTo(typeOfNullDate) {
1560				return spannerTypeArrayOfNullDate
1561			}
1562			if t.ConvertibleTo(typeOfNullNumeric) {
1563				return spannerTypeArrayOfNullNumeric
1564			}
1565		case reflect.Slice:
1566			// The only array-of-array type that is supported is [][]byte.
1567			kind := val.Type().Elem().Elem().Kind()
1568			switch kind {
1569			case reflect.Uint8:
1570				return spannerTypeArrayOfByteArray
1571			}
1572		}
1573	}
1574	// Not convertible to a known base type.
1575	return spannerTypeUnknown
1576}
1577
1578// decodeValueToCustomType decodes a protobuf Value into a pointer to a Go
1579// value. It must be possible to convert the value to the type pointed to by
1580// the pointer.
1581func (dsc decodableSpannerType) decodeValueToCustomType(v *proto3.Value, t *sppb.Type, acode sppb.TypeCode, ptr interface{}) error {
1582	code := t.Code
1583	_, isNull := v.Kind.(*proto3.Value_NullValue)
1584	if dsc == spannerTypeInvalid {
1585		return errNilDst(ptr)
1586	}
1587	if isNull && !dsc.supportsNull() {
1588		return errDstNotForNull(ptr)
1589	}
1590
1591	var result interface{}
1592	switch dsc {
1593	case spannerTypeNonNullString, spannerTypeNullString:
1594		if code != sppb.TypeCode_STRING {
1595			return errTypeMismatch(code, acode, ptr)
1596		}
1597		if isNull {
1598			result = &NullString{}
1599			break
1600		}
1601		x, err := getStringValue(v)
1602		if err != nil {
1603			return err
1604		}
1605		if dsc == spannerTypeNonNullString {
1606			result = &x
1607		} else {
1608			result = &NullString{x, !isNull}
1609		}
1610	case spannerTypeByteArray:
1611		if code != sppb.TypeCode_BYTES {
1612			return errTypeMismatch(code, acode, ptr)
1613		}
1614		if isNull {
1615			result = []byte(nil)
1616			break
1617		}
1618		x, err := getStringValue(v)
1619		if err != nil {
1620			return err
1621		}
1622		y, err := base64.StdEncoding.DecodeString(x)
1623		if err != nil {
1624			return errBadEncoding(v, err)
1625		}
1626		result = y
1627	case spannerTypeNonNullInt64, spannerTypeNullInt64:
1628		if code != sppb.TypeCode_INT64 {
1629			return errTypeMismatch(code, acode, ptr)
1630		}
1631		if isNull {
1632			result = &NullInt64{}
1633			break
1634		}
1635		x, err := getStringValue(v)
1636		if err != nil {
1637			return err
1638		}
1639		y, err := strconv.ParseInt(x, 10, 64)
1640		if err != nil {
1641			return errBadEncoding(v, err)
1642		}
1643		if dsc == spannerTypeNonNullInt64 {
1644			result = &y
1645		} else {
1646			result = &NullInt64{y, !isNull}
1647		}
1648	case spannerTypeNonNullBool, spannerTypeNullBool:
1649		if code != sppb.TypeCode_BOOL {
1650			return errTypeMismatch(code, acode, ptr)
1651		}
1652		if isNull {
1653			result = &NullBool{}
1654			break
1655		}
1656		x, err := getBoolValue(v)
1657		if err != nil {
1658			return err
1659		}
1660		if dsc == spannerTypeNonNullBool {
1661			result = &x
1662		} else {
1663			result = &NullBool{x, !isNull}
1664		}
1665	case spannerTypeNonNullFloat64, spannerTypeNullFloat64:
1666		if code != sppb.TypeCode_FLOAT64 {
1667			return errTypeMismatch(code, acode, ptr)
1668		}
1669		if isNull {
1670			result = &NullFloat64{}
1671			break
1672		}
1673		x, err := getFloat64Value(v)
1674		if err != nil {
1675			return err
1676		}
1677		if dsc == spannerTypeNonNullFloat64 {
1678			result = &x
1679		} else {
1680			result = &NullFloat64{x, !isNull}
1681		}
1682	case spannerTypeNonNullNumeric, spannerTypeNullNumeric:
1683		if code != sppb.TypeCode_NUMERIC {
1684			return errTypeMismatch(code, acode, ptr)
1685		}
1686		if isNull {
1687			result = &NullNumeric{}
1688			break
1689		}
1690		x := v.GetStringValue()
1691		y, ok := (&big.Rat{}).SetString(x)
1692		if !ok {
1693			return errUnexpectedNumericStr(x)
1694		}
1695		if dsc == spannerTypeNonNullNumeric {
1696			result = y
1697		} else {
1698			result = &NullNumeric{*y, true}
1699		}
1700	case spannerTypeNonNullTime, spannerTypeNullTime:
1701		var nt NullTime
1702		err := parseNullTime(v, &nt, code, isNull)
1703		if err != nil {
1704			return err
1705		}
1706		if dsc == spannerTypeNonNullTime {
1707			result = &nt.Time
1708		} else {
1709			result = &nt
1710		}
1711	case spannerTypeNonNullDate, spannerTypeNullDate:
1712		if code != sppb.TypeCode_DATE {
1713			return errTypeMismatch(code, acode, ptr)
1714		}
1715		if isNull {
1716			result = &NullDate{}
1717			break
1718		}
1719		x, err := getStringValue(v)
1720		if err != nil {
1721			return err
1722		}
1723		y, err := civil.ParseDate(x)
1724		if err != nil {
1725			return errBadEncoding(v, err)
1726		}
1727		if dsc == spannerTypeNonNullDate {
1728			result = &y
1729		} else {
1730			result = &NullDate{y, !isNull}
1731		}
1732	case spannerTypeArrayOfNonNullString, spannerTypeArrayOfNullString:
1733		if acode != sppb.TypeCode_STRING {
1734			return errTypeMismatch(code, acode, ptr)
1735		}
1736		if isNull {
1737			ptr = nil
1738			return nil
1739		}
1740		x, err := getListValue(v)
1741		if err != nil {
1742			return err
1743		}
1744		y, err := decodeGenericArray(reflect.TypeOf(ptr).Elem(), x, stringType(), "STRING")
1745		if err != nil {
1746			return err
1747		}
1748		result = y
1749	case spannerTypeArrayOfByteArray:
1750		if acode != sppb.TypeCode_BYTES {
1751			return errTypeMismatch(code, acode, ptr)
1752		}
1753		if isNull {
1754			ptr = nil
1755			return nil
1756		}
1757		x, err := getListValue(v)
1758		if err != nil {
1759			return err
1760		}
1761		y, err := decodeGenericArray(reflect.TypeOf(ptr).Elem(), x, bytesType(), "BYTES")
1762		if err != nil {
1763			return err
1764		}
1765		result = y
1766	case spannerTypeArrayOfNonNullInt64, spannerTypeArrayOfNullInt64:
1767		if acode != sppb.TypeCode_INT64 {
1768			return errTypeMismatch(code, acode, ptr)
1769		}
1770		if isNull {
1771			ptr = nil
1772			return nil
1773		}
1774		x, err := getListValue(v)
1775		if err != nil {
1776			return err
1777		}
1778		y, err := decodeGenericArray(reflect.TypeOf(ptr).Elem(), x, intType(), "INT64")
1779		if err != nil {
1780			return err
1781		}
1782		result = y
1783	case spannerTypeArrayOfNonNullBool, spannerTypeArrayOfNullBool:
1784		if acode != sppb.TypeCode_BOOL {
1785			return errTypeMismatch(code, acode, ptr)
1786		}
1787		if isNull {
1788			ptr = nil
1789			return nil
1790		}
1791		x, err := getListValue(v)
1792		if err != nil {
1793			return err
1794		}
1795		y, err := decodeGenericArray(reflect.TypeOf(ptr).Elem(), x, boolType(), "BOOL")
1796		if err != nil {
1797			return err
1798		}
1799		result = y
1800	case spannerTypeArrayOfNonNullFloat64, spannerTypeArrayOfNullFloat64:
1801		if acode != sppb.TypeCode_FLOAT64 {
1802			return errTypeMismatch(code, acode, ptr)
1803		}
1804		if isNull {
1805			ptr = nil
1806			return nil
1807		}
1808		x, err := getListValue(v)
1809		if err != nil {
1810			return err
1811		}
1812		y, err := decodeGenericArray(reflect.TypeOf(ptr).Elem(), x, floatType(), "FLOAT64")
1813		if err != nil {
1814			return err
1815		}
1816		result = y
1817	case spannerTypeArrayOfNonNullNumeric, spannerTypeArrayOfNullNumeric:
1818		if acode != sppb.TypeCode_NUMERIC {
1819			return errTypeMismatch(code, acode, ptr)
1820		}
1821		if isNull {
1822			ptr = nil
1823			return nil
1824		}
1825		x, err := getListValue(v)
1826		if err != nil {
1827			return err
1828		}
1829		y, err := decodeGenericArray(reflect.TypeOf(ptr).Elem(), x, numericType(), "NUMERIC")
1830		if err != nil {
1831			return err
1832		}
1833		result = y
1834	case spannerTypeArrayOfNonNullTime, spannerTypeArrayOfNullTime:
1835		if acode != sppb.TypeCode_TIMESTAMP {
1836			return errTypeMismatch(code, acode, ptr)
1837		}
1838		if isNull {
1839			ptr = nil
1840			return nil
1841		}
1842		x, err := getListValue(v)
1843		if err != nil {
1844			return err
1845		}
1846		y, err := decodeGenericArray(reflect.TypeOf(ptr).Elem(), x, timeType(), "TIMESTAMP")
1847		if err != nil {
1848			return err
1849		}
1850		result = y
1851	case spannerTypeArrayOfNonNullDate, spannerTypeArrayOfNullDate:
1852		if acode != sppb.TypeCode_DATE {
1853			return errTypeMismatch(code, acode, ptr)
1854		}
1855		if isNull {
1856			ptr = nil
1857			return nil
1858		}
1859		x, err := getListValue(v)
1860		if err != nil {
1861			return err
1862		}
1863		y, err := decodeGenericArray(reflect.TypeOf(ptr).Elem(), x, dateType(), "DATE")
1864		if err != nil {
1865			return err
1866		}
1867		result = y
1868	default:
1869		// This should not be possible.
1870		return fmt.Errorf("unknown decodable type found: %v", dsc)
1871	}
1872	source := reflect.Indirect(reflect.ValueOf(result))
1873	destination := reflect.Indirect(reflect.ValueOf(ptr))
1874	destination.Set(source.Convert(destination.Type()))
1875	return nil
1876}
1877
1878// errSrvVal returns an error for getting a wrong source protobuf value in decoding.
1879func errSrcVal(v *proto3.Value, want string) error {
1880	return spannerErrorf(codes.FailedPrecondition, "cannot use %v(Kind: %T) as %s Value",
1881		v, v.GetKind(), want)
1882}
1883
1884// getStringValue returns the string value encoded in proto3.Value v whose
1885// kind is proto3.Value_StringValue.
1886func getStringValue(v *proto3.Value) (string, error) {
1887	if x, ok := v.GetKind().(*proto3.Value_StringValue); ok && x != nil {
1888		return x.StringValue, nil
1889	}
1890	return "", errSrcVal(v, "String")
1891}
1892
1893// getBoolValue returns the bool value encoded in proto3.Value v whose
1894// kind is proto3.Value_BoolValue.
1895func getBoolValue(v *proto3.Value) (bool, error) {
1896	if x, ok := v.GetKind().(*proto3.Value_BoolValue); ok && x != nil {
1897		return x.BoolValue, nil
1898	}
1899	return false, errSrcVal(v, "Bool")
1900}
1901
1902// getListValue returns the proto3.ListValue contained in proto3.Value v whose
1903// kind is proto3.Value_ListValue.
1904func getListValue(v *proto3.Value) (*proto3.ListValue, error) {
1905	if x, ok := v.GetKind().(*proto3.Value_ListValue); ok && x != nil {
1906		return x.ListValue, nil
1907	}
1908	return nil, errSrcVal(v, "List")
1909}
1910
1911// getGenericValue returns the interface{} value encoded in proto3.Value.
1912func getGenericValue(t *sppb.Type, v *proto3.Value) (interface{}, error) {
1913	switch x := v.GetKind().(type) {
1914	case *proto3.Value_NumberValue:
1915		return x.NumberValue, nil
1916	case *proto3.Value_BoolValue:
1917		return x.BoolValue, nil
1918	case *proto3.Value_StringValue:
1919		return x.StringValue, nil
1920	case *proto3.Value_NullValue:
1921		return getTypedNil(t)
1922	default:
1923		return 0, errSrcVal(v, "Number, Bool, String")
1924	}
1925}
1926
1927func getTypedNil(t *sppb.Type) (interface{}, error) {
1928	switch t.Code {
1929	case sppb.TypeCode_FLOAT64:
1930		var f *float64
1931		return f, nil
1932	case sppb.TypeCode_BOOL:
1933		var b *bool
1934		return b, nil
1935	default:
1936		// The encoding for most types is string, except for the ones listed
1937		// above.
1938		var s *string
1939		return s, nil
1940	}
1941}
1942
1943// errUnexpectedNumericStr returns error for decoder getting an unexpected
1944// string for representing special numeric values.
1945func errUnexpectedNumericStr(s string) error {
1946	return spannerErrorf(codes.FailedPrecondition, "unexpected string value %q for numeric number", s)
1947}
1948
1949// errUnexpectedFloat64Str returns error for decoder getting an unexpected
1950// string for representing special float values.
1951func errUnexpectedFloat64Str(s string) error {
1952	return spannerErrorf(codes.FailedPrecondition, "unexpected string value %q for float64 number", s)
1953}
1954
1955// getFloat64Value returns the float64 value encoded in proto3.Value v whose
1956// kind is proto3.Value_NumberValue / proto3.Value_StringValue.
1957// Cloud Spanner uses string to encode NaN, Infinity and -Infinity.
1958func getFloat64Value(v *proto3.Value) (float64, error) {
1959	switch x := v.GetKind().(type) {
1960	case *proto3.Value_NumberValue:
1961		if x == nil {
1962			break
1963		}
1964		return x.NumberValue, nil
1965	case *proto3.Value_StringValue:
1966		if x == nil {
1967			break
1968		}
1969		switch x.StringValue {
1970		case "NaN":
1971			return math.NaN(), nil
1972		case "Infinity":
1973			return math.Inf(1), nil
1974		case "-Infinity":
1975			return math.Inf(-1), nil
1976		default:
1977			return 0, errUnexpectedFloat64Str(x.StringValue)
1978		}
1979	}
1980	return 0, errSrcVal(v, "Number")
1981}
1982
1983// errNilListValue returns error for unexpected nil ListValue in decoding Cloud Spanner ARRAYs.
1984func errNilListValue(sqlType string) error {
1985	return spannerErrorf(codes.FailedPrecondition, "unexpected nil ListValue in decoding %v array", sqlType)
1986}
1987
1988// errDecodeArrayElement returns error for failure in decoding single array element.
1989func errDecodeArrayElement(i int, v proto.Message, sqlType string, err error) error {
1990	var se *Error
1991	if !errorAs(err, &se) {
1992		return spannerErrorf(codes.Unknown,
1993			"cannot decode %v(array element %v) as %v, error = <%v>", v, i, sqlType, err)
1994	}
1995	se.decorate(fmt.Sprintf("cannot decode %v(array element %v) as %v", v, i, sqlType))
1996	return se
1997}
1998
1999// decodeGenericArray decodes proto3.ListValue pb into a slice which type is
2000// determined through reflection.
2001func decodeGenericArray(tp reflect.Type, pb *proto3.ListValue, t *sppb.Type, sqlType string) (interface{}, error) {
2002	if pb == nil {
2003		return nil, errNilListValue(sqlType)
2004	}
2005	a := reflect.MakeSlice(tp, len(pb.Values), len(pb.Values))
2006	for i, v := range pb.Values {
2007		if err := decodeValue(v, t, a.Index(i).Addr().Interface()); err != nil {
2008			return nil, errDecodeArrayElement(i, v, "STRING", err)
2009		}
2010	}
2011	return a.Interface(), nil
2012}
2013
2014// decodeNullStringArray decodes proto3.ListValue pb into a NullString slice.
2015func decodeNullStringArray(pb *proto3.ListValue) ([]NullString, error) {
2016	if pb == nil {
2017		return nil, errNilListValue("STRING")
2018	}
2019	a := make([]NullString, len(pb.Values))
2020	for i, v := range pb.Values {
2021		if err := decodeValue(v, stringType(), &a[i]); err != nil {
2022			return nil, errDecodeArrayElement(i, v, "STRING", err)
2023		}
2024	}
2025	return a, nil
2026}
2027
2028// decodeStringPointerArray decodes proto3.ListValue pb into a *string slice.
2029func decodeStringPointerArray(pb *proto3.ListValue) ([]*string, error) {
2030	if pb == nil {
2031		return nil, errNilListValue("STRING")
2032	}
2033	a := make([]*string, len(pb.Values))
2034	for i, v := range pb.Values {
2035		if err := decodeValue(v, stringType(), &a[i]); err != nil {
2036			return nil, errDecodeArrayElement(i, v, "STRING", err)
2037		}
2038	}
2039	return a, nil
2040}
2041
2042// decodeStringArray decodes proto3.ListValue pb into a string slice.
2043func decodeStringArray(pb *proto3.ListValue) ([]string, error) {
2044	if pb == nil {
2045		return nil, errNilListValue("STRING")
2046	}
2047	a := make([]string, len(pb.Values))
2048	st := stringType()
2049	for i, v := range pb.Values {
2050		if err := decodeValue(v, st, &a[i]); err != nil {
2051			return nil, errDecodeArrayElement(i, v, "STRING", err)
2052		}
2053	}
2054	return a, nil
2055}
2056
2057// decodeNullInt64Array decodes proto3.ListValue pb into a NullInt64 slice.
2058func decodeNullInt64Array(pb *proto3.ListValue) ([]NullInt64, error) {
2059	if pb == nil {
2060		return nil, errNilListValue("INT64")
2061	}
2062	a := make([]NullInt64, len(pb.Values))
2063	for i, v := range pb.Values {
2064		if err := decodeValue(v, intType(), &a[i]); err != nil {
2065			return nil, errDecodeArrayElement(i, v, "INT64", err)
2066		}
2067	}
2068	return a, nil
2069}
2070
2071// decodeInt64PointerArray decodes proto3.ListValue pb into a *int64 slice.
2072func decodeInt64PointerArray(pb *proto3.ListValue) ([]*int64, error) {
2073	if pb == nil {
2074		return nil, errNilListValue("INT64")
2075	}
2076	a := make([]*int64, len(pb.Values))
2077	for i, v := range pb.Values {
2078		if err := decodeValue(v, intType(), &a[i]); err != nil {
2079			return nil, errDecodeArrayElement(i, v, "INT64", err)
2080		}
2081	}
2082	return a, nil
2083}
2084
2085// decodeInt64Array decodes proto3.ListValue pb into a int64 slice.
2086func decodeInt64Array(pb *proto3.ListValue) ([]int64, error) {
2087	if pb == nil {
2088		return nil, errNilListValue("INT64")
2089	}
2090	a := make([]int64, len(pb.Values))
2091	for i, v := range pb.Values {
2092		if err := decodeValue(v, intType(), &a[i]); err != nil {
2093			return nil, errDecodeArrayElement(i, v, "INT64", err)
2094		}
2095	}
2096	return a, nil
2097}
2098
2099// decodeNullBoolArray decodes proto3.ListValue pb into a NullBool slice.
2100func decodeNullBoolArray(pb *proto3.ListValue) ([]NullBool, error) {
2101	if pb == nil {
2102		return nil, errNilListValue("BOOL")
2103	}
2104	a := make([]NullBool, len(pb.Values))
2105	for i, v := range pb.Values {
2106		if err := decodeValue(v, boolType(), &a[i]); err != nil {
2107			return nil, errDecodeArrayElement(i, v, "BOOL", err)
2108		}
2109	}
2110	return a, nil
2111}
2112
2113// decodeBoolPointerArray decodes proto3.ListValue pb into a *bool slice.
2114func decodeBoolPointerArray(pb *proto3.ListValue) ([]*bool, error) {
2115	if pb == nil {
2116		return nil, errNilListValue("BOOL")
2117	}
2118	a := make([]*bool, len(pb.Values))
2119	for i, v := range pb.Values {
2120		if err := decodeValue(v, boolType(), &a[i]); err != nil {
2121			return nil, errDecodeArrayElement(i, v, "BOOL", err)
2122		}
2123	}
2124	return a, nil
2125}
2126
2127// decodeBoolArray decodes proto3.ListValue pb into a bool slice.
2128func decodeBoolArray(pb *proto3.ListValue) ([]bool, error) {
2129	if pb == nil {
2130		return nil, errNilListValue("BOOL")
2131	}
2132	a := make([]bool, len(pb.Values))
2133	for i, v := range pb.Values {
2134		if err := decodeValue(v, boolType(), &a[i]); err != nil {
2135			return nil, errDecodeArrayElement(i, v, "BOOL", err)
2136		}
2137	}
2138	return a, nil
2139}
2140
2141// decodeNullFloat64Array decodes proto3.ListValue pb into a NullFloat64 slice.
2142func decodeNullFloat64Array(pb *proto3.ListValue) ([]NullFloat64, error) {
2143	if pb == nil {
2144		return nil, errNilListValue("FLOAT64")
2145	}
2146	a := make([]NullFloat64, len(pb.Values))
2147	for i, v := range pb.Values {
2148		if err := decodeValue(v, floatType(), &a[i]); err != nil {
2149			return nil, errDecodeArrayElement(i, v, "FLOAT64", err)
2150		}
2151	}
2152	return a, nil
2153}
2154
2155// decodeFloat64PointerArray decodes proto3.ListValue pb into a *float slice.
2156func decodeFloat64PointerArray(pb *proto3.ListValue) ([]*float64, error) {
2157	if pb == nil {
2158		return nil, errNilListValue("FLOAT64")
2159	}
2160	a := make([]*float64, len(pb.Values))
2161	for i, v := range pb.Values {
2162		if err := decodeValue(v, floatType(), &a[i]); err != nil {
2163			return nil, errDecodeArrayElement(i, v, "FLOAT64", err)
2164		}
2165	}
2166	return a, nil
2167}
2168
2169// decodeFloat64Array decodes proto3.ListValue pb into a float64 slice.
2170func decodeFloat64Array(pb *proto3.ListValue) ([]float64, error) {
2171	if pb == nil {
2172		return nil, errNilListValue("FLOAT64")
2173	}
2174	a := make([]float64, len(pb.Values))
2175	for i, v := range pb.Values {
2176		if err := decodeValue(v, floatType(), &a[i]); err != nil {
2177			return nil, errDecodeArrayElement(i, v, "FLOAT64", err)
2178		}
2179	}
2180	return a, nil
2181}
2182
2183// decodeNullNumericArray decodes proto3.ListValue pb into a NullNumeric slice.
2184func decodeNullNumericArray(pb *proto3.ListValue) ([]NullNumeric, error) {
2185	if pb == nil {
2186		return nil, errNilListValue("NUMERIC")
2187	}
2188	a := make([]NullNumeric, len(pb.Values))
2189	for i, v := range pb.Values {
2190		if err := decodeValue(v, numericType(), &a[i]); err != nil {
2191			return nil, errDecodeArrayElement(i, v, "NUMERIC", err)
2192		}
2193	}
2194	return a, nil
2195}
2196
2197// decodeNumericPointerArray decodes proto3.ListValue pb into a *big.Rat slice.
2198func decodeNumericPointerArray(pb *proto3.ListValue) ([]*big.Rat, error) {
2199	if pb == nil {
2200		return nil, errNilListValue("NUMERIC")
2201	}
2202	a := make([]*big.Rat, len(pb.Values))
2203	for i, v := range pb.Values {
2204		if err := decodeValue(v, numericType(), &a[i]); err != nil {
2205			return nil, errDecodeArrayElement(i, v, "NUMERIC", err)
2206		}
2207	}
2208	return a, nil
2209}
2210
2211// decodeNumericArray decodes proto3.ListValue pb into a big.Rat slice.
2212func decodeNumericArray(pb *proto3.ListValue) ([]big.Rat, error) {
2213	if pb == nil {
2214		return nil, errNilListValue("NUMERIC")
2215	}
2216	a := make([]big.Rat, len(pb.Values))
2217	for i, v := range pb.Values {
2218		if err := decodeValue(v, numericType(), &a[i]); err != nil {
2219			return nil, errDecodeArrayElement(i, v, "NUMERIC", err)
2220		}
2221	}
2222	return a, nil
2223}
2224
2225// decodeByteArray decodes proto3.ListValue pb into a slice of byte slice.
2226func decodeByteArray(pb *proto3.ListValue) ([][]byte, error) {
2227	if pb == nil {
2228		return nil, errNilListValue("BYTES")
2229	}
2230	a := make([][]byte, len(pb.Values))
2231	for i, v := range pb.Values {
2232		if err := decodeValue(v, bytesType(), &a[i]); err != nil {
2233			return nil, errDecodeArrayElement(i, v, "BYTES", err)
2234		}
2235	}
2236	return a, nil
2237}
2238
2239// decodeNullTimeArray decodes proto3.ListValue pb into a NullTime slice.
2240func decodeNullTimeArray(pb *proto3.ListValue) ([]NullTime, error) {
2241	if pb == nil {
2242		return nil, errNilListValue("TIMESTAMP")
2243	}
2244	a := make([]NullTime, len(pb.Values))
2245	for i, v := range pb.Values {
2246		if err := decodeValue(v, timeType(), &a[i]); err != nil {
2247			return nil, errDecodeArrayElement(i, v, "TIMESTAMP", err)
2248		}
2249	}
2250	return a, nil
2251}
2252
2253// decodeTimePointerArray decodes proto3.ListValue pb into a NullTime slice.
2254func decodeTimePointerArray(pb *proto3.ListValue) ([]*time.Time, error) {
2255	if pb == nil {
2256		return nil, errNilListValue("TIMESTAMP")
2257	}
2258	a := make([]*time.Time, len(pb.Values))
2259	for i, v := range pb.Values {
2260		if err := decodeValue(v, timeType(), &a[i]); err != nil {
2261			return nil, errDecodeArrayElement(i, v, "TIMESTAMP", err)
2262		}
2263	}
2264	return a, nil
2265}
2266
2267// decodeTimeArray decodes proto3.ListValue pb into a time.Time slice.
2268func decodeTimeArray(pb *proto3.ListValue) ([]time.Time, error) {
2269	if pb == nil {
2270		return nil, errNilListValue("TIMESTAMP")
2271	}
2272	a := make([]time.Time, len(pb.Values))
2273	for i, v := range pb.Values {
2274		if err := decodeValue(v, timeType(), &a[i]); err != nil {
2275			return nil, errDecodeArrayElement(i, v, "TIMESTAMP", err)
2276		}
2277	}
2278	return a, nil
2279}
2280
2281// decodeNullDateArray decodes proto3.ListValue pb into a NullDate slice.
2282func decodeNullDateArray(pb *proto3.ListValue) ([]NullDate, error) {
2283	if pb == nil {
2284		return nil, errNilListValue("DATE")
2285	}
2286	a := make([]NullDate, len(pb.Values))
2287	for i, v := range pb.Values {
2288		if err := decodeValue(v, dateType(), &a[i]); err != nil {
2289			return nil, errDecodeArrayElement(i, v, "DATE", err)
2290		}
2291	}
2292	return a, nil
2293}
2294
2295// decodeDatePointerArray decodes proto3.ListValue pb into a *civil.Date slice.
2296func decodeDatePointerArray(pb *proto3.ListValue) ([]*civil.Date, error) {
2297	if pb == nil {
2298		return nil, errNilListValue("DATE")
2299	}
2300	a := make([]*civil.Date, len(pb.Values))
2301	for i, v := range pb.Values {
2302		if err := decodeValue(v, dateType(), &a[i]); err != nil {
2303			return nil, errDecodeArrayElement(i, v, "DATE", err)
2304		}
2305	}
2306	return a, nil
2307}
2308
2309// decodeDateArray decodes proto3.ListValue pb into a civil.Date slice.
2310func decodeDateArray(pb *proto3.ListValue) ([]civil.Date, error) {
2311	if pb == nil {
2312		return nil, errNilListValue("DATE")
2313	}
2314	a := make([]civil.Date, len(pb.Values))
2315	for i, v := range pb.Values {
2316		if err := decodeValue(v, dateType(), &a[i]); err != nil {
2317			return nil, errDecodeArrayElement(i, v, "DATE", err)
2318		}
2319	}
2320	return a, nil
2321}
2322
2323func errNotStructElement(i int, v *proto3.Value) error {
2324	return errDecodeArrayElement(i, v, "STRUCT",
2325		spannerErrorf(codes.FailedPrecondition, "%v(type: %T) doesn't encode Cloud Spanner STRUCT", v, v))
2326}
2327
2328// decodeRowArray decodes proto3.ListValue pb into a NullRow slice according to
2329// the structural information given in sppb.StructType ty.
2330func decodeRowArray(ty *sppb.StructType, pb *proto3.ListValue) ([]NullRow, error) {
2331	if pb == nil {
2332		return nil, errNilListValue("STRUCT")
2333	}
2334	a := make([]NullRow, len(pb.Values))
2335	for i := range pb.Values {
2336		switch v := pb.Values[i].GetKind().(type) {
2337		case *proto3.Value_ListValue:
2338			a[i] = NullRow{
2339				Row: Row{
2340					fields: ty.Fields,
2341					vals:   v.ListValue.Values,
2342				},
2343				Valid: true,
2344			}
2345		// Null elements not currently supported by the server, see
2346		// https://cloud.google.com/spanner/docs/query-syntax#using-structs-with-select
2347		case *proto3.Value_NullValue:
2348			// no-op, a[i] is NullRow{} already
2349		default:
2350			return nil, errNotStructElement(i, pb.Values[i])
2351		}
2352	}
2353	return a, nil
2354}
2355
2356// errNilSpannerStructType returns error for unexpected nil Cloud Spanner STRUCT
2357// schema type in decoding.
2358func errNilSpannerStructType() error {
2359	return spannerErrorf(codes.FailedPrecondition, "unexpected nil StructType in decoding Cloud Spanner STRUCT")
2360}
2361
2362// errUnnamedField returns error for decoding a Cloud Spanner STRUCT with
2363// unnamed field into a Go struct.
2364func errUnnamedField(ty *sppb.StructType, i int) error {
2365	return spannerErrorf(codes.InvalidArgument, "unnamed field %v in Cloud Spanner STRUCT %+v", i, ty)
2366}
2367
2368// errNoOrDupGoField returns error for decoding a Cloud Spanner
2369// STRUCT into a Go struct which is either missing a field, or has duplicate
2370// fields.
2371func errNoOrDupGoField(s interface{}, f string) error {
2372	return spannerErrorf(codes.InvalidArgument, "Go struct %+v(type %T) has no or duplicate fields for Cloud Spanner STRUCT field %v", s, s, f)
2373}
2374
2375// errDupColNames returns error for duplicated Cloud Spanner STRUCT field names
2376// found in decoding a Cloud Spanner STRUCT into a Go struct.
2377func errDupSpannerField(f string, ty *sppb.StructType) error {
2378	return spannerErrorf(codes.InvalidArgument, "duplicated field name %q in Cloud Spanner STRUCT %+v", f, ty)
2379}
2380
2381// errDecodeStructField returns error for failure in decoding a single field of
2382// a Cloud Spanner STRUCT.
2383func errDecodeStructField(ty *sppb.StructType, f string, err error) error {
2384	var se *Error
2385	if !errorAs(err, &se) {
2386		return spannerErrorf(codes.Unknown,
2387			"cannot decode field %v of Cloud Spanner STRUCT %+v, error = <%v>", f, ty, err)
2388	}
2389	se.decorate(fmt.Sprintf("cannot decode field %v of Cloud Spanner STRUCT %+v", f, ty))
2390	return se
2391}
2392
2393// decodeStruct decodes proto3.ListValue pb into struct referenced by pointer
2394// ptr, according to
2395// the structural information given in sppb.StructType ty.
2396func decodeStruct(ty *sppb.StructType, pb *proto3.ListValue, ptr interface{}) error {
2397	if reflect.ValueOf(ptr).IsNil() {
2398		return errNilDst(ptr)
2399	}
2400	if ty == nil {
2401		return errNilSpannerStructType()
2402	}
2403	// t holds the structural information of ptr.
2404	t := reflect.TypeOf(ptr).Elem()
2405	// v is the actual value that ptr points to.
2406	v := reflect.ValueOf(ptr).Elem()
2407
2408	fields, err := fieldCache.Fields(t)
2409	if err != nil {
2410		return ToSpannerError(err)
2411	}
2412	seen := map[string]bool{}
2413	for i, f := range ty.Fields {
2414		if f.Name == "" {
2415			return errUnnamedField(ty, i)
2416		}
2417		sf := fields.Match(f.Name)
2418		if sf == nil {
2419			return errNoOrDupGoField(ptr, f.Name)
2420		}
2421		if seen[f.Name] {
2422			// We don't allow duplicated field name.
2423			return errDupSpannerField(f.Name, ty)
2424		}
2425		// Try to decode a single field.
2426		if err := decodeValue(pb.Values[i], f.Type, v.FieldByIndex(sf.Index).Addr().Interface()); err != nil {
2427			return errDecodeStructField(ty, f.Name, err)
2428		}
2429		// Mark field f.Name as processed.
2430		seen[f.Name] = true
2431	}
2432	return nil
2433}
2434
2435// isPtrStructPtrSlice returns true if ptr is a pointer to a slice of struct pointers.
2436func isPtrStructPtrSlice(t reflect.Type) bool {
2437	if t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Slice {
2438		// t is not a pointer to a slice.
2439		return false
2440	}
2441	if t = t.Elem(); t.Elem().Kind() != reflect.Ptr || t.Elem().Elem().Kind() != reflect.Struct {
2442		// the slice that t points to is not a slice of struct pointers.
2443		return false
2444	}
2445	return true
2446}
2447
2448// decodeStructArray decodes proto3.ListValue pb into struct slice referenced by
2449// pointer ptr, according to the
2450// structural information given in a sppb.StructType.
2451func decodeStructArray(ty *sppb.StructType, pb *proto3.ListValue, ptr interface{}) error {
2452	if pb == nil {
2453		return errNilListValue("STRUCT")
2454	}
2455	// Type of the struct pointers stored in the slice that ptr points to.
2456	ts := reflect.TypeOf(ptr).Elem().Elem()
2457	// The slice that ptr points to, might be nil at this point.
2458	v := reflect.ValueOf(ptr).Elem()
2459	// Allocate empty slice.
2460	v.Set(reflect.MakeSlice(v.Type(), 0, len(pb.Values)))
2461	// Decode every struct in pb.Values.
2462	for i, pv := range pb.Values {
2463		// Check if pv is a NULL value.
2464		if _, isNull := pv.Kind.(*proto3.Value_NullValue); isNull {
2465			// Append a nil pointer to the slice.
2466			v.Set(reflect.Append(v, reflect.New(ts).Elem()))
2467			continue
2468		}
2469		// Allocate empty struct.
2470		s := reflect.New(ts.Elem())
2471		// Get proto3.ListValue l from proto3.Value pv.
2472		l, err := getListValue(pv)
2473		if err != nil {
2474			return errDecodeArrayElement(i, pv, "STRUCT", err)
2475		}
2476		// Decode proto3.ListValue l into struct referenced by s.Interface().
2477		if err = decodeStruct(ty, l, s.Interface()); err != nil {
2478			return errDecodeArrayElement(i, pv, "STRUCT", err)
2479		}
2480		// Append the decoded struct back into the slice.
2481		v.Set(reflect.Append(v, s))
2482	}
2483	return nil
2484}
2485
2486// errEncoderUnsupportedType returns error for not being able to encode a value
2487// of certain type.
2488func errEncoderUnsupportedType(v interface{}) error {
2489	return spannerErrorf(codes.InvalidArgument, "client doesn't support type %T", v)
2490}
2491
2492// encodeValue encodes a Go native type into a proto3.Value.
2493func encodeValue(v interface{}) (*proto3.Value, *sppb.Type, error) {
2494	pb := &proto3.Value{
2495		Kind: &proto3.Value_NullValue{NullValue: proto3.NullValue_NULL_VALUE},
2496	}
2497	var pt *sppb.Type
2498	var err error
2499	switch v := v.(type) {
2500	case nil:
2501	case string:
2502		pb.Kind = stringKind(v)
2503		pt = stringType()
2504	case NullString:
2505		if v.Valid {
2506			return encodeValue(v.StringVal)
2507		}
2508		pt = stringType()
2509	case []string:
2510		if v != nil {
2511			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2512			if err != nil {
2513				return nil, nil, err
2514			}
2515		}
2516		pt = listType(stringType())
2517	case []NullString:
2518		if v != nil {
2519			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2520			if err != nil {
2521				return nil, nil, err
2522			}
2523		}
2524		pt = listType(stringType())
2525	case *string:
2526		if v != nil {
2527			return encodeValue(*v)
2528		}
2529		pt = stringType()
2530	case []*string:
2531		if v != nil {
2532			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2533			if err != nil {
2534				return nil, nil, err
2535			}
2536		}
2537		pt = listType(stringType())
2538	case []byte:
2539		if v != nil {
2540			pb.Kind = stringKind(base64.StdEncoding.EncodeToString(v))
2541		}
2542		pt = bytesType()
2543	case [][]byte:
2544		if v != nil {
2545			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2546			if err != nil {
2547				return nil, nil, err
2548			}
2549		}
2550		pt = listType(bytesType())
2551	case int:
2552		pb.Kind = stringKind(strconv.FormatInt(int64(v), 10))
2553		pt = intType()
2554	case []int:
2555		if v != nil {
2556			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2557			if err != nil {
2558				return nil, nil, err
2559			}
2560		}
2561		pt = listType(intType())
2562	case int64:
2563		pb.Kind = stringKind(strconv.FormatInt(v, 10))
2564		pt = intType()
2565	case []int64:
2566		if v != nil {
2567			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2568			if err != nil {
2569				return nil, nil, err
2570			}
2571		}
2572		pt = listType(intType())
2573	case NullInt64:
2574		if v.Valid {
2575			return encodeValue(v.Int64)
2576		}
2577		pt = intType()
2578	case []NullInt64:
2579		if v != nil {
2580			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2581			if err != nil {
2582				return nil, nil, err
2583			}
2584		}
2585		pt = listType(intType())
2586	case *int64:
2587		if v != nil {
2588			return encodeValue(*v)
2589		}
2590		pt = intType()
2591	case []*int64:
2592		if v != nil {
2593			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2594			if err != nil {
2595				return nil, nil, err
2596			}
2597		}
2598		pt = listType(intType())
2599	case bool:
2600		pb.Kind = &proto3.Value_BoolValue{BoolValue: v}
2601		pt = boolType()
2602	case []bool:
2603		if v != nil {
2604			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2605			if err != nil {
2606				return nil, nil, err
2607			}
2608		}
2609		pt = listType(boolType())
2610	case NullBool:
2611		if v.Valid {
2612			return encodeValue(v.Bool)
2613		}
2614		pt = boolType()
2615	case []NullBool:
2616		if v != nil {
2617			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2618			if err != nil {
2619				return nil, nil, err
2620			}
2621		}
2622		pt = listType(boolType())
2623	case *bool:
2624		if v != nil {
2625			return encodeValue(*v)
2626		}
2627		pt = boolType()
2628	case []*bool:
2629		if v != nil {
2630			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2631			if err != nil {
2632				return nil, nil, err
2633			}
2634		}
2635		pt = listType(boolType())
2636	case float64:
2637		pb.Kind = &proto3.Value_NumberValue{NumberValue: v}
2638		pt = floatType()
2639	case []float64:
2640		if v != nil {
2641			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2642			if err != nil {
2643				return nil, nil, err
2644			}
2645		}
2646		pt = listType(floatType())
2647	case NullFloat64:
2648		if v.Valid {
2649			return encodeValue(v.Float64)
2650		}
2651		pt = floatType()
2652	case []NullFloat64:
2653		if v != nil {
2654			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2655			if err != nil {
2656				return nil, nil, err
2657			}
2658		}
2659		pt = listType(floatType())
2660	case *float64:
2661		if v != nil {
2662			return encodeValue(*v)
2663		}
2664		pt = floatType()
2665	case []*float64:
2666		if v != nil {
2667			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2668			if err != nil {
2669				return nil, nil, err
2670			}
2671		}
2672		pt = listType(floatType())
2673	case big.Rat:
2674		pb.Kind = stringKind(NumericString(&v))
2675		pt = numericType()
2676	case []big.Rat:
2677		if v != nil {
2678			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2679			if err != nil {
2680				return nil, nil, err
2681			}
2682		}
2683		pt = listType(numericType())
2684	case NullNumeric:
2685		if v.Valid {
2686			return encodeValue(v.Numeric)
2687		}
2688		pt = numericType()
2689	case []NullNumeric:
2690		if v != nil {
2691			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2692			if err != nil {
2693				return nil, nil, err
2694			}
2695		}
2696		pt = listType(numericType())
2697	case *big.Rat:
2698		if v != nil {
2699			pb.Kind = stringKind(NumericString(v))
2700		}
2701		pt = numericType()
2702	case []*big.Rat:
2703		if v != nil {
2704			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2705			if err != nil {
2706				return nil, nil, err
2707			}
2708		}
2709		pt = listType(numericType())
2710	case time.Time:
2711		if v == commitTimestamp {
2712			pb.Kind = stringKind(commitTimestampPlaceholderString)
2713		} else {
2714			pb.Kind = stringKind(v.UTC().Format(time.RFC3339Nano))
2715		}
2716		pt = timeType()
2717	case []time.Time:
2718		if v != nil {
2719			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2720			if err != nil {
2721				return nil, nil, err
2722			}
2723		}
2724		pt = listType(timeType())
2725	case NullTime:
2726		if v.Valid {
2727			return encodeValue(v.Time)
2728		}
2729		pt = timeType()
2730	case []NullTime:
2731		if v != nil {
2732			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2733			if err != nil {
2734				return nil, nil, err
2735			}
2736		}
2737		pt = listType(timeType())
2738	case *time.Time:
2739		if v != nil {
2740			return encodeValue(*v)
2741		}
2742		pt = timeType()
2743	case []*time.Time:
2744		if v != nil {
2745			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2746			if err != nil {
2747				return nil, nil, err
2748			}
2749		}
2750		pt = listType(timeType())
2751	case civil.Date:
2752		pb.Kind = stringKind(v.String())
2753		pt = dateType()
2754	case []civil.Date:
2755		if v != nil {
2756			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2757			if err != nil {
2758				return nil, nil, err
2759			}
2760		}
2761		pt = listType(dateType())
2762	case NullDate:
2763		if v.Valid {
2764			return encodeValue(v.Date)
2765		}
2766		pt = dateType()
2767	case []NullDate:
2768		if v != nil {
2769			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2770			if err != nil {
2771				return nil, nil, err
2772			}
2773		}
2774		pt = listType(dateType())
2775	case *civil.Date:
2776		if v != nil {
2777			return encodeValue(*v)
2778		}
2779		pt = dateType()
2780	case []*civil.Date:
2781		if v != nil {
2782			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2783			if err != nil {
2784				return nil, nil, err
2785			}
2786		}
2787		pt = listType(dateType())
2788	case GenericColumnValue:
2789		// Deep clone to ensure subsequent changes to v before
2790		// transmission don't affect our encoded value.
2791		pb = proto.Clone(v.Value).(*proto3.Value)
2792		pt = proto.Clone(v.Type).(*sppb.Type)
2793	case []GenericColumnValue:
2794		return nil, nil, errEncoderUnsupportedType(v)
2795	default:
2796		// Check if the value is a custom type that implements spanner.Encoder
2797		// interface.
2798		if encodedVal, ok := v.(Encoder); ok {
2799			nv, err := encodedVal.EncodeSpanner()
2800			if err != nil {
2801				return nil, nil, err
2802			}
2803			return encodeValue(nv)
2804		}
2805
2806		// Check if the value is a variant of a base type.
2807		decodableType := getDecodableSpannerType(v, false)
2808		if decodableType != spannerTypeUnknown && decodableType != spannerTypeInvalid {
2809			converted, err := convertCustomTypeValue(decodableType, v)
2810			if err != nil {
2811				return nil, nil, err
2812			}
2813			return encodeValue(converted)
2814		}
2815
2816		if !isStructOrArrayOfStructValue(v) {
2817			return nil, nil, errEncoderUnsupportedType(v)
2818		}
2819		typ := reflect.TypeOf(v)
2820
2821		// Value is a Go struct value/ptr.
2822		if (typ.Kind() == reflect.Struct) ||
2823			(typ.Kind() == reflect.Ptr && typ.Elem().Kind() == reflect.Struct) {
2824			return encodeStruct(v)
2825		}
2826
2827		// Value is a slice of Go struct values/ptrs.
2828		if typ.Kind() == reflect.Slice {
2829			return encodeStructArray(v)
2830		}
2831	}
2832	return pb, pt, nil
2833}
2834
2835func convertCustomTypeValue(sourceType decodableSpannerType, v interface{}) (interface{}, error) {
2836	// destination will be initialized to a base type. The input value will be
2837	// converted to this type and copied to destination.
2838	var destination reflect.Value
2839	switch sourceType {
2840	case spannerTypeInvalid:
2841		return nil, fmt.Errorf("cannot encode a value to type spannerTypeInvalid")
2842	case spannerTypeNonNullString:
2843		destination = reflect.Indirect(reflect.New(reflect.TypeOf("")))
2844	case spannerTypeNullString:
2845		destination = reflect.Indirect(reflect.New(reflect.TypeOf(NullString{})))
2846	case spannerTypeByteArray:
2847		// Return a nil array directly if the input value is nil instead of
2848		// creating an empty slice and returning that.
2849		if reflect.ValueOf(v).IsNil() {
2850			return []byte(nil), nil
2851		}
2852		destination = reflect.MakeSlice(reflect.TypeOf([]byte{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2853	case spannerTypeNonNullInt64:
2854		destination = reflect.Indirect(reflect.New(reflect.TypeOf(int64(0))))
2855	case spannerTypeNullInt64:
2856		destination = reflect.Indirect(reflect.New(reflect.TypeOf(NullInt64{})))
2857	case spannerTypeNonNullBool:
2858		destination = reflect.Indirect(reflect.New(reflect.TypeOf(false)))
2859	case spannerTypeNullBool:
2860		destination = reflect.Indirect(reflect.New(reflect.TypeOf(NullBool{})))
2861	case spannerTypeNonNullFloat64:
2862		destination = reflect.Indirect(reflect.New(reflect.TypeOf(float64(0.0))))
2863	case spannerTypeNullFloat64:
2864		destination = reflect.Indirect(reflect.New(reflect.TypeOf(NullFloat64{})))
2865	case spannerTypeNonNullTime:
2866		destination = reflect.Indirect(reflect.New(reflect.TypeOf(time.Time{})))
2867	case spannerTypeNullTime:
2868		destination = reflect.Indirect(reflect.New(reflect.TypeOf(NullTime{})))
2869	case spannerTypeNonNullDate:
2870		destination = reflect.Indirect(reflect.New(reflect.TypeOf(civil.Date{})))
2871	case spannerTypeNullDate:
2872		destination = reflect.Indirect(reflect.New(reflect.TypeOf(NullDate{})))
2873	case spannerTypeNonNullNumeric:
2874		destination = reflect.Indirect(reflect.New(reflect.TypeOf(big.Rat{})))
2875	case spannerTypeNullNumeric:
2876		destination = reflect.Indirect(reflect.New(reflect.TypeOf(NullNumeric{})))
2877	case spannerTypeArrayOfNonNullString:
2878		if reflect.ValueOf(v).IsNil() {
2879			return []string(nil), nil
2880		}
2881		destination = reflect.MakeSlice(reflect.TypeOf([]string{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2882	case spannerTypeArrayOfNullString:
2883		if reflect.ValueOf(v).IsNil() {
2884			return []NullString(nil), nil
2885		}
2886		destination = reflect.MakeSlice(reflect.TypeOf([]NullString{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2887	case spannerTypeArrayOfByteArray:
2888		if reflect.ValueOf(v).IsNil() {
2889			return [][]byte(nil), nil
2890		}
2891		destination = reflect.MakeSlice(reflect.TypeOf([][]byte{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2892	case spannerTypeArrayOfNonNullInt64:
2893		if reflect.ValueOf(v).IsNil() {
2894			return []int64(nil), nil
2895		}
2896		destination = reflect.MakeSlice(reflect.TypeOf([]int64{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2897	case spannerTypeArrayOfNullInt64:
2898		if reflect.ValueOf(v).IsNil() {
2899			return []NullInt64(nil), nil
2900		}
2901		destination = reflect.MakeSlice(reflect.TypeOf([]NullInt64{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2902	case spannerTypeArrayOfNonNullBool:
2903		if reflect.ValueOf(v).IsNil() {
2904			return []bool(nil), nil
2905		}
2906		destination = reflect.MakeSlice(reflect.TypeOf([]bool{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2907	case spannerTypeArrayOfNullBool:
2908		if reflect.ValueOf(v).IsNil() {
2909			return []NullBool(nil), nil
2910		}
2911		destination = reflect.MakeSlice(reflect.TypeOf([]NullBool{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2912	case spannerTypeArrayOfNonNullFloat64:
2913		if reflect.ValueOf(v).IsNil() {
2914			return []float64(nil), nil
2915		}
2916		destination = reflect.MakeSlice(reflect.TypeOf([]float64{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2917	case spannerTypeArrayOfNullFloat64:
2918		if reflect.ValueOf(v).IsNil() {
2919			return []NullFloat64(nil), nil
2920		}
2921		destination = reflect.MakeSlice(reflect.TypeOf([]NullFloat64{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2922	case spannerTypeArrayOfNonNullTime:
2923		if reflect.ValueOf(v).IsNil() {
2924			return []time.Time(nil), nil
2925		}
2926		destination = reflect.MakeSlice(reflect.TypeOf([]time.Time{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2927	case spannerTypeArrayOfNullTime:
2928		if reflect.ValueOf(v).IsNil() {
2929			return []NullTime(nil), nil
2930		}
2931		destination = reflect.MakeSlice(reflect.TypeOf([]NullTime{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2932	case spannerTypeArrayOfNonNullDate:
2933		if reflect.ValueOf(v).IsNil() {
2934			return []civil.Date(nil), nil
2935		}
2936		destination = reflect.MakeSlice(reflect.TypeOf([]civil.Date{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2937	case spannerTypeArrayOfNullDate:
2938		if reflect.ValueOf(v).IsNil() {
2939			return []NullDate(nil), nil
2940		}
2941		destination = reflect.MakeSlice(reflect.TypeOf([]NullDate{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2942	case spannerTypeArrayOfNonNullNumeric:
2943		if reflect.ValueOf(v).IsNil() {
2944			return []big.Rat(nil), nil
2945		}
2946		destination = reflect.MakeSlice(reflect.TypeOf([]big.Rat{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2947	case spannerTypeArrayOfNullNumeric:
2948		if reflect.ValueOf(v).IsNil() {
2949			return []NullNumeric(nil), nil
2950		}
2951		destination = reflect.MakeSlice(reflect.TypeOf([]NullNumeric{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2952	default:
2953		// This should not be possible.
2954		return nil, fmt.Errorf("unknown decodable type found: %v", sourceType)
2955	}
2956	// destination has been initialized. Convert and copy the input value to
2957	// destination. That must be done per element if the input type is a slice
2958	// or an array.
2959	if destination.Kind() == reflect.Slice || destination.Kind() == reflect.Array {
2960		sourceSlice := reflect.ValueOf(v)
2961		for i := 0; i < destination.Len(); i++ {
2962			source := sourceSlice.Index(i)
2963			destination.Index(i).Set(source.Convert(destination.Type().Elem()))
2964		}
2965	} else {
2966		source := reflect.ValueOf(v)
2967		destination.Set(source.Convert(destination.Type()))
2968	}
2969	// Return the converted value.
2970	return destination.Interface(), nil
2971}
2972
2973// Encodes a Go struct value/ptr in v to the spanner Value and Type protos. v
2974// itself must be non-nil.
2975func encodeStruct(v interface{}) (*proto3.Value, *sppb.Type, error) {
2976	typ := reflect.TypeOf(v)
2977	val := reflect.ValueOf(v)
2978
2979	// Pointer to struct.
2980	if typ.Kind() == reflect.Ptr && typ.Elem().Kind() == reflect.Struct {
2981		typ = typ.Elem()
2982		if val.IsNil() {
2983			// nil pointer to struct, representing a NULL STRUCT value. Use a
2984			// dummy value to get the type.
2985			_, st, err := encodeStruct(reflect.Zero(typ).Interface())
2986			if err != nil {
2987				return nil, nil, err
2988			}
2989			return nullProto(), st, nil
2990		}
2991		val = val.Elem()
2992	}
2993
2994	if typ.Kind() != reflect.Struct {
2995		return nil, nil, errEncoderUnsupportedType(v)
2996	}
2997
2998	stf := make([]*sppb.StructType_Field, 0, typ.NumField())
2999	stv := make([]*proto3.Value, 0, typ.NumField())
3000
3001	for i := 0; i < typ.NumField(); i++ {
3002		// If the field has a 'spanner' tag, use the value of that tag as the field name.
3003		// This is used to build STRUCT types with unnamed/duplicate fields.
3004		sf := typ.Field(i)
3005		fval := val.Field(i)
3006
3007		// Embedded fields are not allowed.
3008		if sf.Anonymous {
3009			return nil, nil, errUnsupportedEmbeddedStructFields(sf.Name)
3010		}
3011
3012		// Unexported fields are ignored.
3013		if !fval.CanInterface() {
3014			continue
3015		}
3016
3017		fname, ok := sf.Tag.Lookup("spanner")
3018		if !ok {
3019			fname = sf.Name
3020		}
3021
3022		eval, etype, err := encodeValue(fval.Interface())
3023		if err != nil {
3024			return nil, nil, err
3025		}
3026
3027		stf = append(stf, mkField(fname, etype))
3028		stv = append(stv, eval)
3029	}
3030
3031	return listProto(stv...), structType(stf...), nil
3032}
3033
3034// Encodes a slice of Go struct values/ptrs in v to the spanner Value and Type
3035// protos. v itself must be non-nil.
3036func encodeStructArray(v interface{}) (*proto3.Value, *sppb.Type, error) {
3037	etyp := reflect.TypeOf(v).Elem()
3038	sliceval := reflect.ValueOf(v)
3039
3040	// Slice of pointers to structs.
3041	if etyp.Kind() == reflect.Ptr {
3042		etyp = etyp.Elem()
3043	}
3044
3045	// Use a dummy struct value to get the element type.
3046	_, elemTyp, err := encodeStruct(reflect.Zero(etyp).Interface())
3047	if err != nil {
3048		return nil, nil, err
3049	}
3050
3051	// nil slice represents a NULL array-of-struct.
3052	if sliceval.IsNil() {
3053		return nullProto(), listType(elemTyp), nil
3054	}
3055
3056	values := make([]*proto3.Value, 0, sliceval.Len())
3057
3058	for i := 0; i < sliceval.Len(); i++ {
3059		ev, _, err := encodeStruct(sliceval.Index(i).Interface())
3060		if err != nil {
3061			return nil, nil, err
3062		}
3063		values = append(values, ev)
3064	}
3065	return listProto(values...), listType(elemTyp), nil
3066}
3067
3068func isStructOrArrayOfStructValue(v interface{}) bool {
3069	typ := reflect.TypeOf(v)
3070	if typ.Kind() == reflect.Slice {
3071		typ = typ.Elem()
3072	}
3073	if typ.Kind() == reflect.Ptr {
3074		typ = typ.Elem()
3075	}
3076	return typ.Kind() == reflect.Struct
3077}
3078
3079func isSupportedMutationType(v interface{}) bool {
3080	switch v.(type) {
3081	case nil, string, *string, NullString, []string, []*string, []NullString,
3082		[]byte, [][]byte,
3083		int, []int, int64, *int64, []int64, []*int64, NullInt64, []NullInt64,
3084		bool, *bool, []bool, []*bool, NullBool, []NullBool,
3085		float64, *float64, []float64, []*float64, NullFloat64, []NullFloat64,
3086		time.Time, *time.Time, []time.Time, []*time.Time, NullTime, []NullTime,
3087		civil.Date, *civil.Date, []civil.Date, []*civil.Date, NullDate, []NullDate,
3088		big.Rat, *big.Rat, []big.Rat, []*big.Rat, NullNumeric, []NullNumeric,
3089		GenericColumnValue:
3090		return true
3091	default:
3092		// Check if the custom type implements spanner.Encoder interface.
3093		if _, ok := v.(Encoder); ok {
3094			return true
3095		}
3096
3097		decodableType := getDecodableSpannerType(v, false)
3098		return decodableType != spannerTypeUnknown && decodableType != spannerTypeInvalid
3099	}
3100}
3101
3102// encodeValueArray encodes a Value array into a proto3.ListValue.
3103func encodeValueArray(vs []interface{}) (*proto3.ListValue, error) {
3104	lv := &proto3.ListValue{}
3105	lv.Values = make([]*proto3.Value, 0, len(vs))
3106	for _, v := range vs {
3107		if !isSupportedMutationType(v) {
3108			return nil, errEncoderUnsupportedType(v)
3109		}
3110		pb, _, err := encodeValue(v)
3111		if err != nil {
3112			return nil, err
3113		}
3114		lv.Values = append(lv.Values, pb)
3115	}
3116	return lv, nil
3117}
3118
3119// encodeArray assumes that all values of the array element type encode without
3120// error.
3121func encodeArray(len int, at func(int) interface{}) (*proto3.Value, error) {
3122	vs := make([]*proto3.Value, len)
3123	var err error
3124	for i := 0; i < len; i++ {
3125		vs[i], _, err = encodeValue(at(i))
3126		if err != nil {
3127			return nil, err
3128		}
3129	}
3130	return listProto(vs...), nil
3131}
3132
3133func spannerTagParser(t reflect.StructTag) (name string, keep bool, other interface{}, err error) {
3134	if s := t.Get("spanner"); s != "" {
3135		if s == "-" {
3136			return "", false, nil, nil
3137		}
3138		return s, true, nil, nil
3139	}
3140	return "", true, nil, nil
3141}
3142
3143var fieldCache = fields.NewCache(spannerTagParser, nil, nil)
3144
3145func trimDoubleQuotes(payload []byte) ([]byte, error) {
3146	if len(payload) <= 1 || payload[0] != '"' || payload[len(payload)-1] != '"' {
3147		return nil, fmt.Errorf("payload is too short or not wrapped with double quotes: got %q", string(payload))
3148	}
3149	// Remove the double quotes at the beginning and the end.
3150	return payload[1 : len(payload)-1], nil
3151}
3152