OLD | NEW |
---|---|
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 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
368 So(props[i].SetValue(v+v, props[i].IndexSetting( )), ShouldBeNil) | 368 So(props[i].SetValue(v+v, props[i].IndexSetting( )), ShouldBeNil) |
369 case int64: | 369 case int64: |
370 // + means integer addition. | 370 // + means integer addition. |
371 So(props[i].SetValue(v+v, props[i].IndexSetting( )), ShouldBeNil) | 371 So(props[i].SetValue(v+v, props[i].IndexSetting( )), ShouldBeNil) |
372 } | 372 } |
373 } | 373 } |
374 } | 374 } |
375 return propMap, nil | 375 return propMap, nil |
376 } | 376 } |
377 | 377 |
378 func (d *Doubler) GetAllMeta() PropertyMap { retur n nil } | 378 func (d *Doubler) Problem() error { return nil } |
379 func (d *Doubler) GetMeta(string) (interface{}, error) { retur n nil, ErrMetaFieldUnset } | |
380 func (d *Doubler) GetMetaDefault(_ string, dflt interface{}) interface{} { retur n dflt } | |
381 func (d *Doubler) SetMeta(string, interface{}) error { retur n ErrMetaFieldUnset } | |
382 func (d *Doubler) Problem() error { retur n nil } | |
383 | 379 |
384 var _ PropertyLoadSaver = (*Doubler)(nil) | 380 var _ PropertyLoadSaver = (*Doubler)(nil) |
385 | 381 |
386 type Deriver struct { | 382 type Deriver struct { |
387 S, Derived, Ignored string | 383 S, Derived, Ignored string |
388 } | 384 } |
389 | 385 |
390 func (d *Deriver) Load(props PropertyMap) error { | 386 func (d *Deriver) Load(props PropertyMap) error { |
391 for name, p := range props { | 387 for name, p := range props { |
392 if name != "S" { | 388 if name != "S" { |
393 continue | 389 continue |
394 } | 390 } |
395 d.S = p[0].Value().(string) | 391 d.S = p[0].Value().(string) |
396 d.Derived = "derived+" + d.S | 392 d.Derived = "derived+" + d.S |
397 } | 393 } |
398 return nil | 394 return nil |
399 } | 395 } |
400 | 396 |
401 func (d *Deriver) Save(withMeta bool) (PropertyMap, error) { | 397 func (d *Deriver) Save(withMeta bool) (PropertyMap, error) { |
402 return map[string][]Property{ | 398 return map[string][]Property{ |
403 "S": {mp(d.S)}, | 399 "S": {mp(d.S)}, |
404 }, nil | 400 }, nil |
405 } | 401 } |
406 | 402 |
407 func (d *Deriver) GetAllMeta() PropertyMap { retur n nil } | 403 func (d *Deriver) Problem() error { return nil } |
408 func (d *Deriver) GetMeta(string) (interface{}, error) { retur n nil, ErrMetaFieldUnset } | |
409 func (d *Deriver) GetMetaDefault(_ string, dflt interface{}) interface{} { retur n dflt } | |
410 func (d *Deriver) SetMeta(string, interface{}) error { retur n ErrMetaFieldUnset } | |
411 func (d *Deriver) Problem() error { retur n nil } | |
412 | 404 |
413 var _ PropertyLoadSaver = (*Deriver)(nil) | 405 var _ PropertyLoadSaver = (*Deriver)(nil) |
414 | 406 |
407 type Augmenter struct { | |
408 S string | |
409 | |
410 g string `gae:"-"` | |
411 } | |
412 | |
413 func (a *Augmenter) Load(props PropertyMap) error { | |
414 if e := props["Extra"]; len(e) > 0 { | |
415 a.g = e[0].Value().(string) | |
416 delete(props, "Extra") | |
417 } | |
418 if err := GetPLS(a).Load(props); err != nil { | |
419 return err | |
420 } | |
421 return nil | |
422 } | |
423 | |
424 func (a *Augmenter) Save(withMeta bool) (PropertyMap, error) { | |
425 props, err := GetPLS(a).Save(withMeta) | |
426 if err != nil { | |
427 return nil, err | |
428 } | |
429 props["Extra"] = []Property{MkProperty("ohai!")} | |
430 return props, nil | |
431 } | |
432 | |
433 func (a *Augmenter) Problem() error { return nil } | |
434 | |
435 var _ PropertyLoadSaver = (*Augmenter)(nil) | |
436 | |
415 type BK struct { | 437 type BK struct { |
416 Key blobstore.Key | 438 Key blobstore.Key |
417 } | 439 } |
418 | 440 |
419 type Convertable []int64 | 441 type Convertable []int64 |
420 | 442 |
421 var _ PropertyConverter = (*Convertable)(nil) | 443 var _ PropertyConverter = (*Convertable)(nil) |
422 | 444 |
423 func (c *Convertable) ToProperty() (ret Property, err error) { | 445 func (c *Convertable) ToProperty() (ret Property, err error) { |
424 buf := make([]string, len(*c)) | 446 buf := make([]string, len(*c)) |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
545 id int64 `gae:"-"` | 567 id int64 `gae:"-"` |
546 } | 568 } |
547 | 569 |
548 var _ MetaGetterSetter = (*IDParser)(nil) | 570 var _ MetaGetterSetter = (*IDParser)(nil) |
549 | 571 |
550 func (i *IDParser) getFullID() string { | 572 func (i *IDParser) getFullID() string { |
551 return fmt.Sprintf("%s|%d", i.parent, i.id) | 573 return fmt.Sprintf("%s|%d", i.parent, i.id) |
552 } | 574 } |
553 | 575 |
554 func (i *IDParser) GetAllMeta() PropertyMap { | 576 func (i *IDParser) GetAllMeta() PropertyMap { |
555 » pm := PropertyMap{} | 577 » pm := GetPLS(i).GetAllMeta() |
556 pm.SetMeta("id", i.getFullID()) | 578 pm.SetMeta("id", i.getFullID()) |
557 return pm | 579 return pm |
558 } | 580 } |
559 | 581 |
560 func (i *IDParser) GetMeta(key string) (interface{}, error) { | 582 func (i *IDParser) GetMeta(key string) (interface{}, error) { |
561 if key == "id" { | 583 if key == "id" { |
562 return i.getFullID(), nil | 584 return i.getFullID(), nil |
563 } | 585 } |
564 » return nil, ErrMetaFieldUnset | 586 » return GetPLS(i).GetMeta(key) |
565 } | 587 } |
566 | 588 |
567 func (i *IDParser) GetMetaDefault(key string, dflt interface{}) interface{} { | 589 func (i *IDParser) GetMetaDefault(key string, dflt interface{}) interface{} { |
568 return GetMetaDefaultImpl(i.GetMeta, key, dflt) | 590 return GetMetaDefaultImpl(i.GetMeta, key, dflt) |
569 } | 591 } |
570 | 592 |
571 func (i *IDParser) SetMeta(key string, value interface{}) (err error) { | 593 func (i *IDParser) SetMeta(key string, value interface{}) (err error) { |
572 if key == "id" { | 594 if key == "id" { |
573 // let the panics flooowwww | 595 // let the panics flooowwww |
574 vS := strings.SplitN(value.(string), "|", 2) | 596 vS := strings.SplitN(value.(string), "|", 2) |
575 i.parent = vS[0] | 597 i.parent = vS[0] |
576 i.id, err = strconv.ParseInt(vS[1], 10, 64) | 598 i.id, err = strconv.ParseInt(vS[1], 10, 64) |
577 return | 599 return |
578 } | 600 } |
579 » return ErrMetaFieldUnset | 601 » return GetPLS(i).SetMeta(key, value) |
580 } | 602 } |
581 | 603 |
582 type KindOverride struct { | 604 type KindOverride struct { |
583 ID int64 `gae:"$id"` | 605 ID int64 `gae:"$id"` |
584 | 606 |
585 customKind string `gae:"-"` | 607 customKind string `gae:"-"` |
586 } | 608 } |
587 | 609 |
588 var _ MetaGetterSetter = (*KindOverride)(nil) | 610 var _ MetaGetterSetter = (*KindOverride)(nil) |
589 | 611 |
590 func (i *KindOverride) GetAllMeta() PropertyMap { | 612 func (i *KindOverride) GetAllMeta() PropertyMap { |
591 » pm := PropertyMap{} | 613 » pm := GetPLS(i).GetAllMeta() |
592 if i.customKind != "" { | 614 if i.customKind != "" { |
593 pm.SetMeta("kind", i.customKind) | 615 pm.SetMeta("kind", i.customKind) |
594 } | 616 } |
595 return pm | 617 return pm |
596 } | 618 } |
597 | 619 |
598 func (i *KindOverride) GetMeta(key string) (interface{}, error) { | 620 func (i *KindOverride) GetMeta(key string) (interface{}, error) { |
599 if key == "kind" && i.customKind != "" { | 621 if key == "kind" && i.customKind != "" { |
600 return i.customKind, nil | 622 return i.customKind, nil |
601 } | 623 } |
602 » return nil, ErrMetaFieldUnset | 624 » return GetPLS(i).GetMeta(key) |
603 } | 625 } |
604 | 626 |
605 func (i *KindOverride) GetMetaDefault(key string, dflt interface{}) interface{} { | 627 func (i *KindOverride) GetMetaDefault(key string, dflt interface{}) interface{} { |
606 return GetMetaDefaultImpl(i.GetMeta, key, dflt) | 628 return GetMetaDefaultImpl(i.GetMeta, key, dflt) |
607 } | 629 } |
608 | 630 |
609 func (i *KindOverride) SetMeta(key string, value interface{}) error { | 631 func (i *KindOverride) SetMeta(key string, value interface{}) error { |
610 if key == "kind" { | 632 if key == "kind" { |
611 kind := value.(string) | 633 kind := value.(string) |
612 if kind != "KindOverride" { | 634 if kind != "KindOverride" { |
613 i.customKind = kind | 635 i.customKind = kind |
614 } else { | 636 } else { |
615 i.customKind = "" | 637 i.customKind = "" |
616 } | 638 } |
617 return nil | 639 return nil |
618 } | 640 } |
619 return ErrMetaFieldUnset | 641 return ErrMetaFieldUnset |
620 } | 642 } |
621 | 643 |
644 type Simple struct { | |
645 } | |
646 | |
622 type testCase struct { | 647 type testCase struct { |
623 desc string | 648 desc string |
624 src interface{} | 649 src interface{} |
625 want interface{} | 650 want interface{} |
626 plsErr string | 651 plsErr string |
627 saveErr string | 652 saveErr string |
628 plsLoadErr string | 653 plsLoadErr string |
629 loadErr string | 654 loadErr string |
630 } | 655 } |
631 | 656 |
(...skipping 548 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1180 { | 1205 { |
1181 desc: "save struct load deriver", | 1206 desc: "save struct load deriver", |
1182 src: &X0{S: "s", I: 1}, | 1207 src: &X0{S: "s", I: 1}, |
1183 want: &Deriver{S: "s", Derived: "derived+s"}, | 1208 want: &Deriver{S: "s", Derived: "derived+s"}, |
1184 }, | 1209 }, |
1185 { | 1210 { |
1186 desc: "save deriver load struct", | 1211 desc: "save deriver load struct", |
1187 src: &Deriver{S: "s", Derived: "derived+s", Ignored: "ignored"} , | 1212 src: &Deriver{S: "s", Derived: "derived+s", Ignored: "ignored"} , |
1188 want: &X0{S: "s"}, | 1213 want: &X0{S: "s"}, |
1189 }, | 1214 }, |
1215 { | |
1216 desc: "augmenter save", | |
1217 src: &Augmenter{S: "s"}, | |
1218 want: PropertyMap{ | |
1219 "S": {mp("s")}, | |
1220 "Extra": {mp("ohai!")}, | |
1221 }, | |
1222 }, | |
1223 { | |
1224 desc: "augmenter load", | |
1225 src: PropertyMap{ | |
1226 "S": {mp("s")}, | |
1227 "Extra": {mp("kthxbye!")}, | |
1228 }, | |
1229 want: &Augmenter{S: "s", g: "kthxbye!"}, | |
1230 }, | |
1190 // Regression: CL 25062824 broke handling of appengine.BlobKey fields. | 1231 // Regression: CL 25062824 broke handling of appengine.BlobKey fields. |
1191 { | 1232 { |
1192 desc: "appengine.BlobKey", | 1233 desc: "appengine.BlobKey", |
1193 src: &BK{Key: "blah"}, | 1234 src: &BK{Key: "blah"}, |
1194 want: &BK{Key: "blah"}, | 1235 want: &BK{Key: "blah"}, |
1195 }, | 1236 }, |
1196 { | 1237 { |
1197 desc: "zero time.Time", | 1238 desc: "zero time.Time", |
1198 src: &T{T: time.Time{}}, | 1239 src: &T{T: time.Time{}}, |
1199 want: &T{T: time.Time{}}, | 1240 want: &T{T: time.Time{}}, |
(...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1603 } | 1644 } |
1604 }) | 1645 }) |
1605 } | 1646 } |
1606 | 1647 |
1607 func TestMeta(t *testing.T) { | 1648 func TestMeta(t *testing.T) { |
1608 t.Parallel() | 1649 t.Parallel() |
1609 | 1650 |
1610 Convey("Test meta fields", t, func() { | 1651 Convey("Test meta fields", t, func() { |
1611 Convey("Can retrieve from struct", func() { | 1652 Convey("Can retrieve from struct", func() { |
1612 o := &N0{ID: 100} | 1653 o := &N0{ID: 100} |
1613 » » » pls := GetPLS(o) | 1654 » » » mgs := getMGS(o) |
1614 » » » val, err := pls.GetMeta("id") | 1655 » » » val, err := mgs.GetMeta("id") |
1615 So(err, ShouldBeNil) | 1656 So(err, ShouldBeNil) |
1616 So(val, ShouldEqual, 100) | 1657 So(val, ShouldEqual, 100) |
1617 | 1658 |
1618 » » » val, err = pls.GetMeta("kind") | 1659 » » » val, err = mgs.GetMeta("kind") |
1619 So(err, ShouldBeNil) | 1660 So(err, ShouldBeNil) |
1620 So(val, ShouldEqual, "whatnow") | 1661 So(val, ShouldEqual, "whatnow") |
1621 | 1662 |
1622 » » » So(pls.GetMetaDefault("kind", "zappo"), ShouldEqual, "wh atnow") | 1663 » » » So(mgs.GetMetaDefault("kind", "zappo"), ShouldEqual, "wh atnow") |
1623 » » » So(pls.GetMetaDefault("id", "stringID"), ShouldEqual, "s tringID") | 1664 » » » So(mgs.GetMetaDefault("id", "stringID"), ShouldEqual, "s tringID") |
1624 » » » So(pls.GetMetaDefault("id", 6), ShouldEqual, 100) | 1665 » » » So(mgs.GetMetaDefault("id", 6), ShouldEqual, 100) |
1625 }) | 1666 }) |
1626 | 1667 |
1627 Convey("Getting something not there is an error", func() { | 1668 Convey("Getting something not there is an error", func() { |
1628 o := &N0{ID: 100} | 1669 o := &N0{ID: 100} |
1629 » » » pls := GetPLS(o) | 1670 » » » mgs := getMGS(o) |
1630 » » » _, err := pls.GetMeta("wat") | 1671 » » » _, err := mgs.GetMeta("wat") |
1631 So(err, ShouldEqual, ErrMetaFieldUnset) | 1672 So(err, ShouldEqual, ErrMetaFieldUnset) |
1632 }) | 1673 }) |
1633 | 1674 |
1634 Convey("Default works for missing fields", func() { | 1675 Convey("Default works for missing fields", func() { |
1635 o := &N0{ID: 100} | 1676 o := &N0{ID: 100} |
1636 » » » pls := GetPLS(o) | 1677 » » » mgs := getMGS(o) |
1637 » » » So(pls.GetMetaDefault("whozit", 10), ShouldEqual, 10) | 1678 » » » So(mgs.GetMetaDefault("whozit", 10), ShouldEqual, 10) |
1638 }) | 1679 }) |
1639 | 1680 |
1640 Convey("getting/setting from a bad struct is an error", func() { | 1681 Convey("getting/setting from a bad struct is an error", func() { |
1641 o := &Recursive{} | 1682 o := &Recursive{} |
1642 » » » pls := GetPLS(o) | 1683 » » » mgs := getMGS(o) |
1643 » » » _, err := pls.GetMeta("wat") | 1684 » » » _, err := mgs.GetMeta("wat") |
1644 So(err, ShouldNotBeNil) | 1685 So(err, ShouldNotBeNil) |
1645 | 1686 |
1646 » » » err = pls.SetMeta("wat", 100) | 1687 » » » err = mgs.SetMeta("wat", 100) |
1647 So(err, ShouldNotBeNil) | 1688 So(err, ShouldNotBeNil) |
1648 }) | 1689 }) |
1649 | 1690 |
1650 Convey("Default works for bad structs", func() { | 1691 Convey("Default works for bad structs", func() { |
1651 o := &Recursive{} | 1692 o := &Recursive{} |
1652 » » » pls := GetPLS(o) | 1693 » » » mgs := getMGS(o) |
1653 » » » So(pls.GetMetaDefault("whozit", 10), ShouldEqual, 10) | 1694 » » » So(mgs.GetMetaDefault("whozit", 10), ShouldEqual, 10) |
1654 }) | 1695 }) |
1655 | 1696 |
1656 Convey("can assign values to exported meta fields", func() { | 1697 Convey("can assign values to exported meta fields", func() { |
1657 o := &N0{ID: 100} | 1698 o := &N0{ID: 100} |
1658 » » » pls := GetPLS(o) | 1699 » » » mgs := getMGS(o) |
1659 » » » err := pls.SetMeta("id", int64(200)) | 1700 » » » err := mgs.SetMeta("id", int64(200)) |
1660 So(err, ShouldBeNil) | 1701 So(err, ShouldBeNil) |
1661 So(o.ID, ShouldEqual, 200) | 1702 So(o.ID, ShouldEqual, 200) |
1662 | 1703 |
1663 }) | 1704 }) |
1664 | 1705 |
1665 Convey("assigning to unsassiagnable fields is a simple error", f unc() { | 1706 Convey("assigning to unsassiagnable fields is a simple error", f unc() { |
1666 o := &N0{ID: 100} | 1707 o := &N0{ID: 100} |
1667 » » » pls := GetPLS(o) | 1708 » » » mgs := getMGS(o) |
1668 » » » err := pls.SetMeta("kind", "hi") | 1709 » » » err := mgs.SetMeta("kind", "hi") |
1669 So(err.Error(), ShouldContainSubstring, "unexported fiel d") | 1710 So(err.Error(), ShouldContainSubstring, "unexported fiel d") |
1670 | 1711 |
1671 » » » err = pls.SetMeta("noob", "hi") | 1712 » » » err = mgs.SetMeta("noob", "hi") |
1672 So(err, ShouldEqual, ErrMetaFieldUnset) | 1713 So(err, ShouldEqual, ErrMetaFieldUnset) |
1673 }) | 1714 }) |
1674 }) | 1715 }) |
1675 | 1716 |
1676 Convey("StructPLS Miscellaneous", t, func() { | 1717 Convey("StructPLS Miscellaneous", t, func() { |
1718 Convey("a simple struct has a default $kind", func() { | |
1719 pls := GetPLS(&Simple{}) | |
iannucci
2015/10/30 21:47:07
may be clearer to just put `type Simple struct{}`
| |
1720 So(pls.GetAllMeta(), ShouldResemble, PropertyMap{ | |
1721 "$kind": []Property{mpNI("Simple")}, | |
1722 }) | |
1723 }) | |
1724 | |
1677 Convey("multiple overlapping fields is an error", func() { | 1725 Convey("multiple overlapping fields is an error", func() { |
1678 o := &BadMeta{} | 1726 o := &BadMeta{} |
1679 pls := GetPLS(o) | 1727 pls := GetPLS(o) |
1680 err := pls.Load(nil) | 1728 err := pls.Load(nil) |
1681 So(err, ShouldErrLike, "multiple times") | 1729 So(err, ShouldErrLike, "multiple times") |
1682 e := pls.Problem() | 1730 e := pls.Problem() |
1683 _, err = pls.Save(true) | 1731 _, err = pls.Save(true) |
1684 So(err, ShouldEqual, e) | 1732 So(err, ShouldEqual, e) |
1685 err = pls.Load(nil) | 1733 err = pls.Load(nil) |
1686 So(err, ShouldEqual, e) | 1734 So(err, ShouldEqual, e) |
1687 }) | 1735 }) |
1688 | 1736 |
1689 Convey("empty property names are invalid", func() { | 1737 Convey("empty property names are invalid", func() { |
1690 So(validPropertyName(""), ShouldBeFalse) | 1738 So(validPropertyName(""), ShouldBeFalse) |
1691 }) | 1739 }) |
1692 | 1740 |
1693 Convey("attempting to get a PLS for a non *struct is an error", func() { | 1741 Convey("attempting to get a PLS for a non *struct is an error", func() { |
1694 pls := GetPLS((*[]string)(nil)) | 1742 pls := GetPLS((*[]string)(nil)) |
1695 So(pls.Problem(), ShouldEqual, ErrInvalidEntityType) | 1743 So(pls.Problem(), ShouldEqual, ErrInvalidEntityType) |
1744 | |
1745 Convey("the error PLS can still be used", func() { | |
1746 k, _ := pls.GetMeta("kind") | |
1747 So(k, ShouldBeNil) | |
1748 | |
1749 props := pls.GetAllMeta() | |
1750 So(props, ShouldResemble, PropertyMap{ | |
1751 "$kind": []Property{mpNI("")}, | |
1752 }) | |
1753 }) | |
1696 }) | 1754 }) |
1697 | 1755 |
1698 Convey("convertible meta default types", func() { | 1756 Convey("convertible meta default types", func() { |
1699 type OKDefaults struct { | 1757 type OKDefaults struct { |
1700 When string `gae:"$when,tomorrow"` | 1758 When string `gae:"$when,tomorrow"` |
1701 Amount int64 `gae:"$amt,100"` | 1759 Amount int64 `gae:"$amt,100"` |
1702 DoIt Toggle `gae:"$doit,on"` | 1760 DoIt Toggle `gae:"$doit,on"` |
1703 } | 1761 } |
1704 okd := &OKDefaults{} | 1762 okd := &OKDefaults{} |
1705 pls := GetPLS(okd) | 1763 pls := GetPLS(okd) |
1764 mgs := getMGS(okd) | |
1706 So(pls.Problem(), ShouldBeNil) | 1765 So(pls.Problem(), ShouldBeNil) |
1707 | 1766 |
1708 » » » v, err := pls.GetMeta("when") | 1767 » » » v, err := mgs.GetMeta("when") |
1709 So(err, ShouldBeNil) | 1768 So(err, ShouldBeNil) |
1710 So(v, ShouldEqual, "tomorrow") | 1769 So(v, ShouldEqual, "tomorrow") |
1711 | 1770 |
1712 » » » v, err = pls.GetMeta("amt") | 1771 » » » v, err = mgs.GetMeta("amt") |
1713 So(err, ShouldBeNil) | 1772 So(err, ShouldBeNil) |
1714 So(v, ShouldEqual, int64(100)) | 1773 So(v, ShouldEqual, int64(100)) |
1715 | 1774 |
1716 So(okd.DoIt, ShouldEqual, Auto) | 1775 So(okd.DoIt, ShouldEqual, Auto) |
1717 » » » v, err = pls.GetMeta("doit") | 1776 » » » v, err = mgs.GetMeta("doit") |
1718 So(err, ShouldBeNil) | 1777 So(err, ShouldBeNil) |
1719 So(v, ShouldBeTrue) | 1778 So(v, ShouldBeTrue) |
1720 | 1779 |
1721 » » » err = pls.SetMeta("doit", false) | 1780 » » » err = mgs.SetMeta("doit", false) |
1722 So(err, ShouldBeNil) | 1781 So(err, ShouldBeNil) |
1723 » » » v, err = pls.GetMeta("doit") | 1782 » » » v, err = mgs.GetMeta("doit") |
1724 So(err, ShouldBeNil) | 1783 So(err, ShouldBeNil) |
1725 So(v, ShouldBeFalse) | 1784 So(v, ShouldBeFalse) |
1726 So(okd.DoIt, ShouldEqual, Off) | 1785 So(okd.DoIt, ShouldEqual, Off) |
1727 | 1786 |
1728 » » » err = pls.SetMeta("doit", true) | 1787 » » » err = mgs.SetMeta("doit", true) |
1729 So(err, ShouldBeNil) | 1788 So(err, ShouldBeNil) |
1730 » » » v, err = pls.GetMeta("doit") | 1789 » » » v, err = mgs.GetMeta("doit") |
1731 So(err, ShouldBeNil) | 1790 So(err, ShouldBeNil) |
1732 So(v, ShouldBeTrue) | 1791 So(v, ShouldBeTrue) |
1733 So(okd.DoIt, ShouldEqual, On) | 1792 So(okd.DoIt, ShouldEqual, On) |
1734 | 1793 |
1735 Convey("Toggle fields REQUIRE a default", func() { | 1794 Convey("Toggle fields REQUIRE a default", func() { |
1736 type BadToggle struct { | 1795 type BadToggle struct { |
1737 Bad Toggle `gae:"$wut"` | 1796 Bad Toggle `gae:"$wut"` |
1738 } | 1797 } |
1739 pls := GetPLS(&BadToggle{}) | 1798 pls := GetPLS(&BadToggle{}) |
1740 So(pls.Problem().Error(), ShouldContainSubstring , "bad/missing default") | 1799 So(pls.Problem().Error(), ShouldContainSubstring , "bad/missing default") |
(...skipping 21 matching lines...) Expand all Loading... | |
1762 v, err = pm.GetMeta("amt") | 1821 v, err = pm.GetMeta("amt") |
1763 So(err, ShouldBeNil) | 1822 So(err, ShouldBeNil) |
1764 So(v, ShouldEqual, int64(100)) | 1823 So(v, ShouldEqual, int64(100)) |
1765 }) | 1824 }) |
1766 | 1825 |
1767 Convey("default are optional", func() { | 1826 Convey("default are optional", func() { |
1768 type OverrideDefault struct { | 1827 type OverrideDefault struct { |
1769 Val int64 `gae:"$val"` | 1828 Val int64 `gae:"$val"` |
1770 } | 1829 } |
1771 o := &OverrideDefault{} | 1830 o := &OverrideDefault{} |
1772 » » » pls := GetPLS(o) | 1831 » » » mgs := getMGS(o) |
1773 | 1832 |
1774 » » » v, err := pls.GetMeta("val") | 1833 » » » v, err := mgs.GetMeta("val") |
1775 So(err, ShouldBeNil) | 1834 So(err, ShouldBeNil) |
1776 So(v, ShouldEqual, int64(0)) | 1835 So(v, ShouldEqual, int64(0)) |
1777 }) | 1836 }) |
1778 | 1837 |
1779 Convey("overridable defaults", func() { | 1838 Convey("overridable defaults", func() { |
1780 type OverrideDefault struct { | 1839 type OverrideDefault struct { |
1781 Val int64 `gae:"$val,100"` | 1840 Val int64 `gae:"$val,100"` |
1782 } | 1841 } |
1783 o := &OverrideDefault{} | 1842 o := &OverrideDefault{} |
1784 » » » pls := GetPLS(o) | 1843 » » » mgs := getMGS(o) |
1785 | 1844 |
1786 » » » v, err := pls.GetMeta("val") | 1845 » » » v, err := mgs.GetMeta("val") |
1787 So(err, ShouldBeNil) | 1846 So(err, ShouldBeNil) |
1788 So(v, ShouldEqual, int64(100)) | 1847 So(v, ShouldEqual, int64(100)) |
1789 | 1848 |
1790 o.Val = 10 | 1849 o.Val = 10 |
1791 » » » v, err = pls.GetMeta("val") | 1850 » » » v, err = mgs.GetMeta("val") |
1792 So(err, ShouldBeNil) | 1851 So(err, ShouldBeNil) |
1793 So(v, ShouldEqual, int64(10)) | 1852 So(v, ShouldEqual, int64(10)) |
1794 }) | 1853 }) |
1795 | 1854 |
1796 Convey("Bad default meta type", func() { | 1855 Convey("Bad default meta type", func() { |
1797 type BadDefault struct { | 1856 type BadDefault struct { |
1798 Val time.Time `gae:"$meta,tomorrow"` | 1857 Val time.Time `gae:"$meta,tomorrow"` |
1799 } | 1858 } |
1800 pls := GetPLS(&BadDefault{}) | 1859 pls := GetPLS(&BadDefault{}) |
1801 So(pls.Problem().Error(), ShouldContainSubstring, "bad t ype") | 1860 So(pls.Problem().Error(), ShouldContainSubstring, "bad t ype") |
1802 }) | 1861 }) |
1803 | 1862 |
1804 Convey("MetaGetterSetter implementation (IDParser)", func() { | 1863 Convey("MetaGetterSetter implementation (IDParser)", func() { |
1805 idp := &IDParser{parent: "moo", id: 100} | 1864 idp := &IDParser{parent: "moo", id: 100} |
1806 » » » pls := GetPLS(idp) | 1865 » » » mgs := getMGS(idp) |
1807 » » » So(pls.GetMetaDefault("id", ""), ShouldEqual, "moo|100") | 1866 » » » So(mgs.GetMetaDefault("id", ""), ShouldEqual, "moo|100") |
1808 » » » So(pls.GetMetaDefault("kind", ""), ShouldEqual, "CoolKin d") | 1867 » » » So(mgs.GetMetaDefault("kind", ""), ShouldEqual, "CoolKin d") |
1809 | 1868 |
1810 » » » So(pls.SetMeta("kind", "Something"), ShouldErrLike, "une xported field") | 1869 » » » So(mgs.SetMeta("kind", "Something"), ShouldErrLike, "une xported field") |
1811 » » » So(pls.SetMeta("id", "happy|27"), ShouldBeNil) | 1870 » » » So(mgs.SetMeta("id", "happy|27"), ShouldBeNil) |
1812 | 1871 |
1813 So(idp.parent, ShouldEqual, "happy") | 1872 So(idp.parent, ShouldEqual, "happy") |
1814 So(idp.id, ShouldEqual, 27) | 1873 So(idp.id, ShouldEqual, 27) |
1815 | 1874 |
1816 » » » So(pls.GetAllMeta(), ShouldResemble, PropertyMap{ | 1875 » » » So(mgs.GetAllMeta(), ShouldResemble, PropertyMap{ |
1817 "$id": {MkPropertyNI("happy|27")}, | 1876 "$id": {MkPropertyNI("happy|27")}, |
1818 "$kind": {MkPropertyNI("CoolKind")}, | 1877 "$kind": {MkPropertyNI("CoolKind")}, |
1819 }) | 1878 }) |
1820 }) | 1879 }) |
1821 | 1880 |
1822 Convey("MetaGetterSetter implementation (KindOverride)", func() { | 1881 Convey("MetaGetterSetter implementation (KindOverride)", func() { |
1823 ko := &KindOverride{ID: 20} | 1882 ko := &KindOverride{ID: 20} |
1824 » » » pls := GetPLS(ko) | 1883 » » » mgs := getMGS(ko) |
1825 » » » So(pls.GetMetaDefault("kind", ""), ShouldEqual, "KindOve rride") | 1884 » » » So(mgs.GetMetaDefault("kind", ""), ShouldEqual, "KindOve rride") |
1826 | 1885 |
1827 ko.customKind = "something" | 1886 ko.customKind = "something" |
1828 » » » So(pls.GetMetaDefault("kind", ""), ShouldEqual, "somethi ng") | 1887 » » » So(mgs.GetMetaDefault("kind", ""), ShouldEqual, "somethi ng") |
1829 | 1888 |
1830 » » » So(pls.SetMeta("kind", "Nerp"), ShouldBeNil) | 1889 » » » So(mgs.SetMeta("kind", "Nerp"), ShouldBeNil) |
1831 So(ko.customKind, ShouldEqual, "Nerp") | 1890 So(ko.customKind, ShouldEqual, "Nerp") |
1832 | 1891 |
1833 » » » So(pls.SetMeta("kind", "KindOverride"), ShouldBeNil) | 1892 » » » So(mgs.SetMeta("kind", "KindOverride"), ShouldBeNil) |
1834 So(ko.customKind, ShouldEqual, "") | 1893 So(ko.customKind, ShouldEqual, "") |
1835 | 1894 |
1836 » » » So(pls.GetAllMeta(), ShouldResemble, PropertyMap{ | 1895 » » » So(mgs.GetAllMeta(), ShouldResemble, PropertyMap{ |
1837 "$id": {MkPropertyNI(20)}, | 1896 "$id": {MkPropertyNI(20)}, |
1838 "$kind": {MkPropertyNI("KindOverride")}, | 1897 "$kind": {MkPropertyNI("KindOverride")}, |
1839 }) | 1898 }) |
1840 ko.customKind = "wut" | 1899 ko.customKind = "wut" |
1841 » » » So(pls.GetAllMeta(), ShouldResemble, PropertyMap{ | 1900 » » » So(mgs.GetAllMeta(), ShouldResemble, PropertyMap{ |
1842 "$id": {MkPropertyNI(20)}, | 1901 "$id": {MkPropertyNI(20)}, |
1843 "$kind": {MkPropertyNI("wut")}, | 1902 "$kind": {MkPropertyNI("wut")}, |
1844 }) | 1903 }) |
1845 }) | 1904 }) |
1846 }) | 1905 }) |
1847 } | 1906 } |
OLD | NEW |