forked from treeder/firetils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.go
267 lines (251 loc) · 8.02 KB
/
data.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package firetils
import (
"context"
"reflect"
"cloud.google.com/go/firestore"
"github.com/treeder/gotils/v2"
"google.golang.org/api/iterator"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// Save sets userID if it's in context, updates timestamps, calls PreSave(), then return object with Ref and ID set.
func Save(ctx context.Context, client *firestore.Client, collection string, v StoredAndStamped) (StoredAndStamped, error) {
return Save2(ctx, client, collection, v, nil)
}
type SaveOptions struct {
SkipOwned bool // won't set userID on the saved object
}
// Save2 same as Save, but with options
func Save2(ctx context.Context, client *firestore.Client, collection string, v StoredAndStamped, opts *SaveOptions) (StoredAndStamped, error) {
if opts == nil {
opts = &SaveOptions{}
}
UpdateTimeStamps(v)
if !opts.SkipOwned {
owned, ok := v.(OwnedI)
if ok {
if owned.GetUserID() == "" {
// only set it if it's empty
SetOwned(ctx, owned)
}
}
}
n := reflect.ValueOf(v)
preSave := n.MethodByName("PreSave")
if preSave.IsValid() {
// fmt.Println("CALLING AfterLoad")
_ = preSave.Call([]reflect.Value{reflect.ValueOf(ctx)})
// TODO: check last value returned for an error!
}
var err error
var ref *firestore.DocumentRef
if v.GetRef() != nil {
ref = v.GetRef()
_, err = ref.Set(ctx, v)
} else if v.GetID() != "" {
// user set the ID
ref = client.Collection(collection).Doc(v.GetID())
_, err = ref.Set(ctx, v) // TODO should this be changed to create so we don't accidently overwrite something?
} else {
// make new ID
ref, _, err = client.Collection(collection).Add(ctx, v)
}
if err != nil {
return nil, gotils.C(ctx).Errorf("Failed to store object: %v", err)
}
v.SetRef(ref)
v.SetID(ref.ID)
return v, nil
}
// Merge does a Set with MergeAll
// Doesn't feel right...
// func Merge(ctx context.Context, client *firestore.Client, collection, id string, v map[string]interface{}) {
// // UpdateTimeStamps(v)
// v["updatedAt"] = time.Now()
// var err error
// var ref *firestore.DocumentRef
// ref = client.Collection(collection).Doc(id)
// _, err = ref.Set(ctx, v, firestore.MergeAll)
// if err != nil {
// return nil, gotils.C(ctx).Errorf("Failed to store object: %v", err)
// }
// }
// Collection returns CollectionRef by name
func Collection(client *firestore.Client, name string) *firestore.CollectionRef {
return client.Collection(name)
}
// GetByID get a doc by ID
func GetByID(ctx context.Context, client *firestore.Client, collectionName, id string, t StoredAndStamped) error {
ref := Collection(client, collectionName).Doc(id)
return GetByRef(ctx, client, ref, t)
}
// GetByID2 same as GetByID, but returns new object
func GetByID2(ctx context.Context, client *firestore.Client, collectionName, id string, v StoredAndStamped) (StoredAndStamped, error) {
t := reflect.TypeOf(v)
// fmt.Printf("DATA: %+v\n", doc.Data())
n := reflect.New(t.Elem())
v2 := n.Interface()
err := GetByID(ctx, client, collectionName, id, v2.(StoredAndStamped))
if err != nil {
return nil, err
}
return v2.(StoredAndStamped), nil
}
// NEED TO DO WHAT WE're DOING IN GET BY QUERY BELOW, and return the object, rather than filling in like DataTo
// GetByID get a doc by ID
// func GetByIDCached(ctx context.Context, collectionName, id string, t FirestoredI) error {
// ckey := fmt.Sprintf("%T-%v", t, id)
// fmt.Printf("XXX CKEY: %v\n", ckey)
// c2, _ := Cache.Get(ckey)
// if c2 != nil {
// user = c2.(*mmodels.User)
// }
// err := GetByID(ctx, collectionName, userID, user)
// if err != nil {
// return err
// }
// Cache.Set(ckey, user, 5)
// }
// GetByRef generic way to get a document
func GetByRef(ctx context.Context, client *firestore.Client, ref *firestore.DocumentRef, t StoredAndStamped) error {
doc, err := ref.Get(ctx)
if status.Code(err) == codes.NotFound {
return gotils.ErrNotFound
}
if err != nil {
return gotils.C(ctx).Errorf("error in GetByRef: %v", err)
}
err = doc.DataTo(t)
if err != nil {
return gotils.C(ctx).Errorf("error in GetByRef: %v", err)
}
afterLoad(ctx, doc.Ref, t)
return nil
}
// GetOneByQuery generic way to get a document
func GetOneByQuery(ctx context.Context, q firestore.Query, t StoredAndIded) error {
iter := q.Documents(ctx)
doc, err := iter.Next()
if err == iterator.Done {
// gotils.WriteError(w, http.StatusInternalServerError, errors.New("Employee already added."))
return gotils.ErrNotFound
}
if status.Code(err) == codes.NotFound {
// gotils.WriteError(w, http.StatusBadRequest, gotils.ErrNotFound)
return gotils.ErrNotFound
}
if err != nil {
return gotils.C(ctx).Errorf("error in GetOneByQuery: %v", err)
}
err = doc.DataTo(t)
if err != nil {
return gotils.C(ctx).Errorf("error in GetOneByQuery: %v", err)
}
afterLoad(ctx, doc.Ref, t)
return nil
}
// GetOneByQuery2 generic way to get a document
// This one returns a new object.
// Still trying to decide which way I like better...
func GetOneByQuery2(ctx context.Context, q firestore.Query, v StoredAndIded) (StoredAndIded, error) {
t := reflect.TypeOf(v)
// fmt.Printf("DATA: %+v\n", doc.Data())
n := reflect.New(t.Elem())
v2 := n.Interface()
err := GetOneByQuery(ctx, q, v2.(StoredAndStamped))
if err != nil {
return nil, err
}
return v2.(StoredAndStamped), nil
}
// GetAllByQuery generic way to get a list of documents.
// NOTE: this doesn't seem to work well, best to use GetAllByQuery2
// limit restricts how many are returned. <=0 is all
// ret will be filled with the objects
func GetAllByQuery(ctx context.Context, q firestore.Query, limit int, ret []interface{}) error {
// tType := t.Elem()
// ret := []FirestoredI{}
if limit > 0 {
q = q.Limit(limit)
}
iter := q.Documents(ctx)
defer iter.Stop()
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
return gotils.C(ctx).Errorf("error iterating over query items: %v", err)
}
// HERE is how this all works: https://play.golang.org/p/tnsvwelTv4A
t := reflect.TypeOf(ret)
// fmt.Printf("DATA: %+v\n", doc.Data())
n := reflect.New(t.Elem())
v2 := n.Interface()
err = doc.DataTo(v2)
if err != nil {
return gotils.C(ctx).Errorf("error on datato: %v", err)
}
fstored := v2.(StoredAndStamped)
afterLoad(ctx, doc.Ref, fstored)
ret = append(ret, fstored)
}
return nil
}
// GetAllByQuery2 generic way to get a list of documents, by just passing in the type.
// limit restricts how many we return. <=0 is all
// v is an instance of the type of object to be returned, it will not be modified or updated.
func GetAllByQuery2(ctx context.Context, q firestore.Query, v StoredAndStamped) ([]StoredAndStamped, error) {
// tType := t.Elem()
ret := []StoredAndStamped{}
// if limit > 0 {
// q = q.Limit(limit)
// }
iter := q.Documents(ctx)
defer iter.Stop()
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, gotils.C(ctx).Errorf("error iterating over query items: %v", err)
}
// HERE is how this all works: https://play.golang.org/p/tnsvwelTv4A
t := reflect.TypeOf(v)
// fmt.Printf("DATA: %+v\n", doc.Data())
n := reflect.New(t.Elem())
v2 := n.Interface()
err = doc.DataTo(v2)
if err != nil {
return nil, gotils.C(ctx).Errorf("error on DataTo: %v", err)
}
fstored := v2.(StoredAndStamped)
afterLoad(ctx, doc.Ref, fstored)
ret = append(ret, fstored)
}
return ret, nil
}
func afterLoad(ctx context.Context, ref *firestore.DocumentRef, v StoredAndIded) {
v.SetRef(ref)
v.SetID(ref.ID)
// fmt.Printf("id: %v, status: %v\n", t.Ref.ID, t.Status)
// could change this to another interface with PreSave and PostSave (or BeforeSave and AfterSave)
n := reflect.ValueOf(v)
afterLoad := n.MethodByName("AfterLoad")
if afterLoad.IsValid() {
// fmt.Println("CALLING AfterLoad")
afterLoad.Call([]reflect.Value{reflect.ValueOf(ctx)})
// TODO: check last value returned for an error!
}
}
// GetByID get a doc by ID
func Delete(ctx context.Context, client *firestore.Client, collectionName, id string) error {
ref := Collection(client, collectionName).Doc(id)
_, err := ref.Delete(ctx)
if err != nil {
return err
}
return nil
}