metadata.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2016 Google Inc. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package datastore
  5. import "context"
  6. // Datastore kinds for the metadata entities.
  7. const (
  8. namespaceKind = "__namespace__"
  9. kindKind = "__kind__"
  10. propertyKind = "__property__"
  11. )
  12. // Namespaces returns all the datastore namespaces.
  13. func Namespaces(ctx context.Context) ([]string, error) {
  14. // TODO(djd): Support range queries.
  15. q := NewQuery(namespaceKind).KeysOnly()
  16. keys, err := q.GetAll(ctx, nil)
  17. if err != nil {
  18. return nil, err
  19. }
  20. // The empty namespace key uses a numeric ID (==1), but luckily
  21. // the string ID defaults to "" for numeric IDs anyway.
  22. return keyNames(keys), nil
  23. }
  24. // Kinds returns the names of all the kinds in the current namespace.
  25. func Kinds(ctx context.Context) ([]string, error) {
  26. // TODO(djd): Support range queries.
  27. q := NewQuery(kindKind).KeysOnly()
  28. keys, err := q.GetAll(ctx, nil)
  29. if err != nil {
  30. return nil, err
  31. }
  32. return keyNames(keys), nil
  33. }
  34. // keyNames returns a slice of the provided keys' names (string IDs).
  35. func keyNames(keys []*Key) []string {
  36. n := make([]string, 0, len(keys))
  37. for _, k := range keys {
  38. n = append(n, k.StringID())
  39. }
  40. return n
  41. }
  42. // KindProperties returns all the indexed properties for the given kind.
  43. // The properties are returned as a map of property names to a slice of the
  44. // representation types. The representation types for the supported Go property
  45. // types are:
  46. //
  47. // "INT64": signed integers and time.Time
  48. // "DOUBLE": float32 and float64
  49. // "BOOLEAN": bool
  50. // "STRING": string, []byte and ByteString
  51. // "POINT": appengine.GeoPoint
  52. // "REFERENCE": *Key
  53. // "USER": (not used in the Go runtime)
  54. func KindProperties(ctx context.Context, kind string) (map[string][]string, error) {
  55. // TODO(djd): Support range queries.
  56. kindKey := NewKey(ctx, kindKind, kind, 0, nil)
  57. q := NewQuery(propertyKind).Ancestor(kindKey)
  58. propMap := map[string][]string{}
  59. props := []struct {
  60. Repr []string `datastore:"property_representation"`
  61. }{}
  62. keys, err := q.GetAll(ctx, &props)
  63. if err != nil {
  64. return nil, err
  65. }
  66. for i, p := range props {
  67. propMap[keys[i].StringID()] = p.Repr
  68. }
  69. return propMap, nil
  70. }