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

Unified Diff: service/datastore/key.go

Issue 2007123002: datastore: Update AllocateIDs to take keys. (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/gae@master
Patch Set: Add empty arg/key short-circuits for other varidic methods. Created 4 years, 7 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/key.go
diff --git a/service/datastore/key.go b/service/datastore/key.go
index 2071bfb7b2ad01ceed9c42592d5d6ba01fb6b053..7cabd3e3db8c337e01a6a6a8dceb811042bed3b1 100644
--- a/service/datastore/key.go
+++ b/service/datastore/key.go
@@ -414,20 +414,51 @@ func (k *Key) GQL() string {
}
// Equal returns true iff the two keys represent identical key values.
-func (k *Key) Equal(other *Key) (ret bool) {
+func (k *Key) Equal(other *Key) bool {
+ return k.SameKind(other) && (k.LastTok() == other.LastTok())
+}
+
+// SameKind asserts that other refers to the same entity as k. This checks the
+// full lineage of the key.
+func (k *Key) SameKind(other *Key) (ret bool) {
iannucci 2016/06/14 01:46:04 This terminology is misleading. Kind means somethi
dnj (Google) 2016/06/14 04:24:18 Done.
ret = (k.appID == other.appID &&
k.namespace == other.namespace &&
len(k.toks) == len(other.toks))
if ret {
for i, t := range k.toks {
- if ret = t == other.toks[i]; !ret {
- return
+ if i == len(k.toks)-1 {
+ // Last token: check only Kind.
+ if ret = (t.Kind == other.toks[i].Kind); !ret {
+ return
+ }
+ } else {
+ if ret = t == other.toks[i]; !ret {
+ return
+ }
}
}
}
return
}
+// Partial returns a partial version of the key. The ID fields of the last token
+// will be set to zero/empty.
+func (k *Key) Partial() *Key {
iannucci 2016/06/14 01:46:04 RemoveID? WithoutID?
dnj (Google) 2016/06/14 04:24:18 I changed "Incomplete" to "IsIncomplete" (asking q
+ if k.Incomplete() {
+ return k
+ }
+ return NewKey(k.appID, k.namespace, k.Kind(), "", 0, k.Parent())
+}
+
+// WithID returns a key with the same kind as k, but with the specified string
iannucci 2016/06/14 01:46:04 kind here is also confusing for the same reason.
dnj (Google) 2016/06/14 04:24:18 Done.
+// and integer ID values.
+func (k *Key) WithID(stringID string, intID int64) *Key {
+ if k.StringID() == stringID && k.IntID() == intID {
+ return k
+ }
+ return NewKey(k.appID, k.namespace, k.Kind(), stringID, intID, k.Parent())
+}
+
// Split componentizes the key into pieces (AppID, Namespace and tokens)
//
// Each token represents one piece of they key's 'path'.

Powered by Google App Engine
This is Rietveld 408576698