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

Unified Diff: service/datastore/index.go

Issue 1574353004: GitHub #8: Seed indexes from index.yml (Closed) Base URL: https://github.com/luci/gae@master
Patch Set: path argument in FindAndParseIndexYAML, add a couple of tests Created 4 years, 11 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « service/datastore/datastore_test.go ('k') | service/datastore/index_test.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: service/datastore/index.go
diff --git a/service/datastore/index.go b/service/datastore/index.go
index fe0ca4fd4fd67d7007ecd705a0546bb1a8b88ead..a60eca84dc2b148a5f22c46de0ccf50caa01713d 100644
--- a/service/datastore/index.go
+++ b/service/datastore/index.go
@@ -8,6 +8,8 @@ import (
"bytes"
"fmt"
"strings"
+
+ "gopkg.in/yaml.v2"
)
// IndexColumn represents a sort order for a single entity field.
@@ -49,6 +51,41 @@ func (i IndexColumn) cmp(o IndexColumn) int {
return cmpString(i.Property, o.Property)()
}
+// UnmarshalYAML deserializes a index.yml `property` into an IndexColumn.
+func (i *IndexColumn) UnmarshalYAML(unmarshal func(interface{}) error) error {
+ var m map[string]string
+ if err := unmarshal(&m); err != nil {
+ return err
+ }
+
+ name, ok := m["name"]
+ if !ok {
+ return fmt.Errorf("datastore: missing required key `name`: %v", m)
+ }
+ i.Property = name
+
+ i.Descending = false // default direction is "asc"
+ if v, ok := m["direction"]; ok && v == "desc" {
+ i.Descending = true
+ }
+
+ return nil
+}
+
+// MarshalYAML serializes an IndexColumn into a index.yml `property`.
+func (i *IndexColumn) MarshalYAML() (interface{}, error) {
+ direction := "asc"
+
+ if i.Descending {
+ direction = "desc"
+ }
+
+ return yaml.Marshal(map[string]string{
+ "name": i.Property,
+ "direction": direction,
+ })
+}
+
// String returns a human-readable version of this IndexColumn which is
// compatible with ParseIndexColumn.
func (i IndexColumn) String() string {
@@ -73,9 +110,22 @@ func (i IndexColumn) GQL() string {
// IndexDefinition holds the parsed definition of a datastore index definition.
type IndexDefinition struct {
- Kind string
- Ancestor bool
- SortBy []IndexColumn
+ Kind string `yaml:"kind"`
+ Ancestor bool `yaml:"ancestor"`
+ SortBy []IndexColumn `yaml:"properties"`
+}
+
+// MarshalYAML serializes an IndexDefinition into a index.yml `index`.
+func (id *IndexDefinition) MarshalYAML() (interface{}, error) {
+ if id.Builtin() || !id.Compound() {
+ return nil, fmt.Errorf("cannot generate YAML for %s", id)
+ }
+
+ return yaml.Marshal(map[string]interface{}{
+ "kind": id.Kind,
+ "ancestor": id.Ancestor,
+ "properties": id.SortBy,
+ })
}
// Equal returns true if the two IndexDefinitions are equivalent.
« no previous file with comments | « service/datastore/datastore_test.go ('k') | service/datastore/index_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698