-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfirestore_utils.go
More file actions
56 lines (46 loc) · 1.21 KB
/
firestore_utils.go
File metadata and controls
56 lines (46 loc) · 1.21 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
package fscli
import (
"context"
"fmt"
"strings"
"cloud.google.com/go/firestore"
)
func findAllCollections(ctx context.Context, fs *firestore.Client, baseDoc string) ([]string, error) {
itr, err := getCollectionsIterator(ctx, fs, baseDoc)
if err != nil {
return nil, err
}
return iterateAllCollections(itr)
}
func getCollectionsIterator(ctx context.Context, fs *firestore.Client, baseDoc string) (*firestore.CollectionIterator, error) {
if baseDoc == "" {
return fs.Collections(ctx), nil
}
lastSlash := strings.LastIndex(baseDoc, "/")
if lastSlash == -1 {
return nil, fmt.Errorf("invalid path: %s", baseDoc)
}
collection := baseDoc[:lastSlash]
docId := baseDoc[lastSlash+1:]
return fs.Collection(collection).Doc(docId).Collections(ctx), nil
}
func iterateAllCollections(itr *firestore.CollectionIterator) ([]string, error) {
cols, err := itr.GetAll()
if err != nil {
return nil, err
}
return getCollectionIds(cols), nil
}
func getCollectionIds(cols []*firestore.CollectionRef) []string {
ids := make([]string, 0)
for _, col := range cols {
ids = append(ids, col.ID)
}
return ids
}
func normalizeFirestorePath(s string) string {
if strings.HasPrefix(s, "/") {
return s[1:]
}
return s
}