You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1609 lines
41 KiB

  1. /*
  2. Copyright 2017 Google LLC
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package spanner
  14. import (
  15. "encoding/base64"
  16. "fmt"
  17. "math"
  18. "reflect"
  19. "strconv"
  20. "time"
  21. "cloud.google.com/go/civil"
  22. "cloud.google.com/go/internal/fields"
  23. proto "github.com/golang/protobuf/proto"
  24. proto3 "github.com/golang/protobuf/ptypes/struct"
  25. sppb "google.golang.org/genproto/googleapis/spanner/v1"
  26. "google.golang.org/grpc/codes"
  27. )
  28. const commitTimestampPlaceholderString = "spanner.commit_timestamp()"
  29. var (
  30. // CommitTimestamp is a special value used to tell Cloud Spanner
  31. // to insert the commit timestamp of the transaction into a column.
  32. // It can be used in a Mutation, or directly used in
  33. // InsertStruct or InsertMap. See ExampleCommitTimestamp.
  34. // This is just a placeholder and the actual value stored in this
  35. // variable has no meaning.
  36. CommitTimestamp = commitTimestamp
  37. commitTimestamp = time.Unix(0, 0).In(time.FixedZone("CommitTimestamp placeholder", 0xDB))
  38. )
  39. // NullInt64 represents a Cloud Spanner INT64 that may be NULL.
  40. type NullInt64 struct {
  41. Int64 int64
  42. Valid bool // Valid is true if Int64 is not NULL.
  43. }
  44. // String implements Stringer.String for NullInt64
  45. func (n NullInt64) String() string {
  46. if !n.Valid {
  47. return fmt.Sprintf("%v", "<null>")
  48. }
  49. return fmt.Sprintf("%v", n.Int64)
  50. }
  51. // NullString represents a Cloud Spanner STRING that may be NULL.
  52. type NullString struct {
  53. StringVal string
  54. Valid bool // Valid is true if StringVal is not NULL.
  55. }
  56. // String implements Stringer.String for NullString
  57. func (n NullString) String() string {
  58. if !n.Valid {
  59. return fmt.Sprintf("%v", "<null>")
  60. }
  61. return fmt.Sprintf("%q", n.StringVal)
  62. }
  63. // NullFloat64 represents a Cloud Spanner FLOAT64 that may be NULL.
  64. type NullFloat64 struct {
  65. Float64 float64
  66. Valid bool // Valid is true if Float64 is not NULL.
  67. }
  68. // Cloud Spanner STRUCT (aka STRUCT) values (https://cloud.google.com/spanner/docs/data-types#struct-type)
  69. // can be represented by a Go struct value.
  70. // The spanner.StructType of such values is built from the field types and field tag information
  71. // of the Go struct. If a field in the struct type definition has a "spanner:<field_name>" tag,
  72. // then the value of the "spanner" key in the tag is used as the name for that field in the
  73. // built spanner.StructType, otherwise the field name in the struct definition is used. To specify a
  74. // field with an empty field name in a Cloud Spanner STRUCT type, use the `spanner:""` tag
  75. // annotation against the corresponding field in the Go struct's type definition.
  76. //
  77. // A STRUCT value can contain STRUCT-typed and Array-of-STRUCT typed fields and these can be
  78. // specified using named struct-typed and []struct-typed fields inside a Go struct. However,
  79. // embedded struct fields are not allowed. Unexported struct fields are ignored.
  80. //
  81. // NULL STRUCT values in Cloud Spanner are typed. A nil pointer to a Go struct value can be used to
  82. // specify a NULL STRUCT value of the corresponding spanner.StructType. Nil and empty slices of a
  83. // Go STRUCT type can be used to specify NULL and empty array values respectively of the
  84. // corresponding spanner.StructType. A slice of pointers to a Go struct type can be used to specify
  85. // an array of NULL-able STRUCT values.
  86. // String implements Stringer.String for NullFloat64
  87. func (n NullFloat64) String() string {
  88. if !n.Valid {
  89. return fmt.Sprintf("%v", "<null>")
  90. }
  91. return fmt.Sprintf("%v", n.Float64)
  92. }
  93. // NullBool represents a Cloud Spanner BOOL that may be NULL.
  94. type NullBool struct {
  95. Bool bool
  96. Valid bool // Valid is true if Bool is not NULL.
  97. }
  98. // String implements Stringer.String for NullBool
  99. func (n NullBool) String() string {
  100. if !n.Valid {
  101. return fmt.Sprintf("%v", "<null>")
  102. }
  103. return fmt.Sprintf("%v", n.Bool)
  104. }
  105. // NullTime represents a Cloud Spanner TIMESTAMP that may be null.
  106. type NullTime struct {
  107. Time time.Time
  108. Valid bool // Valid is true if Time is not NULL.
  109. }
  110. // String implements Stringer.String for NullTime
  111. func (n NullTime) String() string {
  112. if !n.Valid {
  113. return "<null>"
  114. }
  115. return fmt.Sprintf("%q", n.Time.Format(time.RFC3339Nano))
  116. }
  117. // NullDate represents a Cloud Spanner DATE that may be null.
  118. type NullDate struct {
  119. Date civil.Date
  120. Valid bool // Valid is true if Date is not NULL.
  121. }
  122. // String implements Stringer.String for NullDate
  123. func (n NullDate) String() string {
  124. if !n.Valid {
  125. return "<null>"
  126. }
  127. return fmt.Sprintf("%q", n.Date)
  128. }
  129. // NullRow represents a Cloud Spanner STRUCT that may be NULL.
  130. // See also the document for Row.
  131. // Note that NullRow is not a valid Cloud Spanner column Type.
  132. type NullRow struct {
  133. Row Row
  134. Valid bool // Valid is true if Row is not NULL.
  135. }
  136. // GenericColumnValue represents the generic encoded value and type of the
  137. // column. See google.spanner.v1.ResultSet proto for details. This can be
  138. // useful for proxying query results when the result types are not known in
  139. // advance.
  140. //
  141. // If you populate a GenericColumnValue from a row using Row.Column or related
  142. // methods, do not modify the contents of Type and Value.
  143. type GenericColumnValue struct {
  144. Type *sppb.Type
  145. Value *proto3.Value
  146. }
  147. // Decode decodes a GenericColumnValue. The ptr argument should be a pointer
  148. // to a Go value that can accept v.
  149. func (v GenericColumnValue) Decode(ptr interface{}) error {
  150. return decodeValue(v.Value, v.Type, ptr)
  151. }
  152. // NewGenericColumnValue creates a GenericColumnValue from Go value that is
  153. // valid for Cloud Spanner.
  154. func newGenericColumnValue(v interface{}) (*GenericColumnValue, error) {
  155. value, typ, err := encodeValue(v)
  156. if err != nil {
  157. return nil, err
  158. }
  159. return &GenericColumnValue{Value: value, Type: typ}, nil
  160. }
  161. // errTypeMismatch returns error for destination not having a compatible type
  162. // with source Cloud Spanner type.
  163. func errTypeMismatch(srcCode, elCode sppb.TypeCode, dst interface{}) error {
  164. s := srcCode.String()
  165. if srcCode == sppb.TypeCode_ARRAY {
  166. s = fmt.Sprintf("%v[%v]", srcCode, elCode)
  167. }
  168. return spannerErrorf(codes.InvalidArgument, "type %T cannot be used for decoding %s", dst, s)
  169. }
  170. // errNilSpannerType returns error for nil Cloud Spanner type in decoding.
  171. func errNilSpannerType() error {
  172. return spannerErrorf(codes.FailedPrecondition, "unexpected nil Cloud Spanner data type in decoding")
  173. }
  174. // errNilSrc returns error for decoding from nil proto value.
  175. func errNilSrc() error {
  176. return spannerErrorf(codes.FailedPrecondition, "unexpected nil Cloud Spanner value in decoding")
  177. }
  178. // errNilDst returns error for decoding into nil interface{}.
  179. func errNilDst(dst interface{}) error {
  180. return spannerErrorf(codes.InvalidArgument, "cannot decode into nil type %T", dst)
  181. }
  182. // errNilArrElemType returns error for input Cloud Spanner data type being a array but without a
  183. // non-nil array element type.
  184. func errNilArrElemType(t *sppb.Type) error {
  185. return spannerErrorf(codes.FailedPrecondition, "array type %v is with nil array element type", t)
  186. }
  187. func errUnsupportedEmbeddedStructFields(fname string) error {
  188. return spannerErrorf(codes.InvalidArgument, "Embedded field: %s. Embedded and anonymous fields are not allowed "+
  189. "when converting Go structs to Cloud Spanner STRUCT values. To create a STRUCT value with an "+
  190. "unnamed field, use a `spanner:\"\"` field tag.", fname)
  191. }
  192. // errDstNotForNull returns error for decoding a SQL NULL value into a destination which doesn't
  193. // support NULL values.
  194. func errDstNotForNull(dst interface{}) error {
  195. return spannerErrorf(codes.InvalidArgument, "destination %T cannot support NULL SQL values", dst)
  196. }
  197. // errBadEncoding returns error for decoding wrongly encoded types.
  198. func errBadEncoding(v *proto3.Value, err error) error {
  199. return spannerErrorf(codes.FailedPrecondition, "%v wasn't correctly encoded: <%v>", v, err)
  200. }
  201. func parseNullTime(v *proto3.Value, p *NullTime, code sppb.TypeCode, isNull bool) error {
  202. if p == nil {
  203. return errNilDst(p)
  204. }
  205. if code != sppb.TypeCode_TIMESTAMP {
  206. return errTypeMismatch(code, sppb.TypeCode_TYPE_CODE_UNSPECIFIED, p)
  207. }
  208. if isNull {
  209. *p = NullTime{}
  210. return nil
  211. }
  212. x, err := getStringValue(v)
  213. if err != nil {
  214. return err
  215. }
  216. y, err := time.Parse(time.RFC3339Nano, x)
  217. if err != nil {
  218. return errBadEncoding(v, err)
  219. }
  220. p.Valid = true
  221. p.Time = y
  222. return nil
  223. }
  224. // decodeValue decodes a protobuf Value into a pointer to a Go value, as
  225. // specified by sppb.Type.
  226. func decodeValue(v *proto3.Value, t *sppb.Type, ptr interface{}) error {
  227. if v == nil {
  228. return errNilSrc()
  229. }
  230. if t == nil {
  231. return errNilSpannerType()
  232. }
  233. code := t.Code
  234. acode := sppb.TypeCode_TYPE_CODE_UNSPECIFIED
  235. if code == sppb.TypeCode_ARRAY {
  236. if t.ArrayElementType == nil {
  237. return errNilArrElemType(t)
  238. }
  239. acode = t.ArrayElementType.Code
  240. }
  241. _, isNull := v.Kind.(*proto3.Value_NullValue)
  242. // Do the decoding based on the type of ptr.
  243. switch p := ptr.(type) {
  244. case nil:
  245. return errNilDst(nil)
  246. case *string:
  247. if p == nil {
  248. return errNilDst(p)
  249. }
  250. if code != sppb.TypeCode_STRING {
  251. return errTypeMismatch(code, acode, ptr)
  252. }
  253. if isNull {
  254. return errDstNotForNull(ptr)
  255. }
  256. x, err := getStringValue(v)
  257. if err != nil {
  258. return err
  259. }
  260. *p = x
  261. case *NullString:
  262. if p == nil {
  263. return errNilDst(p)
  264. }
  265. if code != sppb.TypeCode_STRING {
  266. return errTypeMismatch(code, acode, ptr)
  267. }
  268. if isNull {
  269. *p = NullString{}
  270. break
  271. }
  272. x, err := getStringValue(v)
  273. if err != nil {
  274. return err
  275. }
  276. p.Valid = true
  277. p.StringVal = x
  278. case *[]NullString:
  279. if p == nil {
  280. return errNilDst(p)
  281. }
  282. if acode != sppb.TypeCode_STRING {
  283. return errTypeMismatch(code, acode, ptr)
  284. }
  285. if isNull {
  286. *p = nil
  287. break
  288. }
  289. x, err := getListValue(v)
  290. if err != nil {
  291. return err
  292. }
  293. y, err := decodeNullStringArray(x)
  294. if err != nil {
  295. return err
  296. }
  297. *p = y
  298. case *[]string:
  299. if p == nil {
  300. return errNilDst(p)
  301. }
  302. if acode != sppb.TypeCode_STRING {
  303. return errTypeMismatch(code, acode, ptr)
  304. }
  305. if isNull {
  306. *p = nil
  307. break
  308. }
  309. x, err := getListValue(v)
  310. if err != nil {
  311. return err
  312. }
  313. y, err := decodeStringArray(x)
  314. if err != nil {
  315. return err
  316. }
  317. *p = y
  318. case *[]byte:
  319. if p == nil {
  320. return errNilDst(p)
  321. }
  322. if code != sppb.TypeCode_BYTES {
  323. return errTypeMismatch(code, acode, ptr)
  324. }
  325. if isNull {
  326. *p = nil
  327. break
  328. }
  329. x, err := getStringValue(v)
  330. if err != nil {
  331. return err
  332. }
  333. y, err := base64.StdEncoding.DecodeString(x)
  334. if err != nil {
  335. return errBadEncoding(v, err)
  336. }
  337. *p = y
  338. case *[][]byte:
  339. if p == nil {
  340. return errNilDst(p)
  341. }
  342. if acode != sppb.TypeCode_BYTES {
  343. return errTypeMismatch(code, acode, ptr)
  344. }
  345. if isNull {
  346. *p = nil
  347. break
  348. }
  349. x, err := getListValue(v)
  350. if err != nil {
  351. return err
  352. }
  353. y, err := decodeByteArray(x)
  354. if err != nil {
  355. return err
  356. }
  357. *p = y
  358. case *int64:
  359. if p == nil {
  360. return errNilDst(p)
  361. }
  362. if code != sppb.TypeCode_INT64 {
  363. return errTypeMismatch(code, acode, ptr)
  364. }
  365. if isNull {
  366. return errDstNotForNull(ptr)
  367. }
  368. x, err := getStringValue(v)
  369. if err != nil {
  370. return err
  371. }
  372. y, err := strconv.ParseInt(x, 10, 64)
  373. if err != nil {
  374. return errBadEncoding(v, err)
  375. }
  376. *p = y
  377. case *NullInt64:
  378. if p == nil {
  379. return errNilDst(p)
  380. }
  381. if code != sppb.TypeCode_INT64 {
  382. return errTypeMismatch(code, acode, ptr)
  383. }
  384. if isNull {
  385. *p = NullInt64{}
  386. break
  387. }
  388. x, err := getStringValue(v)
  389. if err != nil {
  390. return err
  391. }
  392. y, err := strconv.ParseInt(x, 10, 64)
  393. if err != nil {
  394. return errBadEncoding(v, err)
  395. }
  396. p.Valid = true
  397. p.Int64 = y
  398. case *[]NullInt64:
  399. if p == nil {
  400. return errNilDst(p)
  401. }
  402. if acode != sppb.TypeCode_INT64 {
  403. return errTypeMismatch(code, acode, ptr)
  404. }
  405. if isNull {
  406. *p = nil
  407. break
  408. }
  409. x, err := getListValue(v)
  410. if err != nil {
  411. return err
  412. }
  413. y, err := decodeNullInt64Array(x)
  414. if err != nil {
  415. return err
  416. }
  417. *p = y
  418. case *[]int64:
  419. if p == nil {
  420. return errNilDst(p)
  421. }
  422. if acode != sppb.TypeCode_INT64 {
  423. return errTypeMismatch(code, acode, ptr)
  424. }
  425. if isNull {
  426. *p = nil
  427. break
  428. }
  429. x, err := getListValue(v)
  430. if err != nil {
  431. return err
  432. }
  433. y, err := decodeInt64Array(x)
  434. if err != nil {
  435. return err
  436. }
  437. *p = y
  438. case *bool:
  439. if p == nil {
  440. return errNilDst(p)
  441. }
  442. if code != sppb.TypeCode_BOOL {
  443. return errTypeMismatch(code, acode, ptr)
  444. }
  445. if isNull {
  446. return errDstNotForNull(ptr)
  447. }
  448. x, err := getBoolValue(v)
  449. if err != nil {
  450. return err
  451. }
  452. *p = x
  453. case *NullBool:
  454. if p == nil {
  455. return errNilDst(p)
  456. }
  457. if code != sppb.TypeCode_BOOL {
  458. return errTypeMismatch(code, acode, ptr)
  459. }
  460. if isNull {
  461. *p = NullBool{}
  462. break
  463. }
  464. x, err := getBoolValue(v)
  465. if err != nil {
  466. return err
  467. }
  468. p.Valid = true
  469. p.Bool = x
  470. case *[]NullBool:
  471. if p == nil {
  472. return errNilDst(p)
  473. }
  474. if acode != sppb.TypeCode_BOOL {
  475. return errTypeMismatch(code, acode, ptr)
  476. }
  477. if isNull {
  478. *p = nil
  479. break
  480. }
  481. x, err := getListValue(v)
  482. if err != nil {
  483. return err
  484. }
  485. y, err := decodeNullBoolArray(x)
  486. if err != nil {
  487. return err
  488. }
  489. *p = y
  490. case *[]bool:
  491. if p == nil {
  492. return errNilDst(p)
  493. }
  494. if acode != sppb.TypeCode_BOOL {
  495. return errTypeMismatch(code, acode, ptr)
  496. }
  497. if isNull {
  498. *p = nil
  499. break
  500. }
  501. x, err := getListValue(v)
  502. if err != nil {
  503. return err
  504. }
  505. y, err := decodeBoolArray(x)
  506. if err != nil {
  507. return err
  508. }
  509. *p = y
  510. case *float64:
  511. if p == nil {
  512. return errNilDst(p)
  513. }
  514. if code != sppb.TypeCode_FLOAT64 {
  515. return errTypeMismatch(code, acode, ptr)
  516. }
  517. if isNull {
  518. return errDstNotForNull(ptr)
  519. }
  520. x, err := getFloat64Value(v)
  521. if err != nil {
  522. return err
  523. }
  524. *p = x
  525. case *NullFloat64:
  526. if p == nil {
  527. return errNilDst(p)
  528. }
  529. if code != sppb.TypeCode_FLOAT64 {
  530. return errTypeMismatch(code, acode, ptr)
  531. }
  532. if isNull {
  533. *p = NullFloat64{}
  534. break
  535. }
  536. x, err := getFloat64Value(v)
  537. if err != nil {
  538. return err
  539. }
  540. p.Valid = true
  541. p.Float64 = x
  542. case *[]NullFloat64:
  543. if p == nil {
  544. return errNilDst(p)
  545. }
  546. if acode != sppb.TypeCode_FLOAT64 {
  547. return errTypeMismatch(code, acode, ptr)
  548. }
  549. if isNull {
  550. *p = nil
  551. break
  552. }
  553. x, err := getListValue(v)
  554. if err != nil {
  555. return err
  556. }
  557. y, err := decodeNullFloat64Array(x)
  558. if err != nil {
  559. return err
  560. }
  561. *p = y
  562. case *[]float64:
  563. if p == nil {
  564. return errNilDst(p)
  565. }
  566. if acode != sppb.TypeCode_FLOAT64 {
  567. return errTypeMismatch(code, acode, ptr)
  568. }
  569. if isNull {
  570. *p = nil
  571. break
  572. }
  573. x, err := getListValue(v)
  574. if err != nil {
  575. return err
  576. }
  577. y, err := decodeFloat64Array(x)
  578. if err != nil {
  579. return err
  580. }
  581. *p = y
  582. case *time.Time:
  583. var nt NullTime
  584. if isNull {
  585. return errDstNotForNull(ptr)
  586. }
  587. err := parseNullTime(v, &nt, code, isNull)
  588. if err != nil {
  589. return nil
  590. }
  591. *p = nt.Time
  592. case *NullTime:
  593. err := parseNullTime(v, p, code, isNull)
  594. if err != nil {
  595. return err
  596. }
  597. case *[]NullTime:
  598. if p == nil {
  599. return errNilDst(p)
  600. }
  601. if acode != sppb.TypeCode_TIMESTAMP {
  602. return errTypeMismatch(code, acode, ptr)
  603. }
  604. if isNull {
  605. *p = nil
  606. break
  607. }
  608. x, err := getListValue(v)
  609. if err != nil {
  610. return err
  611. }
  612. y, err := decodeNullTimeArray(x)
  613. if err != nil {
  614. return err
  615. }
  616. *p = y
  617. case *[]time.Time:
  618. if p == nil {
  619. return errNilDst(p)
  620. }
  621. if acode != sppb.TypeCode_TIMESTAMP {
  622. return errTypeMismatch(code, acode, ptr)
  623. }
  624. if isNull {
  625. *p = nil
  626. break
  627. }
  628. x, err := getListValue(v)
  629. if err != nil {
  630. return err
  631. }
  632. y, err := decodeTimeArray(x)
  633. if err != nil {
  634. return err
  635. }
  636. *p = y
  637. case *civil.Date:
  638. if p == nil {
  639. return errNilDst(p)
  640. }
  641. if code != sppb.TypeCode_DATE {
  642. return errTypeMismatch(code, acode, ptr)
  643. }
  644. if isNull {
  645. return errDstNotForNull(ptr)
  646. }
  647. x, err := getStringValue(v)
  648. if err != nil {
  649. return err
  650. }
  651. y, err := civil.ParseDate(x)
  652. if err != nil {
  653. return errBadEncoding(v, err)
  654. }
  655. *p = y
  656. case *NullDate:
  657. if p == nil {
  658. return errNilDst(p)
  659. }
  660. if code != sppb.TypeCode_DATE {
  661. return errTypeMismatch(code, acode, ptr)
  662. }
  663. if isNull {
  664. *p = NullDate{}
  665. break
  666. }
  667. x, err := getStringValue(v)
  668. if err != nil {
  669. return err
  670. }
  671. y, err := civil.ParseDate(x)
  672. if err != nil {
  673. return errBadEncoding(v, err)
  674. }
  675. p.Valid = true
  676. p.Date = y
  677. case *[]NullDate:
  678. if p == nil {
  679. return errNilDst(p)
  680. }
  681. if acode != sppb.TypeCode_DATE {
  682. return errTypeMismatch(code, acode, ptr)
  683. }
  684. if isNull {
  685. *p = nil
  686. break
  687. }
  688. x, err := getListValue(v)
  689. if err != nil {
  690. return err
  691. }
  692. y, err := decodeNullDateArray(x)
  693. if err != nil {
  694. return err
  695. }
  696. *p = y
  697. case *[]civil.Date:
  698. if p == nil {
  699. return errNilDst(p)
  700. }
  701. if acode != sppb.TypeCode_DATE {
  702. return errTypeMismatch(code, acode, ptr)
  703. }
  704. if isNull {
  705. *p = nil
  706. break
  707. }
  708. x, err := getListValue(v)
  709. if err != nil {
  710. return err
  711. }
  712. y, err := decodeDateArray(x)
  713. if err != nil {
  714. return err
  715. }
  716. *p = y
  717. case *[]NullRow:
  718. if p == nil {
  719. return errNilDst(p)
  720. }
  721. if acode != sppb.TypeCode_STRUCT {
  722. return errTypeMismatch(code, acode, ptr)
  723. }
  724. if isNull {
  725. *p = nil
  726. break
  727. }
  728. x, err := getListValue(v)
  729. if err != nil {
  730. return err
  731. }
  732. y, err := decodeRowArray(t.ArrayElementType.StructType, x)
  733. if err != nil {
  734. return err
  735. }
  736. *p = y
  737. case *GenericColumnValue:
  738. *p = GenericColumnValue{Type: t, Value: v}
  739. default:
  740. // Check if the proto encoding is for an array of structs.
  741. if !(code == sppb.TypeCode_ARRAY && acode == sppb.TypeCode_STRUCT) {
  742. return errTypeMismatch(code, acode, ptr)
  743. }
  744. vp := reflect.ValueOf(p)
  745. if !vp.IsValid() {
  746. return errNilDst(p)
  747. }
  748. if !isPtrStructPtrSlice(vp.Type()) {
  749. // The container is not a pointer to a struct pointer slice.
  750. return errTypeMismatch(code, acode, ptr)
  751. }
  752. // Only use reflection for nil detection on slow path.
  753. // Also, IsNil panics on many types, so check it after the type check.
  754. if vp.IsNil() {
  755. return errNilDst(p)
  756. }
  757. if isNull {
  758. // The proto Value is encoding NULL, set the pointer to struct
  759. // slice to nil as well.
  760. vp.Elem().Set(reflect.Zero(vp.Elem().Type()))
  761. break
  762. }
  763. x, err := getListValue(v)
  764. if err != nil {
  765. return err
  766. }
  767. if err = decodeStructArray(t.ArrayElementType.StructType, x, p); err != nil {
  768. return err
  769. }
  770. }
  771. return nil
  772. }
  773. // errSrvVal returns an error for getting a wrong source protobuf value in decoding.
  774. func errSrcVal(v *proto3.Value, want string) error {
  775. return spannerErrorf(codes.FailedPrecondition, "cannot use %v(Kind: %T) as %s Value",
  776. v, v.GetKind(), want)
  777. }
  778. // getStringValue returns the string value encoded in proto3.Value v whose
  779. // kind is proto3.Value_StringValue.
  780. func getStringValue(v *proto3.Value) (string, error) {
  781. if x, ok := v.GetKind().(*proto3.Value_StringValue); ok && x != nil {
  782. return x.StringValue, nil
  783. }
  784. return "", errSrcVal(v, "String")
  785. }
  786. // getBoolValue returns the bool value encoded in proto3.Value v whose
  787. // kind is proto3.Value_BoolValue.
  788. func getBoolValue(v *proto3.Value) (bool, error) {
  789. if x, ok := v.GetKind().(*proto3.Value_BoolValue); ok && x != nil {
  790. return x.BoolValue, nil
  791. }
  792. return false, errSrcVal(v, "Bool")
  793. }
  794. // getListValue returns the proto3.ListValue contained in proto3.Value v whose
  795. // kind is proto3.Value_ListValue.
  796. func getListValue(v *proto3.Value) (*proto3.ListValue, error) {
  797. if x, ok := v.GetKind().(*proto3.Value_ListValue); ok && x != nil {
  798. return x.ListValue, nil
  799. }
  800. return nil, errSrcVal(v, "List")
  801. }
  802. // errUnexpectedNumStr returns error for decoder getting a unexpected string for
  803. // representing special float values.
  804. func errUnexpectedNumStr(s string) error {
  805. return spannerErrorf(codes.FailedPrecondition, "unexpected string value %q for number", s)
  806. }
  807. // getFloat64Value returns the float64 value encoded in proto3.Value v whose
  808. // kind is proto3.Value_NumberValue / proto3.Value_StringValue.
  809. // Cloud Spanner uses string to encode NaN, Infinity and -Infinity.
  810. func getFloat64Value(v *proto3.Value) (float64, error) {
  811. switch x := v.GetKind().(type) {
  812. case *proto3.Value_NumberValue:
  813. if x == nil {
  814. break
  815. }
  816. return x.NumberValue, nil
  817. case *proto3.Value_StringValue:
  818. if x == nil {
  819. break
  820. }
  821. switch x.StringValue {
  822. case "NaN":
  823. return math.NaN(), nil
  824. case "Infinity":
  825. return math.Inf(1), nil
  826. case "-Infinity":
  827. return math.Inf(-1), nil
  828. default:
  829. return 0, errUnexpectedNumStr(x.StringValue)
  830. }
  831. }
  832. return 0, errSrcVal(v, "Number")
  833. }
  834. // errNilListValue returns error for unexpected nil ListValue in decoding Cloud Spanner ARRAYs.
  835. func errNilListValue(sqlType string) error {
  836. return spannerErrorf(codes.FailedPrecondition, "unexpected nil ListValue in decoding %v array", sqlType)
  837. }
  838. // errDecodeArrayElement returns error for failure in decoding single array element.
  839. func errDecodeArrayElement(i int, v proto.Message, sqlType string, err error) error {
  840. se, ok := toSpannerError(err).(*Error)
  841. if !ok {
  842. return spannerErrorf(codes.Unknown,
  843. "cannot decode %v(array element %v) as %v, error = <%v>", v, i, sqlType, err)
  844. }
  845. se.decorate(fmt.Sprintf("cannot decode %v(array element %v) as %v", v, i, sqlType))
  846. return se
  847. }
  848. // decodeNullStringArray decodes proto3.ListValue pb into a NullString slice.
  849. func decodeNullStringArray(pb *proto3.ListValue) ([]NullString, error) {
  850. if pb == nil {
  851. return nil, errNilListValue("STRING")
  852. }
  853. a := make([]NullString, len(pb.Values))
  854. for i, v := range pb.Values {
  855. if err := decodeValue(v, stringType(), &a[i]); err != nil {
  856. return nil, errDecodeArrayElement(i, v, "STRING", err)
  857. }
  858. }
  859. return a, nil
  860. }
  861. // decodeStringArray decodes proto3.ListValue pb into a string slice.
  862. func decodeStringArray(pb *proto3.ListValue) ([]string, error) {
  863. if pb == nil {
  864. return nil, errNilListValue("STRING")
  865. }
  866. a := make([]string, len(pb.Values))
  867. st := stringType()
  868. for i, v := range pb.Values {
  869. if err := decodeValue(v, st, &a[i]); err != nil {
  870. return nil, errDecodeArrayElement(i, v, "STRING", err)
  871. }
  872. }
  873. return a, nil
  874. }
  875. // decodeNullInt64Array decodes proto3.ListValue pb into a NullInt64 slice.
  876. func decodeNullInt64Array(pb *proto3.ListValue) ([]NullInt64, error) {
  877. if pb == nil {
  878. return nil, errNilListValue("INT64")
  879. }
  880. a := make([]NullInt64, len(pb.Values))
  881. for i, v := range pb.Values {
  882. if err := decodeValue(v, intType(), &a[i]); err != nil {
  883. return nil, errDecodeArrayElement(i, v, "INT64", err)
  884. }
  885. }
  886. return a, nil
  887. }
  888. // decodeInt64Array decodes proto3.ListValue pb into a int64 slice.
  889. func decodeInt64Array(pb *proto3.ListValue) ([]int64, error) {
  890. if pb == nil {
  891. return nil, errNilListValue("INT64")
  892. }
  893. a := make([]int64, len(pb.Values))
  894. for i, v := range pb.Values {
  895. if err := decodeValue(v, intType(), &a[i]); err != nil {
  896. return nil, errDecodeArrayElement(i, v, "INT64", err)
  897. }
  898. }
  899. return a, nil
  900. }
  901. // decodeNullBoolArray decodes proto3.ListValue pb into a NullBool slice.
  902. func decodeNullBoolArray(pb *proto3.ListValue) ([]NullBool, error) {
  903. if pb == nil {
  904. return nil, errNilListValue("BOOL")
  905. }
  906. a := make([]NullBool, len(pb.Values))
  907. for i, v := range pb.Values {
  908. if err := decodeValue(v, boolType(), &a[i]); err != nil {
  909. return nil, errDecodeArrayElement(i, v, "BOOL", err)
  910. }
  911. }
  912. return a, nil
  913. }
  914. // decodeBoolArray decodes proto3.ListValue pb into a bool slice.
  915. func decodeBoolArray(pb *proto3.ListValue) ([]bool, error) {
  916. if pb == nil {
  917. return nil, errNilListValue("BOOL")
  918. }
  919. a := make([]bool, len(pb.Values))
  920. for i, v := range pb.Values {
  921. if err := decodeValue(v, boolType(), &a[i]); err != nil {
  922. return nil, errDecodeArrayElement(i, v, "BOOL", err)
  923. }
  924. }
  925. return a, nil
  926. }
  927. // decodeNullFloat64Array decodes proto3.ListValue pb into a NullFloat64 slice.
  928. func decodeNullFloat64Array(pb *proto3.ListValue) ([]NullFloat64, error) {
  929. if pb == nil {
  930. return nil, errNilListValue("FLOAT64")
  931. }
  932. a := make([]NullFloat64, len(pb.Values))
  933. for i, v := range pb.Values {
  934. if err := decodeValue(v, floatType(), &a[i]); err != nil {
  935. return nil, errDecodeArrayElement(i, v, "FLOAT64", err)
  936. }
  937. }
  938. return a, nil
  939. }
  940. // decodeFloat64Array decodes proto3.ListValue pb into a float64 slice.
  941. func decodeFloat64Array(pb *proto3.ListValue) ([]float64, error) {
  942. if pb == nil {
  943. return nil, errNilListValue("FLOAT64")
  944. }
  945. a := make([]float64, len(pb.Values))
  946. for i, v := range pb.Values {
  947. if err := decodeValue(v, floatType(), &a[i]); err != nil {
  948. return nil, errDecodeArrayElement(i, v, "FLOAT64", err)
  949. }
  950. }
  951. return a, nil
  952. }
  953. // decodeByteArray decodes proto3.ListValue pb into a slice of byte slice.
  954. func decodeByteArray(pb *proto3.ListValue) ([][]byte, error) {
  955. if pb == nil {
  956. return nil, errNilListValue("BYTES")
  957. }
  958. a := make([][]byte, len(pb.Values))
  959. for i, v := range pb.Values {
  960. if err := decodeValue(v, bytesType(), &a[i]); err != nil {
  961. return nil, errDecodeArrayElement(i, v, "BYTES", err)
  962. }
  963. }
  964. return a, nil
  965. }
  966. // decodeNullTimeArray decodes proto3.ListValue pb into a NullTime slice.
  967. func decodeNullTimeArray(pb *proto3.ListValue) ([]NullTime, error) {
  968. if pb == nil {
  969. return nil, errNilListValue("TIMESTAMP")
  970. }
  971. a := make([]NullTime, len(pb.Values))
  972. for i, v := range pb.Values {
  973. if err := decodeValue(v, timeType(), &a[i]); err != nil {
  974. return nil, errDecodeArrayElement(i, v, "TIMESTAMP", err)
  975. }
  976. }
  977. return a, nil
  978. }
  979. // decodeTimeArray decodes proto3.ListValue pb into a time.Time slice.
  980. func decodeTimeArray(pb *proto3.ListValue) ([]time.Time, error) {
  981. if pb == nil {
  982. return nil, errNilListValue("TIMESTAMP")
  983. }
  984. a := make([]time.Time, len(pb.Values))
  985. for i, v := range pb.Values {
  986. if err := decodeValue(v, timeType(), &a[i]); err != nil {
  987. return nil, errDecodeArrayElement(i, v, "TIMESTAMP", err)
  988. }
  989. }
  990. return a, nil
  991. }
  992. // decodeNullDateArray decodes proto3.ListValue pb into a NullDate slice.
  993. func decodeNullDateArray(pb *proto3.ListValue) ([]NullDate, error) {
  994. if pb == nil {
  995. return nil, errNilListValue("DATE")
  996. }
  997. a := make([]NullDate, len(pb.Values))
  998. for i, v := range pb.Values {
  999. if err := decodeValue(v, dateType(), &a[i]); err != nil {
  1000. return nil, errDecodeArrayElement(i, v, "DATE", err)
  1001. }
  1002. }
  1003. return a, nil
  1004. }
  1005. // decodeDateArray decodes proto3.ListValue pb into a civil.Date slice.
  1006. func decodeDateArray(pb *proto3.ListValue) ([]civil.Date, error) {
  1007. if pb == nil {
  1008. return nil, errNilListValue("DATE")
  1009. }
  1010. a := make([]civil.Date, len(pb.Values))
  1011. for i, v := range pb.Values {
  1012. if err := decodeValue(v, dateType(), &a[i]); err != nil {
  1013. return nil, errDecodeArrayElement(i, v, "DATE", err)
  1014. }
  1015. }
  1016. return a, nil
  1017. }
  1018. func errNotStructElement(i int, v *proto3.Value) error {
  1019. return errDecodeArrayElement(i, v, "STRUCT",
  1020. spannerErrorf(codes.FailedPrecondition, "%v(type: %T) doesn't encode Cloud Spanner STRUCT", v, v))
  1021. }
  1022. // decodeRowArray decodes proto3.ListValue pb into a NullRow slice according to
  1023. // the structural information given in sppb.StructType ty.
  1024. func decodeRowArray(ty *sppb.StructType, pb *proto3.ListValue) ([]NullRow, error) {
  1025. if pb == nil {
  1026. return nil, errNilListValue("STRUCT")
  1027. }
  1028. a := make([]NullRow, len(pb.Values))
  1029. for i := range pb.Values {
  1030. switch v := pb.Values[i].GetKind().(type) {
  1031. case *proto3.Value_ListValue:
  1032. a[i] = NullRow{
  1033. Row: Row{
  1034. fields: ty.Fields,
  1035. vals: v.ListValue.Values,
  1036. },
  1037. Valid: true,
  1038. }
  1039. // Null elements not currently supported by the server, see
  1040. // https://cloud.google.com/spanner/docs/query-syntax#using-structs-with-select
  1041. case *proto3.Value_NullValue:
  1042. // no-op, a[i] is NullRow{} already
  1043. default:
  1044. return nil, errNotStructElement(i, pb.Values[i])
  1045. }
  1046. }
  1047. return a, nil
  1048. }
  1049. // errNilSpannerStructType returns error for unexpected nil Cloud Spanner STRUCT schema type in decoding.
  1050. func errNilSpannerStructType() error {
  1051. return spannerErrorf(codes.FailedPrecondition, "unexpected nil StructType in decoding Cloud Spanner STRUCT")
  1052. }
  1053. // errUnnamedField returns error for decoding a Cloud Spanner STRUCT with unnamed field into a Go struct.
  1054. func errUnnamedField(ty *sppb.StructType, i int) error {
  1055. return spannerErrorf(codes.InvalidArgument, "unnamed field %v in Cloud Spanner STRUCT %+v", i, ty)
  1056. }
  1057. // errNoOrDupGoField returns error for decoding a Cloud Spanner
  1058. // STRUCT into a Go struct which is either missing a field, or has duplicate fields.
  1059. func errNoOrDupGoField(s interface{}, f string) error {
  1060. return spannerErrorf(codes.InvalidArgument, "Go struct %+v(type %T) has no or duplicate fields for Cloud Spanner STRUCT field %v", s, s, f)
  1061. }
  1062. // errDupColNames returns error for duplicated Cloud Spanner STRUCT field names found in decoding a Cloud Spanner STRUCT into a Go struct.
  1063. func errDupSpannerField(f string, ty *sppb.StructType) error {
  1064. return spannerErrorf(codes.InvalidArgument, "duplicated field name %q in Cloud Spanner STRUCT %+v", f, ty)
  1065. }
  1066. // errDecodeStructField returns error for failure in decoding a single field of a Cloud Spanner STRUCT.
  1067. func errDecodeStructField(ty *sppb.StructType, f string, err error) error {
  1068. se, ok := toSpannerError(err).(*Error)
  1069. if !ok {
  1070. return spannerErrorf(codes.Unknown,
  1071. "cannot decode field %v of Cloud Spanner STRUCT %+v, error = <%v>", f, ty, err)
  1072. }
  1073. se.decorate(fmt.Sprintf("cannot decode field %v of Cloud Spanner STRUCT %+v", f, ty))
  1074. return se
  1075. }
  1076. // decodeStruct decodes proto3.ListValue pb into struct referenced by pointer ptr, according to
  1077. // the structural information given in sppb.StructType ty.
  1078. func decodeStruct(ty *sppb.StructType, pb *proto3.ListValue, ptr interface{}) error {
  1079. if reflect.ValueOf(ptr).IsNil() {
  1080. return errNilDst(ptr)
  1081. }
  1082. if ty == nil {
  1083. return errNilSpannerStructType()
  1084. }
  1085. // t holds the structural information of ptr.
  1086. t := reflect.TypeOf(ptr).Elem()
  1087. // v is the actual value that ptr points to.
  1088. v := reflect.ValueOf(ptr).Elem()
  1089. fields, err := fieldCache.Fields(t)
  1090. if err != nil {
  1091. return toSpannerError(err)
  1092. }
  1093. seen := map[string]bool{}
  1094. for i, f := range ty.Fields {
  1095. if f.Name == "" {
  1096. return errUnnamedField(ty, i)
  1097. }
  1098. sf := fields.Match(f.Name)
  1099. if sf == nil {
  1100. return errNoOrDupGoField(ptr, f.Name)
  1101. }
  1102. if seen[f.Name] {
  1103. // We don't allow duplicated field name.
  1104. return errDupSpannerField(f.Name, ty)
  1105. }
  1106. // Try to decode a single field.
  1107. if err := decodeValue(pb.Values[i], f.Type, v.FieldByIndex(sf.Index).Addr().Interface()); err != nil {
  1108. return errDecodeStructField(ty, f.Name, err)
  1109. }
  1110. // Mark field f.Name as processed.
  1111. seen[f.Name] = true
  1112. }
  1113. return nil
  1114. }
  1115. // isPtrStructPtrSlice returns true if ptr is a pointer to a slice of struct pointers.
  1116. func isPtrStructPtrSlice(t reflect.Type) bool {
  1117. if t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Slice {
  1118. // t is not a pointer to a slice.
  1119. return false
  1120. }
  1121. if t = t.Elem(); t.Elem().Kind() != reflect.Ptr || t.Elem().Elem().Kind() != reflect.Struct {
  1122. // the slice that t points to is not a slice of struct pointers.
  1123. return false
  1124. }
  1125. return true
  1126. }
  1127. // decodeStructArray decodes proto3.ListValue pb into struct slice referenced by pointer ptr, according to the
  1128. // structural information given in a sppb.StructType.
  1129. func decodeStructArray(ty *sppb.StructType, pb *proto3.ListValue, ptr interface{}) error {
  1130. if pb == nil {
  1131. return errNilListValue("STRUCT")
  1132. }
  1133. // Type of the struct pointers stored in the slice that ptr points to.
  1134. ts := reflect.TypeOf(ptr).Elem().Elem()
  1135. // The slice that ptr points to, might be nil at this point.
  1136. v := reflect.ValueOf(ptr).Elem()
  1137. // Allocate empty slice.
  1138. v.Set(reflect.MakeSlice(v.Type(), 0, len(pb.Values)))
  1139. // Decode every struct in pb.Values.
  1140. for i, pv := range pb.Values {
  1141. // Check if pv is a NULL value.
  1142. if _, isNull := pv.Kind.(*proto3.Value_NullValue); isNull {
  1143. // Append a nil pointer to the slice.
  1144. v.Set(reflect.Append(v, reflect.New(ts).Elem()))
  1145. continue
  1146. }
  1147. // Allocate empty struct.
  1148. s := reflect.New(ts.Elem())
  1149. // Get proto3.ListValue l from proto3.Value pv.
  1150. l, err := getListValue(pv)
  1151. if err != nil {
  1152. return errDecodeArrayElement(i, pv, "STRUCT", err)
  1153. }
  1154. // Decode proto3.ListValue l into struct referenced by s.Interface().
  1155. if err = decodeStruct(ty, l, s.Interface()); err != nil {
  1156. return errDecodeArrayElement(i, pv, "STRUCT", err)
  1157. }
  1158. // Append the decoded struct back into the slice.
  1159. v.Set(reflect.Append(v, s))
  1160. }
  1161. return nil
  1162. }
  1163. // errEncoderUnsupportedType returns error for not being able to encode a value of
  1164. // certain type.
  1165. func errEncoderUnsupportedType(v interface{}) error {
  1166. return spannerErrorf(codes.InvalidArgument, "client doesn't support type %T", v)
  1167. }
  1168. // encodeValue encodes a Go native type into a proto3.Value.
  1169. func encodeValue(v interface{}) (*proto3.Value, *sppb.Type, error) {
  1170. pb := &proto3.Value{
  1171. Kind: &proto3.Value_NullValue{NullValue: proto3.NullValue_NULL_VALUE},
  1172. }
  1173. var pt *sppb.Type
  1174. var err error
  1175. switch v := v.(type) {
  1176. case nil:
  1177. case string:
  1178. pb.Kind = stringKind(v)
  1179. pt = stringType()
  1180. case NullString:
  1181. if v.Valid {
  1182. return encodeValue(v.StringVal)
  1183. }
  1184. pt = stringType()
  1185. case []string:
  1186. if v != nil {
  1187. pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
  1188. if err != nil {
  1189. return nil, nil, err
  1190. }
  1191. }
  1192. pt = listType(stringType())
  1193. case []NullString:
  1194. if v != nil {
  1195. pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
  1196. if err != nil {
  1197. return nil, nil, err
  1198. }
  1199. }
  1200. pt = listType(stringType())
  1201. case []byte:
  1202. if v != nil {
  1203. pb.Kind = stringKind(base64.StdEncoding.EncodeToString(v))
  1204. }
  1205. pt = bytesType()
  1206. case [][]byte:
  1207. if v != nil {
  1208. pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
  1209. if err != nil {
  1210. return nil, nil, err
  1211. }
  1212. }
  1213. pt = listType(bytesType())
  1214. case int:
  1215. pb.Kind = stringKind(strconv.FormatInt(int64(v), 10))
  1216. pt = intType()
  1217. case []int:
  1218. if v != nil {
  1219. pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
  1220. if err != nil {
  1221. return nil, nil, err
  1222. }
  1223. }
  1224. pt = listType(intType())
  1225. case int64:
  1226. pb.Kind = stringKind(strconv.FormatInt(v, 10))
  1227. pt = intType()
  1228. case []int64:
  1229. if v != nil {
  1230. pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
  1231. if err != nil {
  1232. return nil, nil, err
  1233. }
  1234. }
  1235. pt = listType(intType())
  1236. case NullInt64:
  1237. if v.Valid {
  1238. return encodeValue(v.Int64)
  1239. }
  1240. pt = intType()
  1241. case []NullInt64:
  1242. if v != nil {
  1243. pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
  1244. if err != nil {
  1245. return nil, nil, err
  1246. }
  1247. }
  1248. pt = listType(intType())
  1249. case bool:
  1250. pb.Kind = &proto3.Value_BoolValue{BoolValue: v}
  1251. pt = boolType()
  1252. case []bool:
  1253. if v != nil {
  1254. pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
  1255. if err != nil {
  1256. return nil, nil, err
  1257. }
  1258. }
  1259. pt = listType(boolType())
  1260. case NullBool:
  1261. if v.Valid {
  1262. return encodeValue(v.Bool)
  1263. }
  1264. pt = boolType()
  1265. case []NullBool:
  1266. if v != nil {
  1267. pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
  1268. if err != nil {
  1269. return nil, nil, err
  1270. }
  1271. }
  1272. pt = listType(boolType())
  1273. case float64:
  1274. pb.Kind = &proto3.Value_NumberValue{NumberValue: v}
  1275. pt = floatType()
  1276. case []float64:
  1277. if v != nil {
  1278. pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
  1279. if err != nil {
  1280. return nil, nil, err
  1281. }
  1282. }
  1283. pt = listType(floatType())
  1284. case NullFloat64:
  1285. if v.Valid {
  1286. return encodeValue(v.Float64)
  1287. }
  1288. pt = floatType()
  1289. case []NullFloat64:
  1290. if v != nil {
  1291. pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
  1292. if err != nil {
  1293. return nil, nil, err
  1294. }
  1295. }
  1296. pt = listType(floatType())
  1297. case time.Time:
  1298. if v == commitTimestamp {
  1299. pb.Kind = stringKind(commitTimestampPlaceholderString)
  1300. } else {
  1301. pb.Kind = stringKind(v.UTC().Format(time.RFC3339Nano))
  1302. }
  1303. pt = timeType()
  1304. case []time.Time:
  1305. if v != nil {
  1306. pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
  1307. if err != nil {
  1308. return nil, nil, err
  1309. }
  1310. }
  1311. pt = listType(timeType())
  1312. case NullTime:
  1313. if v.Valid {
  1314. return encodeValue(v.Time)
  1315. }
  1316. pt = timeType()
  1317. case []NullTime:
  1318. if v != nil {
  1319. pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
  1320. if err != nil {
  1321. return nil, nil, err
  1322. }
  1323. }
  1324. pt = listType(timeType())
  1325. case civil.Date:
  1326. pb.Kind = stringKind(v.String())
  1327. pt = dateType()
  1328. case []civil.Date:
  1329. if v != nil {
  1330. pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
  1331. if err != nil {
  1332. return nil, nil, err
  1333. }
  1334. }
  1335. pt = listType(dateType())
  1336. case NullDate:
  1337. if v.Valid {
  1338. return encodeValue(v.Date)
  1339. }
  1340. pt = dateType()
  1341. case []NullDate:
  1342. if v != nil {
  1343. pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
  1344. if err != nil {
  1345. return nil, nil, err
  1346. }
  1347. }
  1348. pt = listType(dateType())
  1349. case GenericColumnValue:
  1350. // Deep clone to ensure subsequent changes to v before
  1351. // transmission don't affect our encoded value.
  1352. pb = proto.Clone(v.Value).(*proto3.Value)
  1353. pt = proto.Clone(v.Type).(*sppb.Type)
  1354. case []GenericColumnValue:
  1355. return nil, nil, errEncoderUnsupportedType(v)
  1356. default:
  1357. if !isStructOrArrayOfStructValue(v) {
  1358. return nil, nil, errEncoderUnsupportedType(v)
  1359. }
  1360. typ := reflect.TypeOf(v)
  1361. // Value is a Go struct value/ptr.
  1362. if (typ.Kind() == reflect.Struct) ||
  1363. (typ.Kind() == reflect.Ptr && typ.Elem().Kind() == reflect.Struct) {
  1364. return encodeStruct(v)
  1365. }
  1366. // Value is a slice of Go struct values/ptrs.
  1367. if typ.Kind() == reflect.Slice {
  1368. return encodeStructArray(v)
  1369. }
  1370. }
  1371. return pb, pt, nil
  1372. }
  1373. // Encodes a Go struct value/ptr in v to the spanner Value and Type protos. v itself must
  1374. // be non-nil.
  1375. func encodeStruct(v interface{}) (*proto3.Value, *sppb.Type, error) {
  1376. typ := reflect.TypeOf(v)
  1377. val := reflect.ValueOf(v)
  1378. // Pointer to struct.
  1379. if typ.Kind() == reflect.Ptr && typ.Elem().Kind() == reflect.Struct {
  1380. typ = typ.Elem()
  1381. if val.IsNil() {
  1382. // nil pointer to struct, representing a NULL STRUCT value. Use a dummy value to
  1383. // get the type.
  1384. _, st, err := encodeStruct(reflect.Zero(typ).Interface())
  1385. if err != nil {
  1386. return nil, nil, err
  1387. }
  1388. return nullProto(), st, nil
  1389. }
  1390. val = val.Elem()
  1391. }
  1392. if typ.Kind() != reflect.Struct {
  1393. return nil, nil, errEncoderUnsupportedType(v)
  1394. }
  1395. stf := make([]*sppb.StructType_Field, 0, typ.NumField())
  1396. stv := make([]*proto3.Value, 0, typ.NumField())
  1397. for i := 0; i < typ.NumField(); i++ {
  1398. // If the field has a 'spanner' tag, use the value of that tag as the field name.
  1399. // This is used to build STRUCT types with unnamed/duplicate fields.
  1400. sf := typ.Field(i)
  1401. fval := val.Field(i)
  1402. // Embedded fields are not allowed.
  1403. if sf.Anonymous {
  1404. return nil, nil, errUnsupportedEmbeddedStructFields(sf.Name)
  1405. }
  1406. // Unexported fields are ignored.
  1407. if !fval.CanInterface() {
  1408. continue
  1409. }
  1410. fname, ok := sf.Tag.Lookup("spanner")
  1411. if !ok {
  1412. fname = sf.Name
  1413. }
  1414. eval, etype, err := encodeValue(fval.Interface())
  1415. if err != nil {
  1416. return nil, nil, err
  1417. }
  1418. stf = append(stf, mkField(fname, etype))
  1419. stv = append(stv, eval)
  1420. }
  1421. return listProto(stv...), structType(stf...), nil
  1422. }
  1423. // Encodes a slice of Go struct values/ptrs in v to the spanner Value and Type protos. v itself
  1424. // must be non-nil.
  1425. func encodeStructArray(v interface{}) (*proto3.Value, *sppb.Type, error) {
  1426. etyp := reflect.TypeOf(v).Elem()
  1427. sliceval := reflect.ValueOf(v)
  1428. // Slice of pointers to structs.
  1429. if etyp.Kind() == reflect.Ptr {
  1430. etyp = etyp.Elem()
  1431. }
  1432. // Use a dummy struct value to get the element type
  1433. _, elemTyp, err := encodeStruct(reflect.Zero(etyp).Interface())
  1434. if err != nil {
  1435. return nil, nil, err
  1436. }
  1437. // nil slice represents a NULL array-of-struct.
  1438. if sliceval.IsNil() {
  1439. return nullProto(), listType(elemTyp), nil
  1440. }
  1441. values := make([]*proto3.Value, 0, sliceval.Len())
  1442. for i := 0; i < sliceval.Len(); i++ {
  1443. ev, _, err := encodeStruct(sliceval.Index(i).Interface())
  1444. if err != nil {
  1445. return nil, nil, err
  1446. }
  1447. values = append(values, ev)
  1448. }
  1449. return listProto(values...), listType(elemTyp), nil
  1450. }
  1451. func isStructOrArrayOfStructValue(v interface{}) bool {
  1452. typ := reflect.TypeOf(v)
  1453. if typ.Kind() == reflect.Slice {
  1454. typ = typ.Elem()
  1455. }
  1456. if typ.Kind() == reflect.Ptr {
  1457. typ = typ.Elem()
  1458. }
  1459. return typ.Kind() == reflect.Struct
  1460. }
  1461. func isSupportedMutationType(v interface{}) bool {
  1462. switch v.(type) {
  1463. case nil, string, NullString, []string, []NullString,
  1464. []byte, [][]byte,
  1465. int, []int, int64, []int64, NullInt64, []NullInt64,
  1466. bool, []bool, NullBool, []NullBool,
  1467. float64, []float64, NullFloat64, []NullFloat64,
  1468. time.Time, []time.Time, NullTime, []NullTime,
  1469. civil.Date, []civil.Date, NullDate, []NullDate,
  1470. GenericColumnValue:
  1471. return true
  1472. default:
  1473. return false
  1474. }
  1475. }
  1476. // encodeValueArray encodes a Value array into a proto3.ListValue.
  1477. func encodeValueArray(vs []interface{}) (*proto3.ListValue, error) {
  1478. lv := &proto3.ListValue{}
  1479. lv.Values = make([]*proto3.Value, 0, len(vs))
  1480. for _, v := range vs {
  1481. if !isSupportedMutationType(v) {
  1482. return nil, errEncoderUnsupportedType(v)
  1483. }
  1484. pb, _, err := encodeValue(v)
  1485. if err != nil {
  1486. return nil, err
  1487. }
  1488. lv.Values = append(lv.Values, pb)
  1489. }
  1490. return lv, nil
  1491. }
  1492. // encodeArray assumes that all values of the array element type encode without error.
  1493. func encodeArray(len int, at func(int) interface{}) (*proto3.Value, error) {
  1494. vs := make([]*proto3.Value, len)
  1495. var err error
  1496. for i := 0; i < len; i++ {
  1497. vs[i], _, err = encodeValue(at(i))
  1498. if err != nil {
  1499. return nil, err
  1500. }
  1501. }
  1502. return listProto(vs...), nil
  1503. }
  1504. func spannerTagParser(t reflect.StructTag) (name string, keep bool, other interface{}, err error) {
  1505. if s := t.Get("spanner"); s != "" {
  1506. if s == "-" {
  1507. return "", false, nil, nil
  1508. }
  1509. return s, true, nil, nil
  1510. }
  1511. return "", true, nil, nil
  1512. }
  1513. var fieldCache = fields.NewCache(spannerTagParser, nil, nil)