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

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

Issue 1414043006: Allow metadata fields to be PropertyConverters for symmetry. (Closed) Base URL: https://github.com/luci/gae.git@master
Patch Set: Created 5 years, 1 month 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
« service/datastore/pls_impl.go ('K') | « service/datastore/pls_impl.go ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 623 matching lines...) Expand 10 before | Expand all | Expand 10 after
634 if kind != "KindOverride" { 634 if kind != "KindOverride" {
635 i.customKind = kind 635 i.customKind = kind
636 } else { 636 } else {
637 i.customKind = "" 637 i.customKind = ""
638 } 638 }
639 return nil 639 return nil
640 } 640 }
641 return ErrMetaFieldUnset 641 return ErrMetaFieldUnset
642 } 642 }
643 643
644 type EmbeddedID struct {
645 Thing string
646 Val int
647 }
648
649 var _ PropertyConverter = (*EmbeddedID)(nil)
650
651 func (e *EmbeddedID) ToProperty() (ret Property, err error) {
652 return mpNI(fmt.Sprintf("%s|%d", e.Thing, e.Val)), nil
653 }
654
655 func (e *EmbeddedID) FromProperty(val Property) error {
656 if val.Type() != PTString {
657 return fmt.Errorf("gotta have a string")
658 }
659 toks := strings.SplitN(val.Value().(string), "|", 2)
660 if len(toks) != 2 {
661 return fmt.Errorf("gotta have two parts")
662 }
663 v, err := strconv.Atoi(toks[1])
664 if err != nil {
665 return err
666 }
667
668 e.Thing = toks[0]
669 e.Val = v
670 return nil
671 }
672
673 type IDEmbedder struct {
674 EmbeddedID `gae:"$id"`
675 }
676
644 type Simple struct{} 677 type Simple struct{}
645 678
646 type testCase struct { 679 type testCase struct {
647 desc string 680 desc string
648 src interface{} 681 src interface{}
649 want interface{} 682 want interface{}
650 plsErr string 683 plsErr string
651 saveErr string 684 saveErr string
652 plsLoadErr string 685 plsLoadErr string
653 loadErr string 686 loadErr string
(...skipping 1249 matching lines...) Expand 10 before | Expand all | Expand 10 after
1903 "$kind": {mpNI("wut")}, 1936 "$kind": {mpNI("wut")},
1904 }) 1937 })
1905 1938
1906 props, err := GetPLS(ko).Save(true) 1939 props, err := GetPLS(ko).Save(true)
1907 So(err, ShouldBeNil) 1940 So(err, ShouldBeNil)
1908 So(props, ShouldResemble, PropertyMap{ 1941 So(props, ShouldResemble, PropertyMap{
1909 "$id": {mpNI(20)}, 1942 "$id": {mpNI(20)},
1910 "$kind": {mpNI("wut")}, 1943 "$kind": {mpNI("wut")},
1911 }) 1944 })
1912 }) 1945 })
1946
1947 Convey("Embeddable Metadata structs", func() {
1948 ide := &IDEmbedder{EmbeddedID{"hello", 10}}
1949 pls := GetPLS(ide)
1950 val, err := pls.GetMeta("id")
1951 So(err, ShouldBeNil)
1952 So(val, ShouldEqual, "hello|10")
1953
1954 So(pls.SetMeta("id", "sup|1337"), ShouldBeNil)
1955 So(ide.EmbeddedID, ShouldResemble, EmbeddedID{"sup", 133 7})
1956
1957 So(pls.GetAllMeta(), ShouldResembleV, PropertyMap{
1958 "$id": {mpNI("sup|1337")},
1959 "$kind": {mpNI("IDEmbedder")},
1960 })
1961 })
1913 }) 1962 })
1914 } 1963 }
OLDNEW
« service/datastore/pls_impl.go ('K') | « service/datastore/pls_impl.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698