metadata.go 2.3 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 "golang.org/x/net/context"
  6. // Datastore kinds for the metadata entities.
  7. const (
  8. namespaceKind = "__namespace__"
  9. kindKind = "__kind__"
  10. propertyKind = "__property__"
  11. entityGroupKind = "__entitygroup__"
  12. )
  13. // Namespaces returns all the datastore namespaces.
  14. func Namespaces(ctx context.Context) ([]string, error) {
  15. // TODO(djd): Support range queries.
  16. q := NewQuery(namespaceKind).KeysOnly()
  17. keys, err := q.GetAll(ctx, nil)
  18. if err != nil {
  19. return nil, err
  20. }
  21. // The empty namespace key uses a numeric ID (==1), but luckily
  22. // the string ID defaults to "" for numeric IDs anyway.
  23. return keyNames(keys), nil
  24. }
  25. // Kinds returns the names of all the kinds in the current namespace.
  26. func Kinds(ctx context.Context) ([]string, error) {
  27. // TODO(djd): Support range queries.
  28. q := NewQuery(kindKind).KeysOnly()
  29. keys, err := q.GetAll(ctx, nil)
  30. if err != nil {
  31. return nil, err
  32. }
  33. return keyNames(keys), nil
  34. }
  35. // keyNames returns a slice of the provided keys' names (string IDs).
  36. func keyNames(keys []*Key) []string {
  37. n := make([]string, 0, len(keys))
  38. for _, k := range keys {
  39. n = append(n, k.StringID())
  40. }
  41. return n
  42. }
  43. // KindProperties returns all the indexed properties for the given kind.
  44. // The properties are returned as a map of property names to a slice of the
  45. // representation types. The representation types for the supported Go property
  46. // types are:
  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. }