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