Index: service/datastore/index.go |
diff --git a/service/datastore/index.go b/service/datastore/index.go |
index 44cb172bd05fa2c340a958a48598b5a6d1f521bf..cf26fc2b3978bf20c2696f0b422e688b5ba2e737 100644 |
--- a/service/datastore/index.go |
+++ b/service/datastore/index.go |
@@ -6,6 +6,8 @@ package datastore |
import ( |
"bytes" |
+ "fmt" |
+ "strings" |
) |
const MaxIndexColumns = 64 |
@@ -30,6 +32,23 @@ type IndexColumn struct { |
Direction IndexDirection |
} |
+func ParseIndexColumn(spec string) (IndexColumn, error) { |
+ col := IndexColumn{} |
+ if strings.HasPrefix(spec, "-") { |
+ col.Direction = DESCENDING |
+ col.Property = strings.TrimSpace(spec[1:]) |
+ } else if strings.HasPrefix(spec, "+") { |
+ return col, fmt.Errorf("datastore: invalid order: %q", spec) |
+ } else { |
+ col.Direction = ASCENDING |
+ col.Property = strings.TrimSpace(spec) |
+ } |
+ if col.Property == "" { |
+ return col, fmt.Errorf("datastore: empty order") |
+ } |
+ return col, nil |
+} |
+ |
func (i IndexColumn) cmp(o IndexColumn) int { |
// sort ascending first |
if i.Direction == ASCENDING && o.Direction == DESCENDING { |
@@ -40,6 +59,21 @@ func (i IndexColumn) cmp(o IndexColumn) int { |
return cmpString(i.Property, o.Property)() |
} |
+func (i IndexColumn) String() string { |
+ ret := "" |
+ if i.Direction == DESCENDING { |
+ ret = "-" |
+ } |
+ return ret + i.Property |
+} |
+ |
+func (i IndexColumn) GQL() string { |
+ if i.Direction == DESCENDING { |
+ return gqlQuoteName(i.Property) + " DESC" |
+ } |
+ return gqlQuoteName(i.Property) |
+} |
+ |
type IndexDefinition struct { |
Kind string |
Ancestor bool |