Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(260)

Side by Side Diff: service/datastore/pls_test.go

Issue 1401533007: Add ability to override just the metadata in a struct. (Closed) Base URL: https://github.com/luci/gae.git@add_exists_method
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // adapted from github.com/golang/appengine/datastore 5 // adapted from github.com/golang/appengine/datastore
6 6
7 package datastore 7 package datastore
8 8
9 import ( 9 import (
10 "bytes" 10 "bytes"
(...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after
530 } 530 }
531 531
532 type DerivedKey struct { 532 type DerivedKey struct {
533 K *Key 533 K *Key
534 } 534 }
535 535
536 type IfaceKey struct { 536 type IfaceKey struct {
537 K *Key 537 K *Key
538 } 538 }
539 539
540 type IDParser struct {
541 _kind string `gae:"$kind,CoolKind"`
542
543 // real $id is myParentName|myID
544 parent string `gae:"-"`
545 id int64 `gae:"-"`
546 }
547
548 var _ MetaGetterSetter = (*IDParser)(nil)
549
550 func (i *IDParser) getFullID() string {
551 return fmt.Sprintf("%s|%d", i.parent, i.id)
552 }
553
554 func (i *IDParser) GetAllMeta() PropertyMap {
555 pm := PropertyMap{}
556 pm.SetMeta("id", i.getFullID())
557 return pm
558 }
559
560 func (i *IDParser) GetMeta(key string) (interface{}, error) {
561 if key == "id" {
562 return i.getFullID(), nil
563 }
564 return nil, ErrMetaFieldUnset
565 }
566
567 func (i *IDParser) GetMetaDefault(key string, dflt interface{}) interface{} {
568 return GetMetaDefaultImpl(i.GetMeta, key, dflt)
569 }
570
571 func (i *IDParser) SetMeta(key string, value interface{}) (err error) {
572 if key == "id" {
573 // let the panics flooowwww
574 vS := strings.SplitN(value.(string), "|", 2)
575 i.parent = vS[0]
576 i.id, err = strconv.ParseInt(vS[1], 10, 64)
577 return
578 }
579 return ErrMetaFieldUnset
580 }
581
582 type KindOverride struct {
583 ID int64 `gae:"$id"`
584
585 customKind string `gae:"-"`
586 }
587
588 var _ MetaGetterSetter = (*KindOverride)(nil)
589
590 func (i *KindOverride) GetAllMeta() PropertyMap {
591 pm := PropertyMap{}
592 if i.customKind != "" {
593 pm.SetMeta("kind", i.customKind)
594 }
595 return pm
596 }
597
598 func (i *KindOverride) GetMeta(key string) (interface{}, error) {
599 if key == "kind" && i.customKind != "" {
600 return i.customKind, nil
601 }
602 return nil, ErrMetaFieldUnset
603 }
604
605 func (i *KindOverride) GetMetaDefault(key string, dflt interface{}) interface{} {
606 return GetMetaDefaultImpl(i.GetMeta, key, dflt)
607 }
608
609 func (i *KindOverride) SetMeta(key string, value interface{}) error {
610 if key == "kind" {
611 kind := value.(string)
612 if kind != "KindOverride" {
613 i.customKind = kind
614 } else {
615 i.customKind = ""
616 }
617 return nil
618 }
619 return ErrMetaFieldUnset
620 }
621
540 type testCase struct { 622 type testCase struct {
541 desc string 623 desc string
542 src interface{} 624 src interface{}
543 want interface{} 625 want interface{}
544 plsErr string 626 plsErr string
545 saveErr string 627 saveErr string
546 plsLoadErr string 628 plsLoadErr string
547 loadErr string 629 loadErr string
548 } 630 }
549 631
(...skipping 1161 matching lines...) Expand 10 before | Expand all | Expand 10 after
1711 So(v, ShouldEqual, int64(10)) 1793 So(v, ShouldEqual, int64(10))
1712 }) 1794 })
1713 1795
1714 Convey("Bad default meta type", func() { 1796 Convey("Bad default meta type", func() {
1715 type BadDefault struct { 1797 type BadDefault struct {
1716 Val time.Time `gae:"$meta,tomorrow"` 1798 Val time.Time `gae:"$meta,tomorrow"`
1717 } 1799 }
1718 pls := GetPLS(&BadDefault{}) 1800 pls := GetPLS(&BadDefault{})
1719 So(pls.Problem().Error(), ShouldContainSubstring, "bad t ype") 1801 So(pls.Problem().Error(), ShouldContainSubstring, "bad t ype")
1720 }) 1802 })
1803
1804 Convey("MetaGetterSetter implementation (IDParser)", func() {
1805 idp := &IDParser{parent: "moo", id: 100}
1806 pls := GetPLS(idp)
1807 So(pls.GetMetaDefault("id", ""), ShouldEqual, "moo|100")
1808 So(pls.GetMetaDefault("kind", ""), ShouldEqual, "CoolKin d")
1809
1810 So(pls.SetMeta("kind", "Something"), ShouldErrLike, "une xported field")
1811 So(pls.SetMeta("id", "happy|27"), ShouldBeNil)
1812
1813 So(idp.parent, ShouldEqual, "happy")
1814 So(idp.id, ShouldEqual, 27)
1815
1816 So(pls.GetAllMeta(), ShouldResemble, PropertyMap{
1817 "$id": {MkPropertyNI("happy|27")},
1818 "$kind": {MkPropertyNI("CoolKind")},
1819 })
1820 })
1821
1822 Convey("MetaGetterSetter implementation (KindOverride)", func() {
1823 ko := &KindOverride{ID: 20}
1824 pls := GetPLS(ko)
1825 So(pls.GetMetaDefault("kind", ""), ShouldEqual, "KindOve rride")
1826
1827 ko.customKind = "something"
1828 So(pls.GetMetaDefault("kind", ""), ShouldEqual, "somethi ng")
1829
1830 So(pls.SetMeta("kind", "Nerp"), ShouldBeNil)
1831 So(ko.customKind, ShouldEqual, "Nerp")
1832
1833 So(pls.SetMeta("kind", "KindOverride"), ShouldBeNil)
1834 So(ko.customKind, ShouldEqual, "")
1835
1836 So(pls.GetAllMeta(), ShouldResemble, PropertyMap{
1837 "$id": {MkPropertyNI(20)},
1838 "$kind": {MkPropertyNI("KindOverride")},
1839 })
1840 ko.customKind = "wut"
1841 So(pls.GetAllMeta(), ShouldResemble, PropertyMap{
1842 "$id": {MkPropertyNI(20)},
1843 "$kind": {MkPropertyNI("wut")},
1844 })
1845 })
1721 }) 1846 })
1722 } 1847 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698