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.
 
 
 

162 lines
3.4 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 internal
  15. import (
  16. "fmt"
  17. firestore "cloud.google.com/go/firestore"
  18. "golang.org/x/net/context"
  19. "google.golang.org/api/iterator"
  20. )
  21. const ELLIPSIS = 0
  22. //[ structDef
  23. type State struct {
  24. Capital string `firestore:"capital"`
  25. Population float64 `firestore:"pop"` // in millions
  26. }
  27. //]
  28. func f1() {
  29. //[ NewClient
  30. ctx := context.Background()
  31. client, err := firestore.NewClient(ctx, "projectID")
  32. if err != nil {
  33. // TODO: Handle error.
  34. }
  35. //]
  36. //[ refs
  37. states := client.Collection("States")
  38. ny := states.Doc("NewYork")
  39. // Or, in a single call:
  40. ny = client.Doc("States/NewYork")
  41. //]
  42. //[ docref.Get
  43. docsnap, err := ny.Get(ctx)
  44. if err != nil {
  45. // TODO: Handle error.
  46. }
  47. dataMap := docsnap.Data()
  48. fmt.Println(dataMap)
  49. //]
  50. //[ DataTo
  51. var nyData State
  52. if err := docsnap.DataTo(&nyData); err != nil {
  53. // TODO: Handle error.
  54. }
  55. //]
  56. //[ GetAll
  57. docsnaps, err := client.GetAll(ctx, []*firestore.DocumentRef{
  58. states.Doc("Wisconsin"), states.Doc("Ohio"),
  59. })
  60. if err != nil {
  61. // TODO: Handle error.
  62. }
  63. for _, ds := range docsnaps {
  64. _ = ds // TODO: Use ds.
  65. }
  66. //[ docref.Create
  67. wr, err := ny.Create(ctx, State{
  68. Capital: "Albany",
  69. Population: 19.8,
  70. })
  71. if err != nil {
  72. // TODO: Handle error.
  73. }
  74. fmt.Println(wr)
  75. //]
  76. //[ docref.Set
  77. ca := states.Doc("California")
  78. _, err = ca.Set(ctx, State{
  79. Capital: "Sacramento",
  80. Population: 39.14,
  81. })
  82. //]
  83. //[ docref.Update
  84. _, err = ca.Update(ctx, []firestore.Update{{Path: "capital", Value: "Sacramento"}})
  85. //]
  86. //[ docref.Delete
  87. _, err = ny.Delete(ctx)
  88. //]
  89. //[ LUT-precond
  90. docsnap, err = ca.Get(ctx)
  91. if err != nil {
  92. // TODO: Handle error.
  93. }
  94. _, err = ca.Update(ctx,
  95. []firestore.Update{{Path: "capital", Value: "Sacramento"}},
  96. firestore.LastUpdateTime(docsnap.UpdateTime))
  97. //]
  98. //[ WriteBatch
  99. writeResults, err := client.Batch().
  100. Create(ny, State{Capital: "Albany"}).
  101. Update(ca, []firestore.Update{{Path: "capital", Value: "Sacramento"}}).
  102. Delete(client.Doc("States/WestDakota")).
  103. Commit(ctx)
  104. //]
  105. _ = writeResults
  106. //[ Query
  107. q := states.Where("pop", ">", 10).OrderBy("pop", firestore.Desc)
  108. //]
  109. //[ Documents
  110. iter := q.Documents(ctx)
  111. for {
  112. doc, err := iter.Next()
  113. if err == iterator.Done {
  114. break
  115. }
  116. if err != nil {
  117. // TODO: Handle error.
  118. }
  119. fmt.Println(doc.Data())
  120. }
  121. //]
  122. //[ CollQuery
  123. iter = client.Collection("States").Documents(ctx)
  124. //]
  125. }
  126. func txn() {
  127. var ctx context.Context
  128. var client *firestore.Client
  129. //[ Transaction
  130. ny := client.Doc("States/NewYork")
  131. err := client.RunTransaction(ctx, func(ctx context.Context, tx *firestore.Transaction) error {
  132. doc, err := tx.Get(ny) // tx.Get, NOT ny.Get!
  133. if err != nil {
  134. return err
  135. }
  136. pop, err := doc.DataAt("pop")
  137. if err != nil {
  138. return err
  139. }
  140. return tx.Update(ny, []firestore.Update{{Path: "pop", Value: pop.(float64) + 0.2}})
  141. })
  142. if err != nil {
  143. // TODO: Handle error.
  144. }
  145. //]
  146. }