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 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 return errors.New("datastore: bad JSON key") | 313 return errors.New("datastore: bad JSON key") |
314 } | 314 } |
315 nk, err := NewKeyEncoded(string(buf[1 : len(buf)-1])) | 315 nk, err := NewKeyEncoded(string(buf[1 : len(buf)-1])) |
316 if err != nil { | 316 if err != nil { |
317 return err | 317 return err |
318 } | 318 } |
319 *k = *nk | 319 *k = *nk |
320 return nil | 320 return nil |
321 } | 321 } |
322 | 322 |
| 323 // GobEncode allows the Key to be encoded in a Gob struct. |
| 324 func (k *Key) GobEncode() ([]byte, error) { |
| 325 return []byte(k.Encode()), nil |
| 326 } |
| 327 |
| 328 // GobDecode allows the Key to be decoded in a Gob struct. |
| 329 func (k *Key) GobDecode(buf []byte) error { |
| 330 nk, err := NewKeyEncoded(string(buf)) |
| 331 if err != nil { |
| 332 return err |
| 333 } |
| 334 *k = *nk |
| 335 return nil |
| 336 } |
| 337 |
323 // Root returns the entity root for the given key. | 338 // Root returns the entity root for the given key. |
324 func (k *Key) Root() *Key { | 339 func (k *Key) Root() *Key { |
325 if len(k.toks) > 1 { | 340 if len(k.toks) > 1 { |
326 ret := *k | 341 ret := *k |
327 ret.toks = ret.toks[:1] | 342 ret.toks = ret.toks[:1] |
328 return &ret | 343 return &ret |
329 } | 344 } |
330 return k | 345 return k |
331 } | 346 } |
332 | 347 |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 for _, t := range k.toks { | 452 for _, t := range k.toks { |
438 ret += int64(len(t.Kind)) | 453 ret += int64(len(t.Kind)) |
439 if t.StringID != "" { | 454 if t.StringID != "" { |
440 ret += int64(len(t.StringID)) | 455 ret += int64(len(t.StringID)) |
441 } else { | 456 } else { |
442 ret += 8 | 457 ret += 8 |
443 } | 458 } |
444 } | 459 } |
445 return ret | 460 return ret |
446 } | 461 } |
OLD | NEW |