-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate-adv.go
75 lines (60 loc) · 1.69 KB
/
update-adv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"context"
"time"
"cloud.google.com/go/firestore"
"github.com/sanity-io/litter"
"google.golang.org/genproto/googleapis/type/latlng"
)
type Customer struct {
ID string
Name string
// Location is a point on the earth
// it MUST be a pointer to saved as the right type in Firestore
Location *latlng.LatLng
}
func main() {
ctx := context.Background()
client, cerr := firestore.NewClient(ctx, "firestore-and-go")
if cerr != nil {
panic(cerr)
}
docRef := client.Collection("customers").Doc("weave")
// START UPDATE OMIT
updates := []firestore.Update{
// Updates can be by path
{Path: "Name", Value: "Weave"},
// ...even when adding new fields
{Path: "PatchedAt", Value: time.Now()},
// ...even with new nested fields
{Path: "Hiring.URL", Value: "https://boards.greenhouse.io/weavehq"},
{Path: "Hiring.ReferredBy", Value: "Carson Anderson"},
// Paths that contain any of ".˜*/[]" must be wrapped in a custom type
{FieldPath: firestore.FieldPath{"[my]", "sp.ecial", "/key**"}, Value: "Company"},
}
// apply all updates to the targeted doc
_, _ = docRef.Update(ctx, updates)
// In Go, you MUST get as a map to not drop unknown fields
docSnapshot, _ := docRef.Get(context.Background())
litter.Dump(docSnapshot.Data())
// END UPDATE OMIT
}
func getAndDump(docRef *firestore.DocumentRef) {
cust := &Customer{}
docSnapshot, err := docRef.Get(context.Background())
if err != nil {
panic(err)
}
err = docSnapshot.DataTo(cust)
if err != nil {
panic(err)
}
litter.Dump(cust)
}
func getAndDumpData(docRef *firestore.DocumentRef) {
docSnapshot, err := docRef.Get(context.Background())
if err != nil {
panic(err)
}
litter.Dump(docSnapshot.Data())
}