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.
 
 
 

3496 lines
70 KiB

  1. // Copyright 2014 Google LLC
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package datastore
  15. import (
  16. "encoding/json"
  17. "errors"
  18. "fmt"
  19. "reflect"
  20. "sort"
  21. "strings"
  22. "testing"
  23. "time"
  24. "cloud.google.com/go/internal/testutil"
  25. "github.com/golang/protobuf/proto"
  26. "github.com/google/go-cmp/cmp"
  27. "golang.org/x/net/context"
  28. pb "google.golang.org/genproto/googleapis/datastore/v1"
  29. "google.golang.org/grpc"
  30. )
  31. type (
  32. myBlob []byte
  33. myByte byte
  34. myString string
  35. )
  36. func makeMyByteSlice(n int) []myByte {
  37. b := make([]myByte, n)
  38. for i := range b {
  39. b[i] = myByte(i)
  40. }
  41. return b
  42. }
  43. func makeInt8Slice(n int) []int8 {
  44. b := make([]int8, n)
  45. for i := range b {
  46. b[i] = int8(i)
  47. }
  48. return b
  49. }
  50. func makeUint8Slice(n int) []uint8 {
  51. b := make([]uint8, n)
  52. for i := range b {
  53. b[i] = uint8(i)
  54. }
  55. return b
  56. }
  57. func newKey(stringID string, parent *Key) *Key {
  58. return NameKey("kind", stringID, parent)
  59. }
  60. var (
  61. testKey0 = newKey("name0", nil)
  62. testKey1a = newKey("name1", nil)
  63. testKey1b = newKey("name1", nil)
  64. testKey2a = newKey("name2", testKey0)
  65. testKey2b = newKey("name2", testKey0)
  66. testGeoPt0 = GeoPoint{Lat: 1.2, Lng: 3.4}
  67. testGeoPt1 = GeoPoint{Lat: 5, Lng: 10}
  68. testBadGeoPt = GeoPoint{Lat: 1000, Lng: 34}
  69. ts = time.Unix(1e9, 0).UTC()
  70. )
  71. type B0 struct {
  72. B []byte `datastore:",noindex"`
  73. }
  74. type B1 struct {
  75. B []int8
  76. }
  77. type B2 struct {
  78. B myBlob `datastore:",noindex"`
  79. }
  80. type B3 struct {
  81. B []myByte `datastore:",noindex"`
  82. }
  83. type B4 struct {
  84. B [][]byte
  85. }
  86. type C0 struct {
  87. I int
  88. C chan int
  89. }
  90. type C1 struct {
  91. I int
  92. C *chan int
  93. }
  94. type C2 struct {
  95. I int
  96. C []chan int
  97. }
  98. type C3 struct {
  99. C string
  100. }
  101. type c4 struct {
  102. C string
  103. }
  104. type E struct{}
  105. type G0 struct {
  106. G GeoPoint
  107. }
  108. type G1 struct {
  109. G []GeoPoint
  110. }
  111. type K0 struct {
  112. K *Key
  113. }
  114. type K1 struct {
  115. K []*Key
  116. }
  117. type S struct {
  118. St string
  119. }
  120. type NoOmit struct {
  121. A string
  122. B int `datastore:"Bb"`
  123. C bool `datastore:",noindex"`
  124. }
  125. type OmitAll struct {
  126. A string `datastore:",omitempty"`
  127. B int `datastore:"Bb,omitempty"`
  128. C bool `datastore:",omitempty,noindex"`
  129. D time.Time `datastore:",omitempty"`
  130. F []int `datastore:",omitempty"`
  131. }
  132. type Omit struct {
  133. A string `datastore:",omitempty"`
  134. B int `datastore:"Bb,omitempty"`
  135. C bool `datastore:",omitempty,noindex"`
  136. D time.Time `datastore:",omitempty"`
  137. F []int `datastore:",omitempty"`
  138. S `datastore:",omitempty"`
  139. }
  140. type NoOmits struct {
  141. No []NoOmit `datastore:",omitempty"`
  142. S `datastore:",omitempty"`
  143. Ss S `datastore:",omitempty"`
  144. }
  145. type N0 struct {
  146. X0
  147. Nonymous X0
  148. Ignore string `datastore:"-"`
  149. Other string
  150. }
  151. type N1 struct {
  152. X0
  153. Nonymous []X0
  154. Ignore string `datastore:"-"`
  155. Other string
  156. }
  157. type N2 struct {
  158. N1 `datastore:"red"`
  159. Green N1 `datastore:"green"`
  160. Blue N1
  161. White N1 `datastore:"-"`
  162. }
  163. type N3 struct {
  164. C3 `datastore:"red"`
  165. }
  166. type N4 struct {
  167. c4
  168. }
  169. type N5 struct {
  170. c4 `datastore:"red"`
  171. }
  172. type O0 struct {
  173. I int64
  174. }
  175. type O1 struct {
  176. I int32
  177. }
  178. type U0 struct {
  179. U uint
  180. }
  181. type U1 struct {
  182. U string
  183. }
  184. type T struct {
  185. T time.Time
  186. }
  187. type X0 struct {
  188. S string
  189. I int
  190. i int
  191. }
  192. type X1 struct {
  193. S myString
  194. I int32
  195. J int64
  196. }
  197. type X2 struct {
  198. Z string
  199. i int
  200. }
  201. type X3 struct {
  202. S bool
  203. I int
  204. }
  205. type Y0 struct {
  206. B bool
  207. F []float64
  208. G []float64
  209. }
  210. type Y1 struct {
  211. B bool
  212. F float64
  213. }
  214. type Y2 struct {
  215. B bool
  216. F []int64
  217. }
  218. type Pointers struct {
  219. Pi *int
  220. Ps *string
  221. Pb *bool
  222. Pf *float64
  223. Pg *GeoPoint
  224. Pt *time.Time
  225. }
  226. type PointersOmitEmpty struct {
  227. Pi *int `datastore:",omitempty"`
  228. Ps *string `datastore:",omitempty"`
  229. Pb *bool `datastore:",omitempty"`
  230. Pf *float64 `datastore:",omitempty"`
  231. Pg *GeoPoint `datastore:",omitempty"`
  232. Pt *time.Time `datastore:",omitempty"`
  233. }
  234. func populatedPointers() *Pointers {
  235. var (
  236. i int
  237. s string
  238. b bool
  239. f float64
  240. g GeoPoint
  241. t time.Time
  242. )
  243. return &Pointers{
  244. Pi: &i,
  245. Ps: &s,
  246. Pb: &b,
  247. Pf: &f,
  248. Pg: &g,
  249. Pt: &t,
  250. }
  251. }
  252. type Tagged struct {
  253. A int `datastore:"a,noindex"`
  254. B []int `datastore:"b"`
  255. C int `datastore:",noindex"`
  256. D int `datastore:""`
  257. E int
  258. I int `datastore:"-"`
  259. J int `datastore:",noindex" json:"j"`
  260. Y0 `datastore:"-"`
  261. Z chan int `datastore:"-"`
  262. }
  263. type InvalidTagged1 struct {
  264. I int `datastore:"\t"`
  265. }
  266. type InvalidTagged2 struct {
  267. I int
  268. J int `datastore:"I"`
  269. }
  270. type InvalidTagged3 struct {
  271. X string `datastore:"-,noindex"`
  272. }
  273. type InvalidTagged4 struct {
  274. X string `datastore:",garbage"`
  275. }
  276. type Inner1 struct {
  277. W int32
  278. X string
  279. }
  280. type Inner2 struct {
  281. Y float64
  282. }
  283. type Inner3 struct {
  284. Z bool
  285. }
  286. type Inner5 struct {
  287. WW int
  288. }
  289. type Inner4 struct {
  290. X Inner5
  291. }
  292. type Outer struct {
  293. A int16
  294. I []Inner1
  295. J Inner2
  296. Inner3
  297. }
  298. type OuterFlatten struct {
  299. A int16
  300. I []Inner1 `datastore:",flatten"`
  301. J Inner2 `datastore:",flatten,noindex"`
  302. Inner3 `datastore:",flatten"`
  303. K Inner4 `datastore:",flatten"`
  304. }
  305. type OuterEquivalent struct {
  306. A int16
  307. IDotW []int32 `datastore:"I.W"`
  308. IDotX []string `datastore:"I.X"`
  309. JDotY float64 `datastore:"J.Y"`
  310. Z bool
  311. }
  312. type Dotted struct {
  313. A DottedA `datastore:"A0.A1.A2"`
  314. }
  315. type DottedA struct {
  316. B DottedB `datastore:"B3"`
  317. }
  318. type DottedB struct {
  319. C int `datastore:"C4.C5"`
  320. }
  321. type SliceOfSlices struct {
  322. I int
  323. S []struct {
  324. J int
  325. F []float64
  326. } `datastore:",flatten"`
  327. }
  328. type Recursive struct {
  329. I int
  330. R []Recursive
  331. }
  332. type MutuallyRecursive0 struct {
  333. I int
  334. R []MutuallyRecursive1
  335. }
  336. type MutuallyRecursive1 struct {
  337. I int
  338. R []MutuallyRecursive0
  339. }
  340. type EntityWithKey struct {
  341. I int
  342. S string
  343. K *Key `datastore:"__key__"`
  344. }
  345. type EntityWithKey2 EntityWithKey
  346. type WithNestedEntityWithKey struct {
  347. N EntityWithKey
  348. }
  349. type WithNonKeyField struct {
  350. I int
  351. K string `datastore:"__key__"`
  352. }
  353. type NestedWithNonKeyField struct {
  354. N WithNonKeyField
  355. }
  356. type Basic struct {
  357. A string
  358. }
  359. type PtrToStructField struct {
  360. B *Basic
  361. C *Basic `datastore:"c,noindex"`
  362. *Basic
  363. D []*Basic
  364. }
  365. var two int = 2
  366. type EmbeddedTime struct {
  367. time.Time
  368. }
  369. type SpecialTime struct {
  370. MyTime EmbeddedTime
  371. }
  372. type Doubler struct {
  373. S string
  374. I int64
  375. B bool
  376. }
  377. type Repeat struct {
  378. Key string
  379. Value []byte
  380. }
  381. type Repeated struct {
  382. Repeats []Repeat
  383. }
  384. func (d *Doubler) Load(props []Property) error {
  385. return LoadStruct(d, props)
  386. }
  387. func (d *Doubler) Save() ([]Property, error) {
  388. // Save the default Property slice to an in-memory buffer (a PropertyList).
  389. props, err := SaveStruct(d)
  390. if err != nil {
  391. return nil, err
  392. }
  393. var list PropertyList
  394. if err := list.Load(props); err != nil {
  395. return nil, err
  396. }
  397. // Edit that PropertyList, and send it on.
  398. for i := range list {
  399. switch v := list[i].Value.(type) {
  400. case string:
  401. // + means string concatenation.
  402. list[i].Value = v + v
  403. case int64:
  404. // + means integer addition.
  405. list[i].Value = v + v
  406. }
  407. }
  408. return list.Save()
  409. }
  410. var _ PropertyLoadSaver = (*Doubler)(nil)
  411. type Deriver struct {
  412. S, Derived, Ignored string
  413. }
  414. func (e *Deriver) Load(props []Property) error {
  415. for _, p := range props {
  416. if p.Name != "S" {
  417. continue
  418. }
  419. e.S = p.Value.(string)
  420. e.Derived = "derived+" + e.S
  421. }
  422. return nil
  423. }
  424. func (e *Deriver) Save() ([]Property, error) {
  425. return []Property{
  426. {
  427. Name: "S",
  428. Value: e.S,
  429. },
  430. }, nil
  431. }
  432. var _ PropertyLoadSaver = (*Deriver)(nil)
  433. type BadMultiPropEntity struct{}
  434. func (e *BadMultiPropEntity) Load(props []Property) error {
  435. return errors.New("unimplemented")
  436. }
  437. func (e *BadMultiPropEntity) Save() ([]Property, error) {
  438. // Write multiple properties with the same name "I".
  439. var props []Property
  440. for i := 0; i < 3; i++ {
  441. props = append(props, Property{
  442. Name: "I",
  443. Value: int64(i),
  444. })
  445. }
  446. return props, nil
  447. }
  448. var _ PropertyLoadSaver = (*BadMultiPropEntity)(nil)
  449. type testCase struct {
  450. desc string
  451. src interface{}
  452. want interface{}
  453. putErr string
  454. getErr string
  455. }
  456. var testCases = []testCase{
  457. {
  458. "chan save fails",
  459. &C0{I: -1},
  460. &E{},
  461. "unsupported struct field",
  462. "",
  463. },
  464. {
  465. "*chan save fails",
  466. &C1{I: -1},
  467. &E{},
  468. "unsupported struct field",
  469. "",
  470. },
  471. {
  472. "[]chan save fails",
  473. &C2{I: -1, C: make([]chan int, 8)},
  474. &E{},
  475. "unsupported struct field",
  476. "",
  477. },
  478. {
  479. "chan load fails",
  480. &C3{C: "not a chan"},
  481. &C0{},
  482. "",
  483. "type mismatch",
  484. },
  485. {
  486. "*chan load fails",
  487. &C3{C: "not a *chan"},
  488. &C1{},
  489. "",
  490. "type mismatch",
  491. },
  492. {
  493. "[]chan load fails",
  494. &C3{C: "not a []chan"},
  495. &C2{},
  496. "",
  497. "type mismatch",
  498. },
  499. {
  500. "empty struct",
  501. &E{},
  502. &E{},
  503. "",
  504. "",
  505. },
  506. {
  507. "geopoint",
  508. &G0{G: testGeoPt0},
  509. &G0{G: testGeoPt0},
  510. "",
  511. "",
  512. },
  513. {
  514. "geopoint invalid",
  515. &G0{G: testBadGeoPt},
  516. &G0{},
  517. "invalid GeoPoint value",
  518. "",
  519. },
  520. {
  521. "geopoint as props",
  522. &G0{G: testGeoPt0},
  523. &PropertyList{
  524. Property{Name: "G", Value: testGeoPt0, NoIndex: false},
  525. },
  526. "",
  527. "",
  528. },
  529. {
  530. "geopoint slice",
  531. &G1{G: []GeoPoint{testGeoPt0, testGeoPt1}},
  532. &G1{G: []GeoPoint{testGeoPt0, testGeoPt1}},
  533. "",
  534. "",
  535. },
  536. {
  537. "omit empty, all",
  538. &OmitAll{},
  539. new(PropertyList),
  540. "",
  541. "",
  542. },
  543. {
  544. "omit empty",
  545. &Omit{},
  546. &PropertyList{
  547. Property{Name: "St", Value: "", NoIndex: false},
  548. },
  549. "",
  550. "",
  551. },
  552. {
  553. "omit empty, fields populated",
  554. &Omit{
  555. A: "a",
  556. B: 10,
  557. C: true,
  558. F: []int{11},
  559. },
  560. &PropertyList{
  561. Property{Name: "A", Value: "a", NoIndex: false},
  562. Property{Name: "Bb", Value: int64(10), NoIndex: false},
  563. Property{Name: "C", Value: true, NoIndex: true},
  564. Property{Name: "F", Value: []interface{}{int64(11)}, NoIndex: false},
  565. Property{Name: "St", Value: "", NoIndex: false},
  566. },
  567. "",
  568. "",
  569. },
  570. {
  571. "omit empty, fields populated",
  572. &Omit{
  573. A: "a",
  574. B: 10,
  575. C: true,
  576. F: []int{11},
  577. S: S{St: "string"},
  578. },
  579. &PropertyList{
  580. Property{Name: "A", Value: "a", NoIndex: false},
  581. Property{Name: "Bb", Value: int64(10), NoIndex: false},
  582. Property{Name: "C", Value: true, NoIndex: true},
  583. Property{Name: "F", Value: []interface{}{int64(11)}, NoIndex: false},
  584. Property{Name: "St", Value: "string", NoIndex: false},
  585. },
  586. "",
  587. "",
  588. },
  589. {
  590. "omit empty does not propagate",
  591. &NoOmits{
  592. No: []NoOmit{
  593. {},
  594. },
  595. S: S{},
  596. Ss: S{},
  597. },
  598. &PropertyList{
  599. Property{Name: "No", Value: []interface{}{
  600. &Entity{
  601. Properties: []Property{
  602. {Name: "A", Value: "", NoIndex: false},
  603. {Name: "Bb", Value: int64(0), NoIndex: false},
  604. {Name: "C", Value: false, NoIndex: true},
  605. },
  606. },
  607. }, NoIndex: false},
  608. Property{Name: "Ss", Value: &Entity{
  609. Properties: []Property{
  610. {Name: "St", Value: "", NoIndex: false},
  611. },
  612. }, NoIndex: false},
  613. Property{Name: "St", Value: "", NoIndex: false},
  614. },
  615. "",
  616. "",
  617. },
  618. {
  619. "key",
  620. &K0{K: testKey1a},
  621. &K0{K: testKey1b},
  622. "",
  623. "",
  624. },
  625. {
  626. "key with parent",
  627. &K0{K: testKey2a},
  628. &K0{K: testKey2b},
  629. "",
  630. "",
  631. },
  632. {
  633. "nil key",
  634. &K0{},
  635. &K0{},
  636. "",
  637. "",
  638. },
  639. {
  640. "all nil keys in slice",
  641. &K1{[]*Key{nil, nil}},
  642. &K1{[]*Key{nil, nil}},
  643. "",
  644. "",
  645. },
  646. {
  647. "some nil keys in slice",
  648. &K1{[]*Key{testKey1a, nil, testKey2a}},
  649. &K1{[]*Key{testKey1b, nil, testKey2b}},
  650. "",
  651. "",
  652. },
  653. {
  654. "overflow",
  655. &O0{I: 1 << 48},
  656. &O1{},
  657. "",
  658. "overflow",
  659. },
  660. {
  661. "time",
  662. &T{T: time.Unix(1e9, 0)},
  663. &T{T: time.Unix(1e9, 0)},
  664. "",
  665. "",
  666. },
  667. {
  668. "time as props",
  669. &T{T: time.Unix(1e9, 0)},
  670. &PropertyList{
  671. Property{Name: "T", Value: time.Unix(1e9, 0), NoIndex: false},
  672. },
  673. "",
  674. "",
  675. },
  676. {
  677. "uint save",
  678. &U0{U: 1},
  679. &U0{},
  680. "unsupported struct field",
  681. "",
  682. },
  683. {
  684. "uint load",
  685. &U1{U: "not a uint"},
  686. &U0{},
  687. "",
  688. "type mismatch",
  689. },
  690. {
  691. "zero",
  692. &X0{},
  693. &X0{},
  694. "",
  695. "",
  696. },
  697. {
  698. "basic",
  699. &X0{S: "one", I: 2, i: 3},
  700. &X0{S: "one", I: 2},
  701. "",
  702. "",
  703. },
  704. {
  705. "save string/int load myString/int32",
  706. &X0{S: "one", I: 2, i: 3},
  707. &X1{S: "one", I: 2},
  708. "",
  709. "",
  710. },
  711. {
  712. "missing fields",
  713. &X0{S: "one", I: 2, i: 3},
  714. &X2{},
  715. "",
  716. "no such struct field",
  717. },
  718. {
  719. "save string load bool",
  720. &X0{S: "one", I: 2, i: 3},
  721. &X3{I: 2},
  722. "",
  723. "type mismatch",
  724. },
  725. {
  726. "basic slice",
  727. &Y0{B: true, F: []float64{7, 8, 9}},
  728. &Y0{B: true, F: []float64{7, 8, 9}},
  729. "",
  730. "",
  731. },
  732. {
  733. "save []float64 load float64",
  734. &Y0{B: true, F: []float64{7, 8, 9}},
  735. &Y1{B: true},
  736. "",
  737. "requires a slice",
  738. },
  739. {
  740. "save []float64 load []int64",
  741. &Y0{B: true, F: []float64{7, 8, 9}},
  742. &Y2{B: true},
  743. "",
  744. "type mismatch",
  745. },
  746. {
  747. "single slice is too long",
  748. &Y0{F: make([]float64, maxIndexedProperties+1)},
  749. &Y0{},
  750. "too many indexed properties",
  751. "",
  752. },
  753. {
  754. "two slices are too long",
  755. &Y0{F: make([]float64, maxIndexedProperties), G: make([]float64, maxIndexedProperties)},
  756. &Y0{},
  757. "too many indexed properties",
  758. "",
  759. },
  760. {
  761. "one slice and one scalar are too long",
  762. &Y0{F: make([]float64, maxIndexedProperties), B: true},
  763. &Y0{},
  764. "too many indexed properties",
  765. "",
  766. },
  767. {
  768. "slice of slices of bytes",
  769. &Repeated{
  770. Repeats: []Repeat{
  771. {
  772. Key: "key 1",
  773. Value: []byte("value 1"),
  774. },
  775. {
  776. Key: "key 2",
  777. Value: []byte("value 2"),
  778. },
  779. },
  780. },
  781. &Repeated{
  782. Repeats: []Repeat{
  783. {
  784. Key: "key 1",
  785. Value: []byte("value 1"),
  786. },
  787. {
  788. Key: "key 2",
  789. Value: []byte("value 2"),
  790. },
  791. },
  792. },
  793. "",
  794. "",
  795. },
  796. {
  797. "long blob",
  798. &B0{B: makeUint8Slice(maxIndexedProperties + 1)},
  799. &B0{B: makeUint8Slice(maxIndexedProperties + 1)},
  800. "",
  801. "",
  802. },
  803. {
  804. "long []int8 is too long",
  805. &B1{B: makeInt8Slice(maxIndexedProperties + 1)},
  806. &B1{},
  807. "too many indexed properties",
  808. "",
  809. },
  810. {
  811. "short []int8",
  812. &B1{B: makeInt8Slice(3)},
  813. &B1{B: makeInt8Slice(3)},
  814. "",
  815. "",
  816. },
  817. {
  818. "long myBlob",
  819. &B2{B: makeUint8Slice(maxIndexedProperties + 1)},
  820. &B2{B: makeUint8Slice(maxIndexedProperties + 1)},
  821. "",
  822. "",
  823. },
  824. {
  825. "short myBlob",
  826. &B2{B: makeUint8Slice(3)},
  827. &B2{B: makeUint8Slice(3)},
  828. "",
  829. "",
  830. },
  831. {
  832. "long []myByte",
  833. &B3{B: makeMyByteSlice(maxIndexedProperties + 1)},
  834. &B3{B: makeMyByteSlice(maxIndexedProperties + 1)},
  835. "",
  836. "",
  837. },
  838. {
  839. "short []myByte",
  840. &B3{B: makeMyByteSlice(3)},
  841. &B3{B: makeMyByteSlice(3)},
  842. "",
  843. "",
  844. },
  845. {
  846. "slice of blobs",
  847. &B4{B: [][]byte{
  848. makeUint8Slice(3),
  849. makeUint8Slice(4),
  850. makeUint8Slice(5),
  851. }},
  852. &B4{B: [][]byte{
  853. makeUint8Slice(3),
  854. makeUint8Slice(4),
  855. makeUint8Slice(5),
  856. }},
  857. "",
  858. "",
  859. },
  860. {
  861. "[]byte must be noindex",
  862. &PropertyList{
  863. Property{Name: "B", Value: makeUint8Slice(1501), NoIndex: false},
  864. },
  865. nil,
  866. "[]byte property too long to index",
  867. "",
  868. },
  869. {
  870. "string must be noindex",
  871. &PropertyList{
  872. Property{Name: "B", Value: strings.Repeat("x", 1501), NoIndex: false},
  873. },
  874. nil,
  875. "string property too long to index",
  876. "",
  877. },
  878. {
  879. "slice of []byte must be noindex",
  880. &PropertyList{
  881. Property{Name: "B", Value: []interface{}{
  882. []byte("short"),
  883. makeUint8Slice(1501),
  884. }, NoIndex: false},
  885. },
  886. nil,
  887. "[]byte property too long to index",
  888. "",
  889. },
  890. {
  891. "slice of string must be noindex",
  892. &PropertyList{
  893. Property{Name: "B", Value: []interface{}{
  894. "short",
  895. strings.Repeat("x", 1501),
  896. }, NoIndex: false},
  897. },
  898. nil,
  899. "string property too long to index",
  900. "",
  901. },
  902. {
  903. "save tagged load props",
  904. &Tagged{A: 1, B: []int{21, 22, 23}, C: 3, D: 4, E: 5, I: 6, J: 7},
  905. &PropertyList{
  906. // A and B are renamed to a and b; A and C are noindex, I is ignored.
  907. // Order is sorted as per byName.
  908. Property{Name: "C", Value: int64(3), NoIndex: true},
  909. Property{Name: "D", Value: int64(4), NoIndex: false},
  910. Property{Name: "E", Value: int64(5), NoIndex: false},
  911. Property{Name: "J", Value: int64(7), NoIndex: true},
  912. Property{Name: "a", Value: int64(1), NoIndex: true},
  913. Property{Name: "b", Value: []interface{}{int64(21), int64(22), int64(23)}, NoIndex: false},
  914. },
  915. "",
  916. "",
  917. },
  918. {
  919. "save tagged load tagged",
  920. &Tagged{A: 1, B: []int{21, 22, 23}, C: 3, D: 4, E: 5, I: 6, J: 7},
  921. &Tagged{A: 1, B: []int{21, 22, 23}, C: 3, D: 4, E: 5, J: 7},
  922. "",
  923. "",
  924. },
  925. {
  926. "invalid tagged1",
  927. &InvalidTagged1{I: 1},
  928. &InvalidTagged1{},
  929. "struct tag has invalid property name",
  930. "",
  931. },
  932. {
  933. "invalid tagged2",
  934. &InvalidTagged2{I: 1, J: 2},
  935. &InvalidTagged2{J: 2},
  936. "",
  937. "",
  938. },
  939. {
  940. "invalid tagged3",
  941. &InvalidTagged3{X: "hello"},
  942. &InvalidTagged3{},
  943. "struct tag has invalid property name: \"-\"",
  944. "",
  945. },
  946. {
  947. "invalid tagged4",
  948. &InvalidTagged4{X: "hello"},
  949. &InvalidTagged4{},
  950. "struct tag has invalid option: \"garbage\"",
  951. "",
  952. },
  953. {
  954. "doubler",
  955. &Doubler{S: "s", I: 1, B: true},
  956. &Doubler{S: "ss", I: 2, B: true},
  957. "",
  958. "",
  959. },
  960. {
  961. "save struct load props",
  962. &X0{S: "s", I: 1},
  963. &PropertyList{
  964. Property{Name: "I", Value: int64(1), NoIndex: false},
  965. Property{Name: "S", Value: "s", NoIndex: false},
  966. },
  967. "",
  968. "",
  969. },
  970. {
  971. "save props load struct",
  972. &PropertyList{
  973. Property{Name: "I", Value: int64(1), NoIndex: false},
  974. Property{Name: "S", Value: "s", NoIndex: false},
  975. },
  976. &X0{S: "s", I: 1},
  977. "",
  978. "",
  979. },
  980. {
  981. "nil-value props",
  982. &PropertyList{
  983. Property{Name: "I", Value: nil, NoIndex: false},
  984. Property{Name: "B", Value: nil, NoIndex: false},
  985. Property{Name: "S", Value: nil, NoIndex: false},
  986. Property{Name: "F", Value: nil, NoIndex: false},
  987. Property{Name: "K", Value: nil, NoIndex: false},
  988. Property{Name: "T", Value: nil, NoIndex: false},
  989. Property{Name: "J", Value: []interface{}{nil, int64(7), nil}, NoIndex: false},
  990. },
  991. &struct {
  992. I int64
  993. B bool
  994. S string
  995. F float64
  996. K *Key
  997. T time.Time
  998. J []int64
  999. }{
  1000. J: []int64{0, 7, 0},
  1001. },
  1002. "",
  1003. "",
  1004. },
  1005. {
  1006. "save outer load props flatten",
  1007. &OuterFlatten{
  1008. A: 1,
  1009. I: []Inner1{
  1010. {10, "ten"},
  1011. {20, "twenty"},
  1012. {30, "thirty"},
  1013. },
  1014. J: Inner2{
  1015. Y: 3.14,
  1016. },
  1017. Inner3: Inner3{
  1018. Z: true,
  1019. },
  1020. K: Inner4{
  1021. X: Inner5{
  1022. WW: 12,
  1023. },
  1024. },
  1025. },
  1026. &PropertyList{
  1027. Property{Name: "A", Value: int64(1), NoIndex: false},
  1028. Property{Name: "I.W", Value: []interface{}{int64(10), int64(20), int64(30)}, NoIndex: false},
  1029. Property{Name: "I.X", Value: []interface{}{"ten", "twenty", "thirty"}, NoIndex: false},
  1030. Property{Name: "J.Y", Value: float64(3.14), NoIndex: true},
  1031. Property{Name: "K.X.WW", Value: int64(12), NoIndex: false},
  1032. Property{Name: "Z", Value: true, NoIndex: false},
  1033. },
  1034. "",
  1035. "",
  1036. },
  1037. {
  1038. "load outer props flatten",
  1039. &PropertyList{
  1040. Property{Name: "A", Value: int64(1), NoIndex: false},
  1041. Property{Name: "I.W", Value: []interface{}{int64(10), int64(20), int64(30)}, NoIndex: false},
  1042. Property{Name: "I.X", Value: []interface{}{"ten", "twenty", "thirty"}, NoIndex: false},
  1043. Property{Name: "J.Y", Value: float64(3.14), NoIndex: true},
  1044. Property{Name: "Z", Value: true, NoIndex: false},
  1045. },
  1046. &OuterFlatten{
  1047. A: 1,
  1048. I: []Inner1{
  1049. {10, "ten"},
  1050. {20, "twenty"},
  1051. {30, "thirty"},
  1052. },
  1053. J: Inner2{
  1054. Y: 3.14,
  1055. },
  1056. Inner3: Inner3{
  1057. Z: true,
  1058. },
  1059. },
  1060. "",
  1061. "",
  1062. },
  1063. {
  1064. "save outer load props",
  1065. &Outer{
  1066. A: 1,
  1067. I: []Inner1{
  1068. {10, "ten"},
  1069. {20, "twenty"},
  1070. {30, "thirty"},
  1071. },
  1072. J: Inner2{
  1073. Y: 3.14,
  1074. },
  1075. Inner3: Inner3{
  1076. Z: true,
  1077. },
  1078. },
  1079. &PropertyList{
  1080. Property{Name: "A", Value: int64(1), NoIndex: false},
  1081. Property{Name: "I", Value: []interface{}{
  1082. &Entity{
  1083. Properties: []Property{
  1084. {Name: "W", Value: int64(10), NoIndex: false},
  1085. {Name: "X", Value: "ten", NoIndex: false},
  1086. },
  1087. },
  1088. &Entity{
  1089. Properties: []Property{
  1090. {Name: "W", Value: int64(20), NoIndex: false},
  1091. {Name: "X", Value: "twenty", NoIndex: false},
  1092. },
  1093. },
  1094. &Entity{
  1095. Properties: []Property{
  1096. {Name: "W", Value: int64(30), NoIndex: false},
  1097. {Name: "X", Value: "thirty", NoIndex: false},
  1098. },
  1099. },
  1100. }, NoIndex: false},
  1101. Property{Name: "J", Value: &Entity{
  1102. Properties: []Property{
  1103. {Name: "Y", Value: float64(3.14), NoIndex: false},
  1104. },
  1105. }, NoIndex: false},
  1106. Property{Name: "Z", Value: true, NoIndex: false},
  1107. },
  1108. "",
  1109. "",
  1110. },
  1111. {
  1112. "save props load outer-equivalent",
  1113. &PropertyList{
  1114. Property{Name: "A", Value: int64(1), NoIndex: false},
  1115. Property{Name: "I.W", Value: []interface{}{int64(10), int64(20), int64(30)}, NoIndex: false},
  1116. Property{Name: "I.X", Value: []interface{}{"ten", "twenty", "thirty"}, NoIndex: false},
  1117. Property{Name: "J.Y", Value: float64(3.14), NoIndex: false},
  1118. Property{Name: "Z", Value: true, NoIndex: false},
  1119. },
  1120. &OuterEquivalent{
  1121. A: 1,
  1122. IDotW: []int32{10, 20, 30},
  1123. IDotX: []string{"ten", "twenty", "thirty"},
  1124. JDotY: 3.14,
  1125. Z: true,
  1126. },
  1127. "",
  1128. "",
  1129. },
  1130. {
  1131. "dotted names save",
  1132. &Dotted{A: DottedA{B: DottedB{C: 88}}},
  1133. &PropertyList{
  1134. Property{Name: "A0.A1.A2", Value: &Entity{
  1135. Properties: []Property{
  1136. {Name: "B3", Value: &Entity{
  1137. Properties: []Property{
  1138. {Name: "C4.C5", Value: int64(88), NoIndex: false},
  1139. },
  1140. }, NoIndex: false},
  1141. },
  1142. }, NoIndex: false},
  1143. },
  1144. "",
  1145. "",
  1146. },
  1147. {
  1148. "dotted names load",
  1149. &PropertyList{
  1150. Property{Name: "A0.A1.A2", Value: &Entity{
  1151. Properties: []Property{
  1152. {Name: "B3", Value: &Entity{
  1153. Properties: []Property{
  1154. {Name: "C4.C5", Value: 99, NoIndex: false},
  1155. },
  1156. }, NoIndex: false},
  1157. },
  1158. }, NoIndex: false},
  1159. },
  1160. &Dotted{A: DottedA{B: DottedB{C: 99}}},
  1161. "",
  1162. "",
  1163. },
  1164. {
  1165. "save struct load deriver",
  1166. &X0{S: "s", I: 1},
  1167. &Deriver{S: "s", Derived: "derived+s"},
  1168. "",
  1169. "",
  1170. },
  1171. {
  1172. "save deriver load struct",
  1173. &Deriver{S: "s", Derived: "derived+s", Ignored: "ignored"},
  1174. &X0{S: "s"},
  1175. "",
  1176. "",
  1177. },
  1178. {
  1179. "zero time.Time",
  1180. &T{T: time.Time{}},
  1181. &T{T: time.Time{}},
  1182. "",
  1183. "",
  1184. },
  1185. {
  1186. "time.Time near Unix zero time",
  1187. &T{T: time.Unix(0, 4e3)},
  1188. &T{T: time.Unix(0, 4e3)},
  1189. "",
  1190. "",
  1191. },
  1192. {
  1193. "time.Time, far in the future",
  1194. &T{T: time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC)},
  1195. &T{T: time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC)},
  1196. "",
  1197. "",
  1198. },
  1199. {
  1200. "time.Time, very far in the past",
  1201. &T{T: time.Date(-300000, 1, 1, 0, 0, 0, 0, time.UTC)},
  1202. &T{},
  1203. "time value out of range",
  1204. "",
  1205. },
  1206. {
  1207. "time.Time, very far in the future",
  1208. &T{T: time.Date(294248, 1, 1, 0, 0, 0, 0, time.UTC)},
  1209. &T{},
  1210. "time value out of range",
  1211. "",
  1212. },
  1213. {
  1214. "structs",
  1215. &N0{
  1216. X0: X0{S: "one", I: 2, i: 3},
  1217. Nonymous: X0{S: "four", I: 5, i: 6},
  1218. Ignore: "ignore",
  1219. Other: "other",
  1220. },
  1221. &N0{
  1222. X0: X0{S: "one", I: 2},
  1223. Nonymous: X0{S: "four", I: 5},
  1224. Other: "other",
  1225. },
  1226. "",
  1227. "",
  1228. },
  1229. {
  1230. "slice of structs",
  1231. &N1{
  1232. X0: X0{S: "one", I: 2, i: 3},
  1233. Nonymous: []X0{
  1234. {S: "four", I: 5, i: 6},
  1235. {S: "seven", I: 8, i: 9},
  1236. {S: "ten", I: 11, i: 12},
  1237. {S: "thirteen", I: 14, i: 15},
  1238. },
  1239. Ignore: "ignore",
  1240. Other: "other",
  1241. },
  1242. &N1{
  1243. X0: X0{S: "one", I: 2},
  1244. Nonymous: []X0{
  1245. {S: "four", I: 5},
  1246. {S: "seven", I: 8},
  1247. {S: "ten", I: 11},
  1248. {S: "thirteen", I: 14},
  1249. },
  1250. Other: "other",
  1251. },
  1252. "",
  1253. "",
  1254. },
  1255. {
  1256. "structs with slices of structs",
  1257. &N2{
  1258. N1: N1{
  1259. X0: X0{S: "rouge"},
  1260. Nonymous: []X0{
  1261. {S: "rosso0"},
  1262. {S: "rosso1"},
  1263. },
  1264. },
  1265. Green: N1{
  1266. X0: X0{S: "vert"},
  1267. Nonymous: []X0{
  1268. {S: "verde0"},
  1269. {S: "verde1"},
  1270. {S: "verde2"},
  1271. },
  1272. },
  1273. Blue: N1{
  1274. X0: X0{S: "bleu"},
  1275. Nonymous: []X0{
  1276. {S: "blu0"},
  1277. {S: "blu1"},
  1278. {S: "blu2"},
  1279. {S: "blu3"},
  1280. },
  1281. },
  1282. },
  1283. &N2{
  1284. N1: N1{
  1285. X0: X0{S: "rouge"},
  1286. Nonymous: []X0{
  1287. {S: "rosso0"},
  1288. {S: "rosso1"},
  1289. },
  1290. },
  1291. Green: N1{
  1292. X0: X0{S: "vert"},
  1293. Nonymous: []X0{
  1294. {S: "verde0"},
  1295. {S: "verde1"},
  1296. {S: "verde2"},
  1297. },
  1298. },
  1299. Blue: N1{
  1300. X0: X0{S: "bleu"},
  1301. Nonymous: []X0{
  1302. {S: "blu0"},
  1303. {S: "blu1"},
  1304. {S: "blu2"},
  1305. {S: "blu3"},
  1306. },
  1307. },
  1308. },
  1309. "",
  1310. "",
  1311. },
  1312. {
  1313. "save structs load props",
  1314. &N2{
  1315. N1: N1{
  1316. X0: X0{S: "rouge"},
  1317. Nonymous: []X0{
  1318. {S: "rosso0"},
  1319. {S: "rosso1"},
  1320. },
  1321. },
  1322. Green: N1{
  1323. X0: X0{S: "vert"},
  1324. Nonymous: []X0{
  1325. {S: "verde0"},
  1326. {S: "verde1"},
  1327. {S: "verde2"},
  1328. },
  1329. },
  1330. Blue: N1{
  1331. X0: X0{S: "bleu"},
  1332. Nonymous: []X0{
  1333. {S: "blu0"},
  1334. {S: "blu1"},
  1335. {S: "blu2"},
  1336. {S: "blu3"},
  1337. },
  1338. },
  1339. },
  1340. &PropertyList{
  1341. Property{Name: "Blue", Value: &Entity{
  1342. Properties: []Property{
  1343. {Name: "I", Value: int64(0), NoIndex: false},
  1344. {Name: "Nonymous", Value: []interface{}{
  1345. &Entity{
  1346. Properties: []Property{
  1347. {Name: "I", Value: int64(0), NoIndex: false},
  1348. {Name: "S", Value: "blu0", NoIndex: false},
  1349. },
  1350. },
  1351. &Entity{
  1352. Properties: []Property{
  1353. {Name: "I", Value: int64(0), NoIndex: false},
  1354. {Name: "S", Value: "blu1", NoIndex: false},
  1355. },
  1356. },
  1357. &Entity{
  1358. Properties: []Property{
  1359. {Name: "I", Value: int64(0), NoIndex: false},
  1360. {Name: "S", Value: "blu2", NoIndex: false},
  1361. },
  1362. },
  1363. &Entity{
  1364. Properties: []Property{
  1365. {Name: "I", Value: int64(0), NoIndex: false},
  1366. {Name: "S", Value: "blu3", NoIndex: false},
  1367. },
  1368. },
  1369. }, NoIndex: false},
  1370. {Name: "Other", Value: "", NoIndex: false},
  1371. {Name: "S", Value: "bleu", NoIndex: false},
  1372. },
  1373. }, NoIndex: false},
  1374. Property{Name: "green", Value: &Entity{
  1375. Properties: []Property{
  1376. {Name: "I", Value: int64(0), NoIndex: false},
  1377. {Name: "Nonymous", Value: []interface{}{
  1378. &Entity{
  1379. Properties: []Property{
  1380. {Name: "I", Value: int64(0), NoIndex: false},
  1381. {Name: "S", Value: "verde0", NoIndex: false},
  1382. },
  1383. },
  1384. &Entity{
  1385. Properties: []Property{
  1386. {Name: "I", Value: int64(0), NoIndex: false},
  1387. {Name: "S", Value: "verde1", NoIndex: false},
  1388. },
  1389. },
  1390. &Entity{
  1391. Properties: []Property{
  1392. {Name: "I", Value: int64(0), NoIndex: false},
  1393. {Name: "S", Value: "verde2", NoIndex: false},
  1394. },
  1395. },
  1396. }, NoIndex: false},
  1397. {Name: "Other", Value: "", NoIndex: false},
  1398. {Name: "S", Value: "vert", NoIndex: false},
  1399. },
  1400. }, NoIndex: false},
  1401. Property{Name: "red", Value: &Entity{
  1402. Properties: []Property{
  1403. {Name: "I", Value: int64(0), NoIndex: false},
  1404. {Name: "Nonymous", Value: []interface{}{
  1405. &Entity{
  1406. Properties: []Property{
  1407. {Name: "I", Value: int64(0), NoIndex: false},
  1408. {Name: "S", Value: "rosso0", NoIndex: false},
  1409. },
  1410. },
  1411. &Entity{
  1412. Properties: []Property{
  1413. {Name: "I", Value: int64(0), NoIndex: false},
  1414. {Name: "S", Value: "rosso1", NoIndex: false},
  1415. },
  1416. },
  1417. }, NoIndex: false},
  1418. {Name: "Other", Value: "", NoIndex: false},
  1419. {Name: "S", Value: "rouge", NoIndex: false},
  1420. },
  1421. }, NoIndex: false},
  1422. },
  1423. "",
  1424. "",
  1425. },
  1426. {
  1427. "nested entity with key",
  1428. &WithNestedEntityWithKey{
  1429. N: EntityWithKey{
  1430. I: 12,
  1431. S: "abcd",
  1432. K: testKey0,
  1433. },
  1434. },
  1435. &WithNestedEntityWithKey{
  1436. N: EntityWithKey{
  1437. I: 12,
  1438. S: "abcd",
  1439. K: testKey0,
  1440. },
  1441. },
  1442. "",
  1443. "",
  1444. },
  1445. {
  1446. "entity with key at top level",
  1447. &EntityWithKey{
  1448. I: 12,
  1449. S: "abc",
  1450. K: testKey0,
  1451. },
  1452. &EntityWithKey{
  1453. I: 12,
  1454. S: "abc",
  1455. K: testKey0,
  1456. },
  1457. "",
  1458. "",
  1459. },
  1460. {
  1461. "entity with key at top level (key is populated on load)",
  1462. &EntityWithKey{
  1463. I: 12,
  1464. S: "abc",
  1465. },
  1466. &EntityWithKey{
  1467. I: 12,
  1468. S: "abc",
  1469. K: testKey0,
  1470. },
  1471. "",
  1472. "",
  1473. },
  1474. {
  1475. "__key__ field not a *Key",
  1476. &NestedWithNonKeyField{
  1477. N: WithNonKeyField{
  1478. I: 12,
  1479. K: "abcd",
  1480. },
  1481. },
  1482. &NestedWithNonKeyField{
  1483. N: WithNonKeyField{
  1484. I: 12,
  1485. K: "abcd",
  1486. },
  1487. },
  1488. "datastore: __key__ field on struct datastore.WithNonKeyField is not a *datastore.Key",
  1489. "",
  1490. },
  1491. {
  1492. "save struct with ptr to struct fields",
  1493. &PtrToStructField{
  1494. &Basic{
  1495. A: "b",
  1496. },
  1497. &Basic{
  1498. A: "c",
  1499. },
  1500. &Basic{
  1501. A: "anon",
  1502. },
  1503. []*Basic{
  1504. {
  1505. A: "slice0",
  1506. },
  1507. {
  1508. A: "slice1",
  1509. },
  1510. },
  1511. },
  1512. &PropertyList{
  1513. Property{Name: "A", Value: "anon", NoIndex: false},
  1514. Property{Name: "B", Value: &Entity{
  1515. Properties: []Property{
  1516. {Name: "A", Value: "b", NoIndex: false},
  1517. },
  1518. }},
  1519. Property{Name: "D", Value: []interface{}{
  1520. &Entity{
  1521. Properties: []Property{
  1522. {Name: "A", Value: "slice0", NoIndex: false},
  1523. },
  1524. },
  1525. &Entity{
  1526. Properties: []Property{
  1527. {Name: "A", Value: "slice1", NoIndex: false},
  1528. },
  1529. },
  1530. }, NoIndex: false},
  1531. Property{Name: "c", Value: &Entity{
  1532. Properties: []Property{
  1533. {Name: "A", Value: "c", NoIndex: true},
  1534. },
  1535. }, NoIndex: true},
  1536. },
  1537. "",
  1538. "",
  1539. },
  1540. {
  1541. "save and load struct with ptr to struct fields",
  1542. &PtrToStructField{
  1543. &Basic{
  1544. A: "b",
  1545. },
  1546. &Basic{
  1547. A: "c",
  1548. },
  1549. &Basic{
  1550. A: "anon",
  1551. },
  1552. []*Basic{
  1553. {
  1554. A: "slice0",
  1555. },
  1556. {
  1557. A: "slice1",
  1558. },
  1559. },
  1560. },
  1561. &PtrToStructField{
  1562. &Basic{
  1563. A: "b",
  1564. },
  1565. &Basic{
  1566. A: "c",
  1567. },
  1568. &Basic{
  1569. A: "anon",
  1570. },
  1571. []*Basic{
  1572. {
  1573. A: "slice0",
  1574. },
  1575. {
  1576. A: "slice1",
  1577. },
  1578. },
  1579. },
  1580. "",
  1581. "",
  1582. },
  1583. {
  1584. "struct with nil ptr to struct fields",
  1585. &PtrToStructField{
  1586. nil,
  1587. nil,
  1588. nil,
  1589. nil,
  1590. },
  1591. new(PropertyList),
  1592. "",
  1593. "",
  1594. },
  1595. {
  1596. "nested load entity with key",
  1597. &WithNestedEntityWithKey{
  1598. N: EntityWithKey{
  1599. I: 12,
  1600. S: "abcd",
  1601. K: testKey0,
  1602. },
  1603. },
  1604. &PropertyList{
  1605. Property{Name: "N", Value: &Entity{
  1606. Key: testKey0,
  1607. Properties: []Property{
  1608. {Name: "I", Value: int64(12), NoIndex: false},
  1609. {Name: "S", Value: "abcd", NoIndex: false},
  1610. },
  1611. },
  1612. NoIndex: false},
  1613. },
  1614. "",
  1615. "",
  1616. },
  1617. {
  1618. "nested save entity with key",
  1619. &PropertyList{
  1620. Property{Name: "N", Value: &Entity{
  1621. Key: testKey0,
  1622. Properties: []Property{
  1623. {Name: "I", Value: int64(12), NoIndex: false},
  1624. {Name: "S", Value: "abcd", NoIndex: false},
  1625. },
  1626. }, NoIndex: false},
  1627. },
  1628. &WithNestedEntityWithKey{
  1629. N: EntityWithKey{
  1630. I: 12,
  1631. S: "abcd",
  1632. K: testKey0,
  1633. },
  1634. },
  1635. "",
  1636. "",
  1637. },
  1638. {
  1639. "anonymous field with tag",
  1640. &N3{
  1641. C3: C3{C: "s"},
  1642. },
  1643. &PropertyList{
  1644. Property{Name: "red", Value: &Entity{
  1645. Properties: []Property{
  1646. {Name: "C", Value: "s", NoIndex: false},
  1647. },
  1648. }, NoIndex: false},
  1649. },
  1650. "",
  1651. "",
  1652. },
  1653. {
  1654. "unexported anonymous field",
  1655. &N4{
  1656. c4: c4{C: "s"},
  1657. },
  1658. &PropertyList{
  1659. Property{Name: "C", Value: "s", NoIndex: false},
  1660. },
  1661. "",
  1662. "",
  1663. },
  1664. {
  1665. "unexported anonymous field with tag",
  1666. &N5{
  1667. c4: c4{C: "s"},
  1668. },
  1669. new(PropertyList),
  1670. "",
  1671. "",
  1672. },
  1673. {
  1674. "save props load structs with ragged fields",
  1675. &PropertyList{
  1676. Property{Name: "red.S", Value: "rot", NoIndex: false},
  1677. Property{Name: "green.Nonymous.I", Value: []interface{}{int64(10), int64(11), int64(12), int64(13)}, NoIndex: false},
  1678. Property{Name: "Blue.Nonymous.I", Value: []interface{}{int64(20), int64(21)}, NoIndex: false},
  1679. Property{Name: "Blue.Nonymous.S", Value: []interface{}{"blau0", "blau1", "blau2"}, NoIndex: false},
  1680. },
  1681. &N2{
  1682. N1: N1{
  1683. X0: X0{S: "rot"},
  1684. },
  1685. Green: N1{
  1686. Nonymous: []X0{
  1687. {I: 10},
  1688. {I: 11},
  1689. {I: 12},
  1690. {I: 13},
  1691. },
  1692. },
  1693. Blue: N1{
  1694. Nonymous: []X0{
  1695. {S: "blau0", I: 20},
  1696. {S: "blau1", I: 21},
  1697. {S: "blau2"},
  1698. },
  1699. },
  1700. },
  1701. "",
  1702. "",
  1703. },
  1704. {
  1705. "save structs with noindex tags",
  1706. &struct {
  1707. A struct {
  1708. X string `datastore:",noindex"`
  1709. Y string
  1710. } `datastore:",noindex"`
  1711. B struct {
  1712. X string `datastore:",noindex"`
  1713. Y string
  1714. }
  1715. }{},
  1716. &PropertyList{
  1717. Property{Name: "A", Value: &Entity{
  1718. Properties: []Property{
  1719. {Name: "X", Value: "", NoIndex: true},
  1720. {Name: "Y", Value: "", NoIndex: true},
  1721. },
  1722. }, NoIndex: true},
  1723. Property{Name: "B", Value: &Entity{
  1724. Properties: []Property{
  1725. {Name: "X", Value: "", NoIndex: true},
  1726. {Name: "Y", Value: "", NoIndex: false},
  1727. },
  1728. }, NoIndex: false},
  1729. },
  1730. "",
  1731. "",
  1732. },
  1733. {
  1734. "embedded struct with name override",
  1735. &struct {
  1736. Inner1 `datastore:"foo"`
  1737. }{},
  1738. &PropertyList{
  1739. Property{Name: "foo", Value: &Entity{
  1740. Properties: []Property{
  1741. {Name: "W", Value: int64(0), NoIndex: false},
  1742. {Name: "X", Value: "", NoIndex: false},
  1743. },
  1744. }, NoIndex: false},
  1745. },
  1746. "",
  1747. "",
  1748. },
  1749. {
  1750. "slice of slices",
  1751. &SliceOfSlices{},
  1752. nil,
  1753. "flattening nested structs leads to a slice of slices",
  1754. "",
  1755. },
  1756. {
  1757. "recursive struct",
  1758. &Recursive{},
  1759. &Recursive{},
  1760. "",
  1761. "",
  1762. },
  1763. {
  1764. "mutually recursive struct",
  1765. &MutuallyRecursive0{},
  1766. &MutuallyRecursive0{},
  1767. "",
  1768. "",
  1769. },
  1770. {
  1771. "non-exported struct fields",
  1772. &struct {
  1773. i, J int64
  1774. }{i: 1, J: 2},
  1775. &PropertyList{
  1776. Property{Name: "J", Value: int64(2), NoIndex: false},
  1777. },
  1778. "",
  1779. "",
  1780. },
  1781. {
  1782. "json.RawMessage",
  1783. &struct {
  1784. J json.RawMessage
  1785. }{
  1786. J: json.RawMessage("rawr"),
  1787. },
  1788. &PropertyList{
  1789. Property{Name: "J", Value: []byte("rawr"), NoIndex: false},
  1790. },
  1791. "",
  1792. "",
  1793. },
  1794. {
  1795. "json.RawMessage to myBlob",
  1796. &struct {
  1797. B json.RawMessage
  1798. }{
  1799. B: json.RawMessage("rawr"),
  1800. },
  1801. &B2{B: myBlob("rawr")},
  1802. "",
  1803. "",
  1804. },
  1805. {
  1806. "repeated property names",
  1807. &PropertyList{
  1808. Property{Name: "A", Value: ""},
  1809. Property{Name: "A", Value: ""},
  1810. },
  1811. nil,
  1812. "duplicate Property",
  1813. "",
  1814. },
  1815. {
  1816. "embedded time field",
  1817. &SpecialTime{MyTime: EmbeddedTime{ts}},
  1818. &SpecialTime{MyTime: EmbeddedTime{ts}},
  1819. "",
  1820. "",
  1821. },
  1822. {
  1823. "embedded time load",
  1824. &PropertyList{
  1825. Property{Name: "MyTime.Time", Value: ts},
  1826. },
  1827. &SpecialTime{MyTime: EmbeddedTime{ts}},
  1828. "",
  1829. "",
  1830. },
  1831. {
  1832. "pointer fields: nil",
  1833. &Pointers{},
  1834. &Pointers{},
  1835. "",
  1836. "",
  1837. },
  1838. {
  1839. "pointer fields: populated with zeroes",
  1840. populatedPointers(),
  1841. populatedPointers(),
  1842. "",
  1843. "",
  1844. },
  1845. }
  1846. // checkErr returns the empty string if either both want and err are zero,
  1847. // or if want is a non-empty substring of err's string representation.
  1848. func checkErr(want string, err error) string {
  1849. if err != nil {
  1850. got := err.Error()
  1851. if want == "" || strings.Index(got, want) == -1 {
  1852. return got
  1853. }
  1854. } else if want != "" {
  1855. return fmt.Sprintf("want error %q", want)
  1856. }
  1857. return ""
  1858. }
  1859. func TestRoundTrip(t *testing.T) {
  1860. for _, tc := range testCases {
  1861. p, err := saveEntity(testKey0, tc.src)
  1862. if s := checkErr(tc.putErr, err); s != "" {
  1863. t.Errorf("%s: save: %s", tc.desc, s)
  1864. continue
  1865. }
  1866. if p == nil {
  1867. continue
  1868. }
  1869. var got interface{}
  1870. if _, ok := tc.want.(*PropertyList); ok {
  1871. got = new(PropertyList)
  1872. } else {
  1873. got = reflect.New(reflect.TypeOf(tc.want).Elem()).Interface()
  1874. }
  1875. err = loadEntityProto(got, p)
  1876. if s := checkErr(tc.getErr, err); s != "" {
  1877. t.Errorf("%s: load: %s", tc.desc, s)
  1878. continue
  1879. }
  1880. if pl, ok := got.(*PropertyList); ok {
  1881. // Sort by name to make sure we have a deterministic order.
  1882. sortPL(*pl)
  1883. }
  1884. if !testutil.Equal(got, tc.want, cmp.AllowUnexported(X0{}, X2{})) {
  1885. t.Errorf("%s: compare:\ngot: %+#v\nwant: %+#v", tc.desc, got, tc.want)
  1886. continue
  1887. }
  1888. }
  1889. }
  1890. type aPtrPLS struct {
  1891. Count int
  1892. }
  1893. func (pls *aPtrPLS) Load([]Property) error {
  1894. pls.Count += 1
  1895. return nil
  1896. }
  1897. func (pls *aPtrPLS) Save() ([]Property, error) {
  1898. return []Property{{Name: "Count", Value: 4}}, nil
  1899. }
  1900. type aValuePLS struct {
  1901. Count int
  1902. }
  1903. func (pls aValuePLS) Load([]Property) error {
  1904. pls.Count += 2
  1905. return nil
  1906. }
  1907. func (pls aValuePLS) Save() ([]Property, error) {
  1908. return []Property{{Name: "Count", Value: 8}}, nil
  1909. }
  1910. type aValuePtrPLS struct {
  1911. Count int
  1912. }
  1913. func (pls *aValuePtrPLS) Load([]Property) error {
  1914. pls.Count = 11
  1915. return nil
  1916. }
  1917. func (pls *aValuePtrPLS) Save() ([]Property, error) {
  1918. return []Property{{Name: "Count", Value: 12}}, nil
  1919. }
  1920. type aNotPLS struct {
  1921. Count int
  1922. }
  1923. type plsString string
  1924. func (s *plsString) Load([]Property) error {
  1925. *s = "LOADED"
  1926. return nil
  1927. }
  1928. func (s *plsString) Save() ([]Property, error) {
  1929. return []Property{{Name: "SS", Value: "SAVED"}}, nil
  1930. }
  1931. func ptrToplsString(s string) *plsString {
  1932. plsStr := plsString(s)
  1933. return &plsStr
  1934. }
  1935. type aSubPLS struct {
  1936. Foo string
  1937. Bar *aPtrPLS
  1938. Baz aValuePtrPLS
  1939. S plsString
  1940. }
  1941. type aSubNotPLS struct {
  1942. Foo string
  1943. Bar *aNotPLS
  1944. }
  1945. type aSubPLSErr struct {
  1946. Foo string
  1947. Bar aValuePLS
  1948. }
  1949. type aSubPLSNoErr struct {
  1950. Foo string
  1951. Bar aPtrPLS
  1952. }
  1953. type GrandparentFlatten struct {
  1954. Parent Parent `datastore:",flatten"`
  1955. }
  1956. type GrandparentOfPtrFlatten struct {
  1957. Parent ParentOfPtr `datastore:",flatten"`
  1958. }
  1959. type GrandparentOfSlice struct {
  1960. Parent ParentOfSlice
  1961. }
  1962. type GrandparentOfSlicePtrs struct {
  1963. Parent ParentOfSlicePtrs
  1964. }
  1965. type GrandparentOfSliceFlatten struct {
  1966. Parent ParentOfSlice `datastore:",flatten"`
  1967. }
  1968. type GrandparentOfSlicePtrsFlatten struct {
  1969. Parent ParentOfSlicePtrs `datastore:",flatten"`
  1970. }
  1971. type Grandparent struct {
  1972. Parent Parent
  1973. }
  1974. type Parent struct {
  1975. Child Child
  1976. String plsString
  1977. }
  1978. type ParentOfPtr struct {
  1979. Child *Child
  1980. String *plsString
  1981. }
  1982. type ParentOfSlice struct {
  1983. Children []Child
  1984. Strings []plsString
  1985. }
  1986. type ParentOfSlicePtrs struct {
  1987. Children []*Child
  1988. Strings []*plsString
  1989. }
  1990. type Child struct {
  1991. I int
  1992. Grandchild Grandchild
  1993. }
  1994. type Grandchild struct {
  1995. S string
  1996. }
  1997. func (c *Child) Load(props []Property) error {
  1998. for _, p := range props {
  1999. if p.Name == "I" {
  2000. c.I += 1
  2001. } else if p.Name == "Grandchild.S" {
  2002. c.Grandchild.S = "grandchild loaded"
  2003. }
  2004. }
  2005. return nil
  2006. }
  2007. func (c *Child) Save() ([]Property, error) {
  2008. v := c.I + 1
  2009. return []Property{
  2010. {Name: "I", Value: v},
  2011. {Name: "Grandchild.S", Value: fmt.Sprintf("grandchild saved %d", v)},
  2012. }, nil
  2013. }
  2014. func TestLoadSavePLS(t *testing.T) {
  2015. type testCase struct {
  2016. desc string
  2017. src interface{}
  2018. wantSave *pb.Entity
  2019. wantLoad interface{}
  2020. saveErr string
  2021. loadErr string
  2022. }
  2023. testCases := []testCase{
  2024. {
  2025. desc: "non-struct implements PLS (top-level)",
  2026. src: ptrToplsString("hello"),
  2027. wantSave: &pb.Entity{
  2028. Key: keyToProto(testKey0),
  2029. Properties: map[string]*pb.Value{
  2030. "SS": {ValueType: &pb.Value_StringValue{StringValue: "SAVED"}},
  2031. },
  2032. },
  2033. wantLoad: ptrToplsString("LOADED"),
  2034. },
  2035. {
  2036. desc: "substructs do implement PLS",
  2037. src: &aSubPLS{Foo: "foo", Bar: &aPtrPLS{Count: 2}, Baz: aValuePtrPLS{Count: 15}, S: "something"},
  2038. wantSave: &pb.Entity{
  2039. Key: keyToProto(testKey0),
  2040. Properties: map[string]*pb.Value{
  2041. "Foo": {ValueType: &pb.Value_StringValue{StringValue: "foo"}},
  2042. "Bar": {ValueType: &pb.Value_EntityValue{
  2043. EntityValue: &pb.Entity{
  2044. Properties: map[string]*pb.Value{
  2045. "Count": {ValueType: &pb.Value_IntegerValue{IntegerValue: 4}},
  2046. },
  2047. },
  2048. }},
  2049. "Baz": {ValueType: &pb.Value_EntityValue{
  2050. EntityValue: &pb.Entity{
  2051. Properties: map[string]*pb.Value{
  2052. "Count": {ValueType: &pb.Value_IntegerValue{IntegerValue: 12}},
  2053. },
  2054. },
  2055. }},
  2056. "S": {ValueType: &pb.Value_EntityValue{
  2057. EntityValue: &pb.Entity{
  2058. Properties: map[string]*pb.Value{
  2059. "SS": {ValueType: &pb.Value_StringValue{StringValue: "SAVED"}},
  2060. },
  2061. },
  2062. }},
  2063. },
  2064. },
  2065. wantLoad: &aSubPLS{Foo: "foo", Bar: &aPtrPLS{Count: 1}, Baz: aValuePtrPLS{Count: 11}, S: "LOADED"},
  2066. },
  2067. {
  2068. desc: "substruct (ptr) does implement PLS, nil valued substruct",
  2069. src: &aSubPLS{Foo: "foo", S: "something"},
  2070. wantSave: &pb.Entity{
  2071. Key: keyToProto(testKey0),
  2072. Properties: map[string]*pb.Value{
  2073. "Foo": {ValueType: &pb.Value_StringValue{StringValue: "foo"}},
  2074. "Baz": {ValueType: &pb.Value_EntityValue{
  2075. EntityValue: &pb.Entity{
  2076. Properties: map[string]*pb.Value{
  2077. "Count": {ValueType: &pb.Value_IntegerValue{IntegerValue: 12}},
  2078. },
  2079. },
  2080. }},
  2081. "S": {ValueType: &pb.Value_EntityValue{
  2082. EntityValue: &pb.Entity{
  2083. Properties: map[string]*pb.Value{
  2084. "SS": {ValueType: &pb.Value_StringValue{StringValue: "SAVED"}},
  2085. },
  2086. },
  2087. }},
  2088. },
  2089. },
  2090. wantLoad: &aSubPLS{Foo: "foo", Baz: aValuePtrPLS{Count: 11}, S: "LOADED"},
  2091. },
  2092. {
  2093. desc: "substruct (ptr) does not implement PLS",
  2094. src: &aSubNotPLS{Foo: "foo", Bar: &aNotPLS{Count: 2}},
  2095. wantSave: &pb.Entity{
  2096. Key: keyToProto(testKey0),
  2097. Properties: map[string]*pb.Value{
  2098. "Foo": {ValueType: &pb.Value_StringValue{StringValue: "foo"}},
  2099. "Bar": {ValueType: &pb.Value_EntityValue{
  2100. EntityValue: &pb.Entity{
  2101. Properties: map[string]*pb.Value{
  2102. "Count": {ValueType: &pb.Value_IntegerValue{IntegerValue: 2}},
  2103. },
  2104. },
  2105. }},
  2106. },
  2107. },
  2108. wantLoad: &aSubNotPLS{Foo: "foo", Bar: &aNotPLS{Count: 2}},
  2109. },
  2110. {
  2111. desc: "substruct (value) does implement PLS, error on save",
  2112. src: &aSubPLSErr{Foo: "foo", Bar: aValuePLS{Count: 2}},
  2113. wantSave: (*pb.Entity)(nil),
  2114. wantLoad: &aSubPLSErr{},
  2115. saveErr: "PropertyLoadSaver methods must be implemented on a pointer",
  2116. },
  2117. {
  2118. desc: "substruct (value) does implement PLS, error on load",
  2119. src: &aSubPLSNoErr{Foo: "foo", Bar: aPtrPLS{Count: 2}},
  2120. wantSave: &pb.Entity{
  2121. Key: keyToProto(testKey0),
  2122. Properties: map[string]*pb.Value{
  2123. "Foo": {ValueType: &pb.Value_StringValue{StringValue: "foo"}},
  2124. "Bar": {ValueType: &pb.Value_EntityValue{
  2125. EntityValue: &pb.Entity{
  2126. Properties: map[string]*pb.Value{
  2127. "Count": {ValueType: &pb.Value_IntegerValue{IntegerValue: 4}},
  2128. },
  2129. },
  2130. }},
  2131. },
  2132. },
  2133. wantLoad: &aSubPLSErr{},
  2134. loadErr: "PropertyLoadSaver methods must be implemented on a pointer",
  2135. },
  2136. {
  2137. desc: "parent does not have flatten option, child impl PLS",
  2138. src: &Grandparent{
  2139. Parent: Parent{
  2140. Child: Child{
  2141. I: 9,
  2142. Grandchild: Grandchild{
  2143. S: "BAD",
  2144. },
  2145. },
  2146. String: plsString("something"),
  2147. },
  2148. },
  2149. wantSave: &pb.Entity{
  2150. Key: keyToProto(testKey0),
  2151. Properties: map[string]*pb.Value{
  2152. "Parent": {ValueType: &pb.Value_EntityValue{
  2153. EntityValue: &pb.Entity{
  2154. Properties: map[string]*pb.Value{
  2155. "Child": {ValueType: &pb.Value_EntityValue{
  2156. EntityValue: &pb.Entity{
  2157. Properties: map[string]*pb.Value{
  2158. "I": {ValueType: &pb.Value_IntegerValue{IntegerValue: 10}},
  2159. "Grandchild.S": {ValueType: &pb.Value_StringValue{StringValue: "grandchild saved 10"}},
  2160. },
  2161. },
  2162. }},
  2163. "String": {ValueType: &pb.Value_EntityValue{
  2164. EntityValue: &pb.Entity{
  2165. Properties: map[string]*pb.Value{
  2166. "SS": {ValueType: &pb.Value_StringValue{StringValue: "SAVED"}},
  2167. },
  2168. },
  2169. }},
  2170. },
  2171. },
  2172. }},
  2173. },
  2174. },
  2175. wantLoad: &Grandparent{
  2176. Parent: Parent{
  2177. Child: Child{
  2178. I: 1,
  2179. Grandchild: Grandchild{
  2180. S: "grandchild loaded",
  2181. },
  2182. },
  2183. String: "LOADED",
  2184. },
  2185. },
  2186. },
  2187. {
  2188. desc: "parent has flatten option enabled, child impl PLS",
  2189. src: &GrandparentFlatten{
  2190. Parent: Parent{
  2191. Child: Child{
  2192. I: 7,
  2193. Grandchild: Grandchild{
  2194. S: "BAD",
  2195. },
  2196. },
  2197. String: plsString("something"),
  2198. },
  2199. },
  2200. wantSave: &pb.Entity{
  2201. Key: keyToProto(testKey0),
  2202. Properties: map[string]*pb.Value{
  2203. "Parent.Child.I": {ValueType: &pb.Value_IntegerValue{IntegerValue: 8}},
  2204. "Parent.Child.Grandchild.S": {ValueType: &pb.Value_StringValue{StringValue: "grandchild saved 8"}},
  2205. "Parent.String.SS": {ValueType: &pb.Value_StringValue{StringValue: "SAVED"}},
  2206. },
  2207. },
  2208. wantLoad: &GrandparentFlatten{
  2209. Parent: Parent{
  2210. Child: Child{
  2211. I: 1,
  2212. Grandchild: Grandchild{
  2213. S: "grandchild loaded",
  2214. },
  2215. },
  2216. String: "LOADED",
  2217. },
  2218. },
  2219. },
  2220. {
  2221. desc: "parent has flatten option enabled, child (ptr to) impl PLS",
  2222. src: &GrandparentOfPtrFlatten{
  2223. Parent: ParentOfPtr{
  2224. Child: &Child{
  2225. I: 7,
  2226. Grandchild: Grandchild{
  2227. S: "BAD",
  2228. },
  2229. },
  2230. String: ptrToplsString("something"),
  2231. },
  2232. },
  2233. wantSave: &pb.Entity{
  2234. Key: keyToProto(testKey0),
  2235. Properties: map[string]*pb.Value{
  2236. "Parent.Child.I": {ValueType: &pb.Value_IntegerValue{IntegerValue: 8}},
  2237. "Parent.Child.Grandchild.S": {ValueType: &pb.Value_StringValue{StringValue: "grandchild saved 8"}},
  2238. "Parent.String.SS": {ValueType: &pb.Value_StringValue{StringValue: "SAVED"}},
  2239. },
  2240. },
  2241. wantLoad: &GrandparentOfPtrFlatten{
  2242. Parent: ParentOfPtr{
  2243. Child: &Child{
  2244. I: 1,
  2245. Grandchild: Grandchild{
  2246. S: "grandchild loaded",
  2247. },
  2248. },
  2249. String: ptrToplsString("LOADED"),
  2250. },
  2251. },
  2252. },
  2253. {
  2254. desc: "children (slice of) impl PLS",
  2255. src: &GrandparentOfSlice{
  2256. Parent: ParentOfSlice{
  2257. Children: []Child{
  2258. {
  2259. I: 7,
  2260. Grandchild: Grandchild{
  2261. S: "BAD",
  2262. },
  2263. },
  2264. {
  2265. I: 9,
  2266. Grandchild: Grandchild{
  2267. S: "BAD2",
  2268. },
  2269. },
  2270. },
  2271. Strings: []plsString{
  2272. "something1",
  2273. "something2",
  2274. },
  2275. },
  2276. },
  2277. wantSave: &pb.Entity{
  2278. Key: keyToProto(testKey0),
  2279. Properties: map[string]*pb.Value{
  2280. "Parent": {ValueType: &pb.Value_EntityValue{
  2281. EntityValue: &pb.Entity{
  2282. Properties: map[string]*pb.Value{
  2283. "Children": {ValueType: &pb.Value_ArrayValue{
  2284. ArrayValue: &pb.ArrayValue{Values: []*pb.Value{
  2285. {ValueType: &pb.Value_EntityValue{
  2286. EntityValue: &pb.Entity{
  2287. Properties: map[string]*pb.Value{
  2288. "I": {ValueType: &pb.Value_IntegerValue{IntegerValue: 8}},
  2289. "Grandchild.S": {ValueType: &pb.Value_StringValue{StringValue: "grandchild saved 8"}},
  2290. },
  2291. },
  2292. }},
  2293. {ValueType: &pb.Value_EntityValue{
  2294. EntityValue: &pb.Entity{
  2295. Properties: map[string]*pb.Value{
  2296. "I": {ValueType: &pb.Value_IntegerValue{IntegerValue: 10}},
  2297. "Grandchild.S": {ValueType: &pb.Value_StringValue{StringValue: "grandchild saved 10"}},
  2298. },
  2299. },
  2300. }},
  2301. }},
  2302. }},
  2303. "Strings": {ValueType: &pb.Value_ArrayValue{
  2304. ArrayValue: &pb.ArrayValue{Values: []*pb.Value{
  2305. {ValueType: &pb.Value_EntityValue{
  2306. EntityValue: &pb.Entity{
  2307. Properties: map[string]*pb.Value{
  2308. "SS": {ValueType: &pb.Value_StringValue{StringValue: "SAVED"}},
  2309. },
  2310. },
  2311. }},
  2312. {ValueType: &pb.Value_EntityValue{
  2313. EntityValue: &pb.Entity{
  2314. Properties: map[string]*pb.Value{
  2315. "SS": {ValueType: &pb.Value_StringValue{StringValue: "SAVED"}},
  2316. },
  2317. },
  2318. }},
  2319. }},
  2320. }},
  2321. },
  2322. },
  2323. }},
  2324. },
  2325. },
  2326. wantLoad: &GrandparentOfSlice{
  2327. Parent: ParentOfSlice{
  2328. Children: []Child{
  2329. {
  2330. I: 1,
  2331. Grandchild: Grandchild{
  2332. S: "grandchild loaded",
  2333. },
  2334. },
  2335. {
  2336. I: 1,
  2337. Grandchild: Grandchild{
  2338. S: "grandchild loaded",
  2339. },
  2340. },
  2341. },
  2342. Strings: []plsString{
  2343. "LOADED",
  2344. "LOADED",
  2345. },
  2346. },
  2347. },
  2348. },
  2349. {
  2350. desc: "children (slice of ptrs) impl PLS",
  2351. src: &GrandparentOfSlicePtrs{
  2352. Parent: ParentOfSlicePtrs{
  2353. Children: []*Child{
  2354. {
  2355. I: 7,
  2356. Grandchild: Grandchild{
  2357. S: "BAD",
  2358. },
  2359. },
  2360. {
  2361. I: 9,
  2362. Grandchild: Grandchild{
  2363. S: "BAD2",
  2364. },
  2365. },
  2366. },
  2367. Strings: []*plsString{
  2368. ptrToplsString("something1"),
  2369. ptrToplsString("something2"),
  2370. },
  2371. },
  2372. },
  2373. wantSave: &pb.Entity{
  2374. Key: keyToProto(testKey0),
  2375. Properties: map[string]*pb.Value{
  2376. "Parent": {ValueType: &pb.Value_EntityValue{
  2377. EntityValue: &pb.Entity{
  2378. Properties: map[string]*pb.Value{
  2379. "Children": {ValueType: &pb.Value_ArrayValue{
  2380. ArrayValue: &pb.ArrayValue{Values: []*pb.Value{
  2381. {ValueType: &pb.Value_EntityValue{
  2382. EntityValue: &pb.Entity{
  2383. Properties: map[string]*pb.Value{
  2384. "I": {ValueType: &pb.Value_IntegerValue{IntegerValue: 8}},
  2385. "Grandchild.S": {ValueType: &pb.Value_StringValue{StringValue: "grandchild saved 8"}},
  2386. },
  2387. },
  2388. }},
  2389. {ValueType: &pb.Value_EntityValue{
  2390. EntityValue: &pb.Entity{
  2391. Properties: map[string]*pb.Value{
  2392. "I": {ValueType: &pb.Value_IntegerValue{IntegerValue: 10}},
  2393. "Grandchild.S": {ValueType: &pb.Value_StringValue{StringValue: "grandchild saved 10"}},
  2394. },
  2395. },
  2396. }},
  2397. }},
  2398. }},
  2399. "Strings": {ValueType: &pb.Value_ArrayValue{
  2400. ArrayValue: &pb.ArrayValue{Values: []*pb.Value{
  2401. {ValueType: &pb.Value_EntityValue{
  2402. EntityValue: &pb.Entity{
  2403. Properties: map[string]*pb.Value{
  2404. "SS": {ValueType: &pb.Value_StringValue{StringValue: "SAVED"}},
  2405. },
  2406. },
  2407. }},
  2408. {ValueType: &pb.Value_EntityValue{
  2409. EntityValue: &pb.Entity{
  2410. Properties: map[string]*pb.Value{
  2411. "SS": {ValueType: &pb.Value_StringValue{StringValue: "SAVED"}},
  2412. },
  2413. },
  2414. }},
  2415. }},
  2416. }},
  2417. },
  2418. },
  2419. }},
  2420. },
  2421. },
  2422. wantLoad: &GrandparentOfSlicePtrs{
  2423. Parent: ParentOfSlicePtrs{
  2424. Children: []*Child{
  2425. {
  2426. I: 1,
  2427. Grandchild: Grandchild{
  2428. S: "grandchild loaded",
  2429. },
  2430. },
  2431. {
  2432. I: 1,
  2433. Grandchild: Grandchild{
  2434. S: "grandchild loaded",
  2435. },
  2436. },
  2437. },
  2438. Strings: []*plsString{
  2439. ptrToplsString("LOADED"),
  2440. ptrToplsString("LOADED"),
  2441. },
  2442. },
  2443. },
  2444. },
  2445. {
  2446. desc: "parent has flatten option, children (slice of) impl PLS",
  2447. src: &GrandparentOfSliceFlatten{
  2448. Parent: ParentOfSlice{
  2449. Children: []Child{
  2450. {
  2451. I: 7,
  2452. Grandchild: Grandchild{
  2453. S: "BAD",
  2454. },
  2455. },
  2456. {
  2457. I: 9,
  2458. Grandchild: Grandchild{
  2459. S: "BAD2",
  2460. },
  2461. },
  2462. },
  2463. Strings: []plsString{
  2464. "something1",
  2465. "something2",
  2466. },
  2467. },
  2468. },
  2469. wantSave: &pb.Entity{
  2470. Key: keyToProto(testKey0),
  2471. Properties: map[string]*pb.Value{
  2472. "Parent.Children.I": {ValueType: &pb.Value_ArrayValue{ArrayValue: &pb.ArrayValue{
  2473. Values: []*pb.Value{
  2474. {ValueType: &pb.Value_IntegerValue{IntegerValue: 8}},
  2475. {ValueType: &pb.Value_IntegerValue{IntegerValue: 10}},
  2476. },
  2477. },
  2478. }},
  2479. "Parent.Children.Grandchild.S": {ValueType: &pb.Value_ArrayValue{ArrayValue: &pb.ArrayValue{
  2480. Values: []*pb.Value{
  2481. {ValueType: &pb.Value_StringValue{StringValue: "grandchild saved 8"}},
  2482. {ValueType: &pb.Value_StringValue{StringValue: "grandchild saved 10"}},
  2483. },
  2484. },
  2485. }},
  2486. "Parent.Strings.SS": {ValueType: &pb.Value_ArrayValue{ArrayValue: &pb.ArrayValue{
  2487. Values: []*pb.Value{
  2488. {ValueType: &pb.Value_StringValue{StringValue: "SAVED"}},
  2489. {ValueType: &pb.Value_StringValue{StringValue: "SAVED"}},
  2490. },
  2491. },
  2492. }},
  2493. },
  2494. },
  2495. wantLoad: &GrandparentOfSliceFlatten{
  2496. Parent: ParentOfSlice{
  2497. Children: []Child{
  2498. {
  2499. I: 1,
  2500. Grandchild: Grandchild{
  2501. S: "grandchild loaded",
  2502. },
  2503. },
  2504. {
  2505. I: 1,
  2506. Grandchild: Grandchild{
  2507. S: "grandchild loaded",
  2508. },
  2509. },
  2510. },
  2511. Strings: []plsString{
  2512. "LOADED",
  2513. "LOADED",
  2514. },
  2515. },
  2516. },
  2517. },
  2518. {
  2519. desc: "parent has flatten option, children (slice of ptrs) impl PLS",
  2520. src: &GrandparentOfSlicePtrsFlatten{
  2521. Parent: ParentOfSlicePtrs{
  2522. Children: []*Child{
  2523. {
  2524. I: 7,
  2525. Grandchild: Grandchild{
  2526. S: "BAD",
  2527. },
  2528. },
  2529. {
  2530. I: 9,
  2531. Grandchild: Grandchild{
  2532. S: "BAD2",
  2533. },
  2534. },
  2535. },
  2536. Strings: []*plsString{
  2537. ptrToplsString("something1"),
  2538. ptrToplsString("something1"),
  2539. },
  2540. },
  2541. },
  2542. wantSave: &pb.Entity{
  2543. Key: keyToProto(testKey0),
  2544. Properties: map[string]*pb.Value{
  2545. "Parent.Children.I": {ValueType: &pb.Value_ArrayValue{ArrayValue: &pb.ArrayValue{
  2546. Values: []*pb.Value{
  2547. {ValueType: &pb.Value_IntegerValue{IntegerValue: 8}},
  2548. {ValueType: &pb.Value_IntegerValue{IntegerValue: 10}},
  2549. },
  2550. },
  2551. }},
  2552. "Parent.Children.Grandchild.S": {ValueType: &pb.Value_ArrayValue{ArrayValue: &pb.ArrayValue{
  2553. Values: []*pb.Value{
  2554. {ValueType: &pb.Value_StringValue{StringValue: "grandchild saved 8"}},
  2555. {ValueType: &pb.Value_StringValue{StringValue: "grandchild saved 10"}},
  2556. },
  2557. },
  2558. }},
  2559. "Parent.Strings.SS": {ValueType: &pb.Value_ArrayValue{ArrayValue: &pb.ArrayValue{
  2560. Values: []*pb.Value{
  2561. {ValueType: &pb.Value_StringValue{StringValue: "SAVED"}},
  2562. {ValueType: &pb.Value_StringValue{StringValue: "SAVED"}},
  2563. },
  2564. },
  2565. }},
  2566. },
  2567. },
  2568. wantLoad: &GrandparentOfSlicePtrsFlatten{
  2569. Parent: ParentOfSlicePtrs{
  2570. Children: []*Child{
  2571. {
  2572. I: 1,
  2573. Grandchild: Grandchild{
  2574. S: "grandchild loaded",
  2575. },
  2576. },
  2577. {
  2578. I: 1,
  2579. Grandchild: Grandchild{
  2580. S: "grandchild loaded",
  2581. },
  2582. },
  2583. },
  2584. Strings: []*plsString{
  2585. ptrToplsString("LOADED"),
  2586. ptrToplsString("LOADED"),
  2587. },
  2588. },
  2589. },
  2590. },
  2591. }
  2592. for _, tc := range testCases {
  2593. e, err := saveEntity(testKey0, tc.src)
  2594. if tc.saveErr == "" { // Want no error.
  2595. if err != nil {
  2596. t.Errorf("%s: save: %v", tc.desc, err)
  2597. continue
  2598. }
  2599. if !testutil.Equal(e, tc.wantSave) {
  2600. t.Errorf("%s: save: \ngot: %+v\nwant: %+v", tc.desc, e, tc.wantSave)
  2601. continue
  2602. }
  2603. } else { // Want error.
  2604. if err == nil {
  2605. t.Errorf("%s: save: want err", tc.desc)
  2606. continue
  2607. }
  2608. if !strings.Contains(err.Error(), tc.saveErr) {
  2609. t.Errorf("%s: save: \ngot err '%s'\nwant err '%s'", tc.desc, err.Error(), tc.saveErr)
  2610. }
  2611. continue
  2612. }
  2613. gota := reflect.New(reflect.TypeOf(tc.wantLoad).Elem()).Interface()
  2614. err = loadEntityProto(gota, e)
  2615. if tc.loadErr == "" { // Want no error.
  2616. if err != nil {
  2617. t.Errorf("%s: load: %v", tc.desc, err)
  2618. continue
  2619. }
  2620. if !testutil.Equal(gota, tc.wantLoad) {
  2621. t.Errorf("%s: load: \ngot: %+v\nwant: %+v", tc.desc, gota, tc.wantLoad)
  2622. continue
  2623. }
  2624. } else { // Want error.
  2625. if err == nil {
  2626. t.Errorf("%s: load: want err", tc.desc)
  2627. continue
  2628. }
  2629. if !strings.Contains(err.Error(), tc.loadErr) {
  2630. t.Errorf("%s: load: \ngot err '%s'\nwant err '%s'", tc.desc, err.Error(), tc.loadErr)
  2631. }
  2632. }
  2633. }
  2634. }
  2635. func TestQueryConstruction(t *testing.T) {
  2636. tests := []struct {
  2637. q, exp *Query
  2638. err string
  2639. }{
  2640. {
  2641. q: NewQuery("Foo"),
  2642. exp: &Query{
  2643. kind: "Foo",
  2644. limit: -1,
  2645. },
  2646. },
  2647. {
  2648. // Regular filtered query with standard spacing.
  2649. q: NewQuery("Foo").Filter("foo >", 7),
  2650. exp: &Query{
  2651. kind: "Foo",
  2652. filter: []filter{
  2653. {
  2654. FieldName: "foo",
  2655. Op: greaterThan,
  2656. Value: 7,
  2657. },
  2658. },
  2659. limit: -1,
  2660. },
  2661. },
  2662. {
  2663. // Filtered query with no spacing.
  2664. q: NewQuery("Foo").Filter("foo=", 6),
  2665. exp: &Query{
  2666. kind: "Foo",
  2667. filter: []filter{
  2668. {
  2669. FieldName: "foo",
  2670. Op: equal,
  2671. Value: 6,
  2672. },
  2673. },
  2674. limit: -1,
  2675. },
  2676. },
  2677. {
  2678. // Filtered query with funky spacing.
  2679. q: NewQuery("Foo").Filter(" foo< ", 8),
  2680. exp: &Query{
  2681. kind: "Foo",
  2682. filter: []filter{
  2683. {
  2684. FieldName: "foo",
  2685. Op: lessThan,
  2686. Value: 8,
  2687. },
  2688. },
  2689. limit: -1,
  2690. },
  2691. },
  2692. {
  2693. // Filtered query with multicharacter op.
  2694. q: NewQuery("Foo").Filter("foo >=", 9),
  2695. exp: &Query{
  2696. kind: "Foo",
  2697. filter: []filter{
  2698. {
  2699. FieldName: "foo",
  2700. Op: greaterEq,
  2701. Value: 9,
  2702. },
  2703. },
  2704. limit: -1,
  2705. },
  2706. },
  2707. {
  2708. // Query with ordering.
  2709. q: NewQuery("Foo").Order("bar"),
  2710. exp: &Query{
  2711. kind: "Foo",
  2712. order: []order{
  2713. {
  2714. FieldName: "bar",
  2715. Direction: ascending,
  2716. },
  2717. },
  2718. limit: -1,
  2719. },
  2720. },
  2721. {
  2722. // Query with reverse ordering, and funky spacing.
  2723. q: NewQuery("Foo").Order(" - bar"),
  2724. exp: &Query{
  2725. kind: "Foo",
  2726. order: []order{
  2727. {
  2728. FieldName: "bar",
  2729. Direction: descending,
  2730. },
  2731. },
  2732. limit: -1,
  2733. },
  2734. },
  2735. {
  2736. // Query with an empty ordering.
  2737. q: NewQuery("Foo").Order(""),
  2738. err: "empty order",
  2739. },
  2740. {
  2741. // Query with a + ordering.
  2742. q: NewQuery("Foo").Order("+bar"),
  2743. err: "invalid order",
  2744. },
  2745. }
  2746. for i, test := range tests {
  2747. if test.q.err != nil {
  2748. got := test.q.err.Error()
  2749. if !strings.Contains(got, test.err) {
  2750. t.Errorf("%d: error mismatch: got %q want something containing %q", i, got, test.err)
  2751. }
  2752. continue
  2753. }
  2754. if !testutil.Equal(test.q, test.exp, cmp.AllowUnexported(Query{})) {
  2755. t.Errorf("%d: mismatch: got %v want %v", i, test.q, test.exp)
  2756. }
  2757. }
  2758. }
  2759. func TestPutMultiTypes(t *testing.T) {
  2760. ctx := context.Background()
  2761. type S struct {
  2762. A int
  2763. B string
  2764. }
  2765. testCases := []struct {
  2766. desc string
  2767. src interface{}
  2768. wantErr bool
  2769. }{
  2770. // Test cases to check each of the valid input types for src.
  2771. // Each case has the same elements.
  2772. {
  2773. desc: "type []struct",
  2774. src: []S{
  2775. {1, "one"}, {2, "two"},
  2776. },
  2777. },
  2778. {
  2779. desc: "type []*struct",
  2780. src: []*S{
  2781. {1, "one"}, {2, "two"},
  2782. },
  2783. },
  2784. {
  2785. desc: "type []interface{} with PLS elems",
  2786. src: []interface{}{
  2787. &PropertyList{Property{Name: "A", Value: 1}, Property{Name: "B", Value: "one"}},
  2788. &PropertyList{Property{Name: "A", Value: 2}, Property{Name: "B", Value: "two"}},
  2789. },
  2790. },
  2791. {
  2792. desc: "type []interface{} with struct ptr elems",
  2793. src: []interface{}{
  2794. &S{1, "one"}, &S{2, "two"},
  2795. },
  2796. },
  2797. {
  2798. desc: "type []PropertyLoadSaver{}",
  2799. src: []PropertyLoadSaver{
  2800. &PropertyList{Property{Name: "A", Value: 1}, Property{Name: "B", Value: "one"}},
  2801. &PropertyList{Property{Name: "A", Value: 2}, Property{Name: "B", Value: "two"}},
  2802. },
  2803. },
  2804. {
  2805. desc: "type []P (non-pointer, *P implements PropertyLoadSaver)",
  2806. src: []PropertyList{
  2807. {Property{Name: "A", Value: 1}, Property{Name: "B", Value: "one"}},
  2808. {Property{Name: "A", Value: 2}, Property{Name: "B", Value: "two"}},
  2809. },
  2810. },
  2811. // Test some invalid cases.
  2812. {
  2813. desc: "type []interface{} with struct elems",
  2814. src: []interface{}{
  2815. S{1, "one"}, S{2, "two"},
  2816. },
  2817. wantErr: true,
  2818. },
  2819. {
  2820. desc: "PropertyList",
  2821. src: PropertyList{
  2822. Property{Name: "A", Value: 1},
  2823. Property{Name: "B", Value: "one"},
  2824. },
  2825. wantErr: true,
  2826. },
  2827. {
  2828. desc: "type []int",
  2829. src: []int{1, 2},
  2830. wantErr: true,
  2831. },
  2832. {
  2833. desc: "not a slice",
  2834. src: S{1, "one"},
  2835. wantErr: true,
  2836. },
  2837. }
  2838. // Use the same keys and expected entities for all tests.
  2839. keys := []*Key{
  2840. NameKey("testKind", "first", nil),
  2841. NameKey("testKind", "second", nil),
  2842. }
  2843. want := []*pb.Mutation{
  2844. {Operation: &pb.Mutation_Upsert{
  2845. Upsert: &pb.Entity{
  2846. Key: keyToProto(keys[0]),
  2847. Properties: map[string]*pb.Value{
  2848. "A": {ValueType: &pb.Value_IntegerValue{IntegerValue: 1}},
  2849. "B": {ValueType: &pb.Value_StringValue{StringValue: "one"}},
  2850. },
  2851. }}},
  2852. {Operation: &pb.Mutation_Upsert{
  2853. Upsert: &pb.Entity{
  2854. Key: keyToProto(keys[1]),
  2855. Properties: map[string]*pb.Value{
  2856. "A": {ValueType: &pb.Value_IntegerValue{IntegerValue: 2}},
  2857. "B": {ValueType: &pb.Value_StringValue{StringValue: "two"}},
  2858. },
  2859. }}},
  2860. }
  2861. for _, tt := range testCases {
  2862. // Set up a fake client which captures upserts.
  2863. var got []*pb.Mutation
  2864. client := &Client{
  2865. client: &fakeClient{
  2866. commitFn: func(req *pb.CommitRequest) (*pb.CommitResponse, error) {
  2867. got = req.Mutations
  2868. return &pb.CommitResponse{}, nil
  2869. },
  2870. },
  2871. }
  2872. _, err := client.PutMulti(ctx, keys, tt.src)
  2873. if err != nil {
  2874. if !tt.wantErr {
  2875. t.Errorf("%s: error %v", tt.desc, err)
  2876. }
  2877. continue
  2878. }
  2879. if tt.wantErr {
  2880. t.Errorf("%s: wanted error, but none returned", tt.desc)
  2881. continue
  2882. }
  2883. if len(got) != len(want) {
  2884. t.Errorf("%s: got %d entities, want %d", tt.desc, len(got), len(want))
  2885. continue
  2886. }
  2887. for i, e := range got {
  2888. if !proto.Equal(e, want[i]) {
  2889. t.Logf("%s: entity %d doesn't match\ngot: %v\nwant: %v", tt.desc, i, e, want[i])
  2890. }
  2891. }
  2892. }
  2893. }
  2894. func TestNoIndexOnSliceProperties(t *testing.T) {
  2895. // Check that ExcludeFromIndexes is set on the inner elements,
  2896. // rather than the top-level ArrayValue value.
  2897. pl := PropertyList{
  2898. Property{
  2899. Name: "repeated",
  2900. Value: []interface{}{
  2901. 123,
  2902. false,
  2903. "short",
  2904. strings.Repeat("a", 1503),
  2905. },
  2906. NoIndex: true,
  2907. },
  2908. }
  2909. key := NameKey("dummy", "dummy", nil)
  2910. entity, err := saveEntity(key, &pl)
  2911. if err != nil {
  2912. t.Fatalf("saveEntity: %v", err)
  2913. }
  2914. want := &pb.Value{
  2915. ValueType: &pb.Value_ArrayValue{ArrayValue: &pb.ArrayValue{Values: []*pb.Value{
  2916. {ValueType: &pb.Value_IntegerValue{IntegerValue: 123}, ExcludeFromIndexes: true},
  2917. {ValueType: &pb.Value_BooleanValue{BooleanValue: false}, ExcludeFromIndexes: true},
  2918. {ValueType: &pb.Value_StringValue{StringValue: "short"}, ExcludeFromIndexes: true},
  2919. {ValueType: &pb.Value_StringValue{StringValue: strings.Repeat("a", 1503)}, ExcludeFromIndexes: true},
  2920. }}},
  2921. }
  2922. if got := entity.Properties["repeated"]; !proto.Equal(got, want) {
  2923. t.Errorf("Entity proto differs\ngot: %v\nwant: %v", got, want)
  2924. }
  2925. }
  2926. type byName PropertyList
  2927. func (s byName) Len() int { return len(s) }
  2928. func (s byName) Less(i, j int) bool { return s[i].Name < s[j].Name }
  2929. func (s byName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  2930. // sortPL sorts the property list by property name, and
  2931. // recursively sorts any nested property lists, or nested slices of
  2932. // property lists.
  2933. func sortPL(pl PropertyList) {
  2934. sort.Stable(byName(pl))
  2935. for _, p := range pl {
  2936. switch p.Value.(type) {
  2937. case *Entity:
  2938. sortPL(p.Value.(*Entity).Properties)
  2939. case []interface{}:
  2940. for _, p2 := range p.Value.([]interface{}) {
  2941. if nent, ok := p2.(*Entity); ok {
  2942. sortPL(nent.Properties)
  2943. }
  2944. }
  2945. }
  2946. }
  2947. }
  2948. func TestValidGeoPoint(t *testing.T) {
  2949. testCases := []struct {
  2950. desc string
  2951. pt GeoPoint
  2952. want bool
  2953. }{
  2954. {
  2955. "valid",
  2956. GeoPoint{67.21, 13.37},
  2957. true,
  2958. },
  2959. {
  2960. "high lat",
  2961. GeoPoint{-90.01, 13.37},
  2962. false,
  2963. },
  2964. {
  2965. "low lat",
  2966. GeoPoint{90.01, 13.37},
  2967. false,
  2968. },
  2969. {
  2970. "high lng",
  2971. GeoPoint{67.21, 182},
  2972. false,
  2973. },
  2974. {
  2975. "low lng",
  2976. GeoPoint{67.21, -181},
  2977. false,
  2978. },
  2979. }
  2980. for _, tc := range testCases {
  2981. if got := tc.pt.Valid(); got != tc.want {
  2982. t.Errorf("%s: got %v, want %v", tc.desc, got, tc.want)
  2983. }
  2984. }
  2985. }
  2986. func TestPutInvalidEntity(t *testing.T) {
  2987. // Test that trying to put an invalid entity always returns the correct error
  2988. // type.
  2989. // Fake client that can pretend to start a transaction.
  2990. fakeClient := &fakeDatastoreClient{
  2991. beginTransaction: func(*pb.BeginTransactionRequest) (*pb.BeginTransactionResponse, error) {
  2992. return &pb.BeginTransactionResponse{
  2993. Transaction: []byte("deadbeef"),
  2994. }, nil
  2995. },
  2996. }
  2997. client := &Client{
  2998. client: fakeClient,
  2999. }
  3000. ctx := context.Background()
  3001. key := IncompleteKey("kind", nil)
  3002. _, err := client.Put(ctx, key, "invalid entity")
  3003. if err != ErrInvalidEntityType {
  3004. t.Errorf("client.Put returned err %v, want %v", err, ErrInvalidEntityType)
  3005. }
  3006. _, err = client.PutMulti(ctx, []*Key{key}, []interface{}{"invalid entity"})
  3007. if me, ok := err.(MultiError); !ok {
  3008. t.Errorf("client.PutMulti returned err %v, want MultiError type", err)
  3009. } else if len(me) != 1 || me[0] != ErrInvalidEntityType {
  3010. t.Errorf("client.PutMulti returned err %v, want MulitError{ErrInvalidEntityType}", err)
  3011. }
  3012. client.RunInTransaction(ctx, func(tx *Transaction) error {
  3013. _, err := tx.Put(key, "invalid entity")
  3014. if err != ErrInvalidEntityType {
  3015. t.Errorf("tx.Put returned err %v, want %v", err, ErrInvalidEntityType)
  3016. }
  3017. _, err = tx.PutMulti([]*Key{key}, []interface{}{"invalid entity"})
  3018. if me, ok := err.(MultiError); !ok {
  3019. t.Errorf("tx.PutMulti returned err %v, want MultiError type", err)
  3020. } else if len(me) != 1 || me[0] != ErrInvalidEntityType {
  3021. t.Errorf("tx.PutMulti returned err %v, want MulitError{ErrInvalidEntityType}", err)
  3022. }
  3023. return errors.New("bang!") // Return error: we don't actually want to commit.
  3024. })
  3025. }
  3026. func TestDeferred(t *testing.T) {
  3027. type Ent struct {
  3028. A int
  3029. B string
  3030. }
  3031. keys := []*Key{
  3032. NameKey("testKind", "first", nil),
  3033. NameKey("testKind", "second", nil),
  3034. }
  3035. entity1 := &pb.Entity{
  3036. Key: keyToProto(keys[0]),
  3037. Properties: map[string]*pb.Value{
  3038. "A": {ValueType: &pb.Value_IntegerValue{IntegerValue: 1}},
  3039. "B": {ValueType: &pb.Value_StringValue{StringValue: "one"}},
  3040. },
  3041. }
  3042. entity2 := &pb.Entity{
  3043. Key: keyToProto(keys[1]),
  3044. Properties: map[string]*pb.Value{
  3045. "A": {ValueType: &pb.Value_IntegerValue{IntegerValue: 2}},
  3046. "B": {ValueType: &pb.Value_StringValue{StringValue: "two"}},
  3047. },
  3048. }
  3049. // count keeps track of the number of times fakeClient.lookup has been
  3050. // called.
  3051. var count int
  3052. // Fake client that will return Deferred keys in resp on the first call.
  3053. fakeClient := &fakeDatastoreClient{
  3054. lookup: func(*pb.LookupRequest) (*pb.LookupResponse, error) {
  3055. count++
  3056. // On the first call, we return deferred keys.
  3057. if count == 1 {
  3058. return &pb.LookupResponse{
  3059. Found: []*pb.EntityResult{
  3060. {
  3061. Entity: entity1,
  3062. Version: 1,
  3063. },
  3064. },
  3065. Deferred: []*pb.Key{
  3066. keyToProto(keys[1]),
  3067. },
  3068. }, nil
  3069. }
  3070. // On the second call, we do not return any more deferred keys.
  3071. return &pb.LookupResponse{
  3072. Found: []*pb.EntityResult{
  3073. {
  3074. Entity: entity2,
  3075. Version: 1,
  3076. },
  3077. },
  3078. }, nil
  3079. },
  3080. }
  3081. client := &Client{
  3082. client: fakeClient,
  3083. }
  3084. ctx := context.Background()
  3085. dst := make([]Ent, len(keys))
  3086. err := client.GetMulti(ctx, keys, dst)
  3087. if err != nil {
  3088. t.Fatalf("client.Get: %v", err)
  3089. }
  3090. if count != 2 {
  3091. t.Fatalf("expected client.lookup to be called 2 times. Got %d", count)
  3092. }
  3093. if len(dst) != 2 {
  3094. t.Fatalf("expected 2 entities returned, got %d", len(dst))
  3095. }
  3096. for _, e := range dst {
  3097. if e.A == 1 {
  3098. if e.B != "one" {
  3099. t.Fatalf("unexpected entity %+v", e)
  3100. }
  3101. } else if e.A == 2 {
  3102. if e.B != "two" {
  3103. t.Fatalf("unexpected entity %+v", e)
  3104. }
  3105. } else {
  3106. t.Fatalf("unexpected entity %+v", e)
  3107. }
  3108. }
  3109. }
  3110. type KeyLoaderEnt struct {
  3111. A int
  3112. K *Key
  3113. }
  3114. func (e *KeyLoaderEnt) Load(p []Property) error {
  3115. e.A = 2
  3116. return nil
  3117. }
  3118. func (e *KeyLoaderEnt) LoadKey(k *Key) error {
  3119. e.K = k
  3120. return nil
  3121. }
  3122. func (e *KeyLoaderEnt) Save() ([]Property, error) {
  3123. return []Property{{Name: "A", Value: int64(3)}}, nil
  3124. }
  3125. func TestKeyLoaderEndToEnd(t *testing.T) {
  3126. keys := []*Key{
  3127. NameKey("testKind", "first", nil),
  3128. NameKey("testKind", "second", nil),
  3129. }
  3130. entity1 := &pb.Entity{
  3131. Key: keyToProto(keys[0]),
  3132. Properties: map[string]*pb.Value{
  3133. "A": {ValueType: &pb.Value_IntegerValue{IntegerValue: 1}},
  3134. "B": {ValueType: &pb.Value_StringValue{StringValue: "one"}},
  3135. },
  3136. }
  3137. entity2 := &pb.Entity{
  3138. Key: keyToProto(keys[1]),
  3139. Properties: map[string]*pb.Value{
  3140. "A": {ValueType: &pb.Value_IntegerValue{IntegerValue: 2}},
  3141. "B": {ValueType: &pb.Value_StringValue{StringValue: "two"}},
  3142. },
  3143. }
  3144. fakeClient := &fakeDatastoreClient{
  3145. lookup: func(*pb.LookupRequest) (*pb.LookupResponse, error) {
  3146. return &pb.LookupResponse{
  3147. Found: []*pb.EntityResult{
  3148. {
  3149. Entity: entity1,
  3150. Version: 1,
  3151. },
  3152. {
  3153. Entity: entity2,
  3154. Version: 1,
  3155. },
  3156. },
  3157. }, nil
  3158. },
  3159. }
  3160. client := &Client{
  3161. client: fakeClient,
  3162. }
  3163. ctx := context.Background()
  3164. dst := make([]*KeyLoaderEnt, len(keys))
  3165. err := client.GetMulti(ctx, keys, dst)
  3166. if err != nil {
  3167. t.Fatalf("client.Get: %v", err)
  3168. }
  3169. for i := range dst {
  3170. if !testutil.Equal(dst[i].K, keys[i]) {
  3171. t.Fatalf("unexpected entity %d to have key %+v, got %+v", i, keys[i], dst[i].K)
  3172. }
  3173. }
  3174. }
  3175. func TestDeferredMissing(t *testing.T) {
  3176. type Ent struct {
  3177. A int
  3178. B string
  3179. }
  3180. keys := []*Key{
  3181. NameKey("testKind", "first", nil),
  3182. NameKey("testKind", "second", nil),
  3183. }
  3184. entity1 := &pb.Entity{
  3185. Key: keyToProto(keys[0]),
  3186. }
  3187. entity2 := &pb.Entity{
  3188. Key: keyToProto(keys[1]),
  3189. }
  3190. var count int
  3191. fakeClient := &fakeDatastoreClient{
  3192. lookup: func(*pb.LookupRequest) (*pb.LookupResponse, error) {
  3193. count++
  3194. if count == 1 {
  3195. return &pb.LookupResponse{
  3196. Missing: []*pb.EntityResult{
  3197. {
  3198. Entity: entity1,
  3199. Version: 1,
  3200. },
  3201. },
  3202. Deferred: []*pb.Key{
  3203. keyToProto(keys[1]),
  3204. },
  3205. }, nil
  3206. }
  3207. return &pb.LookupResponse{
  3208. Missing: []*pb.EntityResult{
  3209. {
  3210. Entity: entity2,
  3211. Version: 1,
  3212. },
  3213. },
  3214. }, nil
  3215. },
  3216. }
  3217. client := &Client{
  3218. client: fakeClient,
  3219. }
  3220. ctx := context.Background()
  3221. dst := make([]Ent, len(keys))
  3222. err := client.GetMulti(ctx, keys, dst)
  3223. errs, ok := err.(MultiError)
  3224. if !ok {
  3225. t.Fatalf("expected error returns to be MultiError; got %v", err)
  3226. }
  3227. if len(errs) != 2 {
  3228. t.Fatalf("expected 2 errors returns, got %d", len(errs))
  3229. }
  3230. if errs[0] != ErrNoSuchEntity {
  3231. t.Fatalf("expected error to be ErrNoSuchEntity; got %v", errs[0])
  3232. }
  3233. if errs[1] != ErrNoSuchEntity {
  3234. t.Fatalf("expected error to be ErrNoSuchEntity; got %v", errs[1])
  3235. }
  3236. if count != 2 {
  3237. t.Fatalf("expected client.lookup to be called 2 times. Got %d", count)
  3238. }
  3239. if len(dst) != 2 {
  3240. t.Fatalf("expected 2 entities returned, got %d", len(dst))
  3241. }
  3242. for _, e := range dst {
  3243. if e.A != 0 || e.B != "" {
  3244. t.Fatalf("unexpected entity %+v", e)
  3245. }
  3246. }
  3247. }
  3248. type fakeDatastoreClient struct {
  3249. pb.DatastoreClient
  3250. // Optional handlers for the datastore methods.
  3251. // Any handlers left undefined will return an error.
  3252. lookup func(*pb.LookupRequest) (*pb.LookupResponse, error)
  3253. runQuery func(*pb.RunQueryRequest) (*pb.RunQueryResponse, error)
  3254. beginTransaction func(*pb.BeginTransactionRequest) (*pb.BeginTransactionResponse, error)
  3255. commit func(*pb.CommitRequest) (*pb.CommitResponse, error)
  3256. rollback func(*pb.RollbackRequest) (*pb.RollbackResponse, error)
  3257. allocateIds func(*pb.AllocateIdsRequest) (*pb.AllocateIdsResponse, error)
  3258. }
  3259. func (c *fakeDatastoreClient) Lookup(ctx context.Context, in *pb.LookupRequest, opts ...grpc.CallOption) (*pb.LookupResponse, error) {
  3260. if c.lookup == nil {
  3261. return nil, errors.New("no lookup handler defined")
  3262. }
  3263. return c.lookup(in)
  3264. }
  3265. func (c *fakeDatastoreClient) RunQuery(ctx context.Context, in *pb.RunQueryRequest, opts ...grpc.CallOption) (*pb.RunQueryResponse, error) {
  3266. if c.runQuery == nil {
  3267. return nil, errors.New("no runQuery handler defined")
  3268. }
  3269. return c.runQuery(in)
  3270. }
  3271. func (c *fakeDatastoreClient) BeginTransaction(ctx context.Context, in *pb.BeginTransactionRequest, opts ...grpc.CallOption) (*pb.BeginTransactionResponse, error) {
  3272. if c.beginTransaction == nil {
  3273. return nil, errors.New("no beginTransaction handler defined")
  3274. }
  3275. return c.beginTransaction(in)
  3276. }
  3277. func (c *fakeDatastoreClient) Commit(ctx context.Context, in *pb.CommitRequest, opts ...grpc.CallOption) (*pb.CommitResponse, error) {
  3278. if c.commit == nil {
  3279. return nil, errors.New("no commit handler defined")
  3280. }
  3281. return c.commit(in)
  3282. }
  3283. func (c *fakeDatastoreClient) Rollback(ctx context.Context, in *pb.RollbackRequest, opts ...grpc.CallOption) (*pb.RollbackResponse, error) {
  3284. if c.rollback == nil {
  3285. return nil, errors.New("no rollback handler defined")
  3286. }
  3287. return c.rollback(in)
  3288. }
  3289. func (c *fakeDatastoreClient) AllocateIds(ctx context.Context, in *pb.AllocateIdsRequest, opts ...grpc.CallOption) (*pb.AllocateIdsResponse, error) {
  3290. if c.allocateIds == nil {
  3291. return nil, errors.New("no allocateIds handler defined")
  3292. }
  3293. return c.allocateIds(in)
  3294. }