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

Unified Diff: service/datastore/key.go

Issue 1377863004: Add Key.HasAncestor() (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
« no previous file with comments | « no previous file | service/datastore/key_test.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: service/datastore/key.go
diff --git a/service/datastore/key.go b/service/datastore/key.go
index 3bf02e861e3f99118fab2258070106b04ca45091..b2b9e45f37da085b33f76f68f0f7d9ea5b791dc5 100644
--- a/service/datastore/key.go
+++ b/service/datastore/key.go
@@ -359,6 +359,22 @@ func (k *Key) Less(other *Key) bool {
return len(k.toks) < len(other.toks)
}
+// HasAncestor returns true iff other is an ancestor of k (or if other == k).
+func (k *Key) HasAncestor(other *Key) bool {
+ if k.appID != other.appID || k.namespace != other.namespace {
+ return false
+ }
+ if len(k.toks) < len(other.toks) {
+ return false
+ }
+ for i, tok := range other.toks {
+ if tok != k.toks[i] {
+ return false
+ }
+ }
+ return true
+}
+
// GQL returns a correctly formatted Cloud Datastore GQL key literal.
//
// The flavor of GQL that this emits is defined here:
« no previous file with comments | « no previous file | service/datastore/key_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698