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

Unified Diff: service/datastore/index.go

Issue 1355783002: Refactor keys and queries in datastore service and implementation. (Closed) Base URL: https://github.com/luci/gae.git@master
Patch Set: Created 5 years, 3 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
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

Powered by Google App Engine
This is Rietveld 408576698