-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexecutor.go
More file actions
127 lines (109 loc) · 2.9 KB
/
executor.go
File metadata and controls
127 lines (109 loc) · 2.9 KB
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
package fscli
import (
"context"
"errors"
"cloud.google.com/go/firestore"
"cloud.google.com/go/firestore/apiv1/firestorepb"
"google.golang.org/api/iterator"
)
type Executor struct {
fs *firestore.Client
}
var ErrInvalidCollection = errors.New("invalid collection")
func NewExecutor(ctx context.Context, fs *firestore.Client) *Executor {
return &Executor{fs}
}
func (exe *Executor) ExecuteQuery(ctx context.Context, op *QueryOperation) ([]*firestore.DocumentSnapshot, error) {
collection := exe.fs.Collection(op.Collection())
if collection == nil {
return nil, ErrInvalidCollection
}
q := collection.Query
for _, filter := range op.filters {
fieldName := filter.FieldName()
value := filter.Value()
if fieldName == FieldDocumentID {
fieldName = firestore.DocumentID
value = toDocRefValue(collection, value)
}
q = q.Where(fieldName, string(filter.Operator()), value)
}
if len(op.selects) > 0 {
q = q.Select(op.selects...)
}
if len(op.orderBys) > 0 {
for _, orderBy := range op.orderBys {
q = q.OrderBy(orderBy.field, firestore.Direction(orderBy.direction))
}
}
if op.limit > 0 {
q = q.Limit(op.limit)
}
itr := q.Documents(ctx)
defer itr.Stop()
docs := make([]*firestore.DocumentSnapshot, 0)
for {
doc, err := itr.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, err
}
docs = append(docs, doc)
}
return docs, nil
}
func (exe *Executor) ExecuteGet(ctx context.Context, op *GetOperation) (*firestore.DocumentSnapshot, error) {
doc, err := exe.fs.Collection(op.Collection()).Doc(op.DocId()).Get(ctx)
if err != nil {
return nil, err
}
return doc, nil
}
func (exe *Executor) ExecuteCount(ctx context.Context, op *CountOperation) (int64, error) {
collection := exe.fs.Collection(op.Collection())
if collection == nil {
return 0, ErrInvalidCollection
}
q := collection.Query
for _, filter := range op.filters {
fieldName := filter.FieldName()
value := filter.Value()
if fieldName == FieldDocumentID {
fieldName = firestore.DocumentID
value = toDocRefValue(collection, value)
}
q = q.Where(fieldName, string(filter.Operator()), value)
}
aggrQ := q.NewAggregationQuery().WithCount("all")
results, err := aggrQ.Get(ctx)
if err != nil {
return 0, err
}
count, ok := results["all"]
if !ok {
return 0, errors.New("invalid aggregation result")
}
v := count.(*firestorepb.Value)
return v.GetIntegerValue(), nil
}
func (exe *Executor) ExecuteListCollections(ctx context.Context, cmd *MetacommandListCollections) ([]string, error) {
return findAllCollections(ctx, exe.fs, cmd.baseDoc)
}
func toDocRefValue(collection *firestore.CollectionRef, value any) any {
switch v := value.(type) {
case string:
return collection.Doc(v)
case []any:
refs := make([]*firestore.DocumentRef, len(v))
for i, item := range v {
if s, ok := item.(string); ok {
refs[i] = collection.Doc(s)
}
}
return refs
default:
return value
}
}