You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.3 KiB

  1. // +build appengine
  2. package msgpack
  3. import (
  4. "reflect"
  5. ds "google.golang.org/appengine/datastore"
  6. )
  7. func init() {
  8. Register((*ds.Key)(nil), encodeDatastoreKeyValue, decodeDatastoreKeyValue)
  9. Register((*ds.Cursor)(nil), encodeDatastoreCursorValue, decodeDatastoreCursorValue)
  10. }
  11. func EncodeDatastoreKey(e *Encoder, key *ds.Key) error {
  12. if key == nil {
  13. return e.EncodeNil()
  14. }
  15. return e.EncodeString(key.Encode())
  16. }
  17. func encodeDatastoreKeyValue(e *Encoder, v reflect.Value) error {
  18. key := v.Interface().(*ds.Key)
  19. return EncodeDatastoreKey(e, key)
  20. }
  21. func DecodeDatastoreKey(d *Decoder) (*ds.Key, error) {
  22. v, err := d.DecodeString()
  23. if err != nil {
  24. return nil, err
  25. }
  26. if v == "" {
  27. return nil, nil
  28. }
  29. return ds.DecodeKey(v)
  30. }
  31. func decodeDatastoreKeyValue(d *Decoder, v reflect.Value) error {
  32. key, err := DecodeDatastoreKey(d)
  33. if err != nil {
  34. return err
  35. }
  36. v.Set(reflect.ValueOf(key))
  37. return nil
  38. }
  39. func encodeDatastoreCursorValue(e *Encoder, v reflect.Value) error {
  40. cursor := v.Interface().(ds.Cursor)
  41. return e.Encode(cursor.String())
  42. }
  43. func decodeDatastoreCursorValue(d *Decoder, v reflect.Value) error {
  44. s, err := d.DecodeString()
  45. if err != nil {
  46. return err
  47. }
  48. cursor, err := ds.DecodeCursor(s)
  49. if err != nil {
  50. return err
  51. }
  52. v.Set(reflect.ValueOf(cursor))
  53. return nil
  54. }