OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package datastore | 5 package datastore |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "encoding/base64" | 9 "encoding/base64" |
10 "encoding/json" | 10 "encoding/json" |
(...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
407 fmt.Fprintf(ret, ", %s, %s", gqlQuoteString(t.Kind), gql QuoteString(t.StringID)) | 407 fmt.Fprintf(ret, ", %s, %s", gqlQuoteString(t.Kind), gql QuoteString(t.StringID)) |
408 } | 408 } |
409 } | 409 } |
410 if _, err := ret.WriteString(")"); err != nil { | 410 if _, err := ret.WriteString(")"); err != nil { |
411 panic(err) | 411 panic(err) |
412 } | 412 } |
413 return ret.String() | 413 return ret.String() |
414 } | 414 } |
415 | 415 |
416 // Equal returns true iff the two keys represent identical key values. | 416 // Equal returns true iff the two keys represent identical key values. |
417 func (k *Key) Equal(other *Key) (ret bool) { | 417 func (k *Key) Equal(other *Key) bool { |
418 » return k.SameKind(other) && (k.LastTok() == other.LastTok()) | |
419 } | |
420 | |
421 // SameKind asserts that other refers to the same entity as k. This checks the | |
422 // full lineage of the key. | |
423 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.
| |
418 ret = (k.appID == other.appID && | 424 ret = (k.appID == other.appID && |
419 k.namespace == other.namespace && | 425 k.namespace == other.namespace && |
420 len(k.toks) == len(other.toks)) | 426 len(k.toks) == len(other.toks)) |
421 if ret { | 427 if ret { |
422 for i, t := range k.toks { | 428 for i, t := range k.toks { |
423 » » » if ret = t == other.toks[i]; !ret { | 429 » » » if i == len(k.toks)-1 { |
424 » » » » return | 430 » » » » // Last token: check only Kind. |
431 » » » » if ret = (t.Kind == other.toks[i].Kind); !ret { | |
432 » » » » » return | |
433 » » » » } | |
434 » » » } else { | |
435 » » » » if ret = t == other.toks[i]; !ret { | |
436 » » » » » return | |
437 » » » » } | |
425 } | 438 } |
426 } | 439 } |
427 } | 440 } |
428 return | 441 return |
429 } | 442 } |
430 | 443 |
444 // Partial returns a partial version of the key. The ID fields of the last token | |
445 // will be set to zero/empty. | |
446 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
| |
447 if k.Incomplete() { | |
448 return k | |
449 } | |
450 return NewKey(k.appID, k.namespace, k.Kind(), "", 0, k.Parent()) | |
451 } | |
452 | |
453 // 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.
| |
454 // and integer ID values. | |
455 func (k *Key) WithID(stringID string, intID int64) *Key { | |
456 if k.StringID() == stringID && k.IntID() == intID { | |
457 return k | |
458 } | |
459 return NewKey(k.appID, k.namespace, k.Kind(), stringID, intID, k.Parent( )) | |
460 } | |
461 | |
431 // Split componentizes the key into pieces (AppID, Namespace and tokens) | 462 // Split componentizes the key into pieces (AppID, Namespace and tokens) |
432 // | 463 // |
433 // Each token represents one piece of they key's 'path'. | 464 // Each token represents one piece of they key's 'path'. |
434 // | 465 // |
435 // toks is guaranteed to be empty if and only if k is nil. If k is non-nil then | 466 // toks is guaranteed to be empty if and only if k is nil. If k is non-nil then |
436 // it contains at least one token. | 467 // it contains at least one token. |
437 func (k *Key) Split() (appID, namespace string, toks []KeyTok) { | 468 func (k *Key) Split() (appID, namespace string, toks []KeyTok) { |
438 appID = k.appID | 469 appID = k.appID |
439 namespace = k.namespace | 470 namespace = k.namespace |
440 toks = make([]KeyTok, len(k.toks)) | 471 toks = make([]KeyTok, len(k.toks)) |
(...skipping 11 matching lines...) Expand all Loading... | |
452 for _, t := range k.toks { | 483 for _, t := range k.toks { |
453 ret += int64(len(t.Kind)) | 484 ret += int64(len(t.Kind)) |
454 if t.StringID != "" { | 485 if t.StringID != "" { |
455 ret += int64(len(t.StringID)) | 486 ret += int64(len(t.StringID)) |
456 } else { | 487 } else { |
457 ret += 8 | 488 ret += 8 |
458 } | 489 } |
459 } | 490 } |
460 return ret | 491 return ret |
461 } | 492 } |
OLD | NEW |