/
array.go
95 lines (73 loc) · 2.7 KB
/
array.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
package document
import (
"cloud.google.com/go/firestore"
"context"
"github.com/remychantenay/fuego/document/internal"
)
// ArrayField provides the necessary to interact with a Firestore document field of type Array.
type ArrayField interface {
// Retrieve returns the value of a specific field containing an array.
//
// note : the returned data will require a type assertion.
Retrieve(ctx context.Context) ([]interface{}, error)
// Append will append the provided data to the existing data (if any) of an Array field.
Append(ctx context.Context, data []interface{}) error
// Override will override the existing data (if any) of an Array field.
// Note: this is the default behaviour with Firestore.
Override(ctx context.Context, data []interface{}) error
}
// Array represents a document field of type Array.
type Array struct {
// Document is the underlying document (incl. ID and ref).
Document Document
// Name is the name of the field.
Name string
firestore *firestore.Client
}
// Retrieve returns the content of a specific field for a given document.
// values, err := fuego.Document("users", "jsmith").Array("Address").Retrieve(ctx)
func (f *Array) Retrieve(ctx context.Context) ([]interface{}, error) {
value, err := internal.RetrieveFieldValue(ctx, f.Document.GetDocumentRef(), f.Name)
if err != nil {
return nil, err
}
return value.([]interface{}), nil
}
// Override will override the existing data (if any) of an Array field.
// values, err := fuego.Document("users", "jsmith").Array("Address").Override(ctx, []interface{}{"New Street", "New Building"})
func (f *Array) Override(ctx context.Context, data []interface{}) error {
ref := f.Document.GetDocumentRef()
m := map[string]interface{}{
f.Name: data,
}
if f.Document.InBatch() {
f.Document.Batch().Set(ref, m, firestore.MergeAll)
return nil
}
_, err := ref.Set(ctx, m, firestore.MergeAll)
return err
}
// Append will append the provided data to the existing data (if any) of an Array field.
//
// The update will be executed inside a transaction.
// values, err := fuego.Document("users", "jsmith").Array("Address").Append(ctx, []interface{}{"More info"})
func (f *Array) Append(ctx context.Context, data []interface{}) error {
return f.firestore.RunTransaction(ctx, func(ctx context.Context, tx *firestore.Transaction) error {
document, err := tx.Get(f.Document.GetDocumentRef())
if err != nil {
return err
}
value, err := document.DataAt(f.Name)
if err != nil {
return err
}
value = append(value.([]interface{}), data...)
err = tx.Set(f.Document.GetDocumentRef(), map[string]interface{}{
f.Name: value,
}, firestore.MergeAll)
if err != nil {
return err
}
return nil // Success, no errors
})
}