Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

115 рядки
3.0 KiB

  1. // Copyright 2017 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package firestore
  15. import (
  16. "math/rand"
  17. "os"
  18. "sync"
  19. "time"
  20. "golang.org/x/net/context"
  21. )
  22. // A CollectionRef is a reference to Firestore collection.
  23. type CollectionRef struct {
  24. c *Client
  25. // Typically Parent.Path, or c.path if Parent is nil.
  26. // May be different if this CollectionRef was created from a stored reference
  27. // to a different project/DB.
  28. parentPath string
  29. // Parent is the document of which this collection is a part. It is
  30. // nil for top-level collections.
  31. Parent *DocumentRef
  32. // The full resource path of the collection: "projects/P/databases/D/documents..."
  33. Path string
  34. // ID is the collection identifier.
  35. ID string
  36. // Use the methods of Query on a CollectionRef to create and run queries.
  37. Query
  38. }
  39. func newTopLevelCollRef(c *Client, dbPath, id string) *CollectionRef {
  40. return &CollectionRef{
  41. c: c,
  42. ID: id,
  43. parentPath: dbPath,
  44. Path: dbPath + "/documents/" + id,
  45. Query: Query{c: c, collectionID: id, parentPath: dbPath},
  46. }
  47. }
  48. func newCollRefWithParent(c *Client, parent *DocumentRef, id string) *CollectionRef {
  49. return &CollectionRef{
  50. c: c,
  51. Parent: parent,
  52. ID: id,
  53. parentPath: parent.Path,
  54. Path: parent.Path + "/" + id,
  55. Query: Query{c: c, collectionID: id, parentPath: parent.Path},
  56. }
  57. }
  58. // Doc returns a DocumentRef that refers to the document in the collection with the
  59. // given identifier.
  60. func (c *CollectionRef) Doc(id string) *DocumentRef {
  61. if c == nil {
  62. return nil
  63. }
  64. return newDocRef(c, id)
  65. }
  66. // NewDoc returns a DocumentRef with a uniquely generated ID.
  67. func (c *CollectionRef) NewDoc() *DocumentRef {
  68. return c.Doc(uniqueID())
  69. }
  70. // Add generates a DocumentRef with a unique ID. It then creates the document
  71. // with the given data, which can be a map[string]interface{}, a struct or a
  72. // pointer to a struct.
  73. //
  74. // Add returns an error in the unlikely event that a document with the same ID
  75. // already exists.
  76. func (c *CollectionRef) Add(ctx context.Context, data interface{}) (*DocumentRef, *WriteResult, error) {
  77. d := c.NewDoc()
  78. wr, err := d.Create(ctx, data)
  79. if err != nil {
  80. return nil, nil, err
  81. }
  82. return d, wr, nil
  83. }
  84. const alphanum = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
  85. var (
  86. rngMu sync.Mutex
  87. rng = rand.New(rand.NewSource(time.Now().UnixNano() ^ int64(os.Getpid())))
  88. )
  89. func uniqueID() string {
  90. var b [20]byte
  91. rngMu.Lock()
  92. for i := 0; i < len(b); i++ {
  93. b[i] = alphanum[rng.Intn(len(alphanum))]
  94. }
  95. rngMu.Unlock()
  96. return string(b[:])
  97. }