This repository has been archived by the owner on Jun 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathfirestoreDocument.go
118 lines (101 loc) · 3.13 KB
/
firestoreDocument.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
package gcp
import (
"context"
"encoding/json"
"time"
"cloud.google.com/go/firestore"
"github.com/puppetlabs/wash/plugin"
)
type firestoreDocument struct {
plugin.EntryBase
client *firestore.Client
path string
data map[string]interface{}
}
type firestoreDocumentMetadata struct {
CreateTime time.Time `json:"CreateTime"`
UpdateTime time.Time `json:"UpdateTime"`
ReadTime time.Time `json:"ReadTime"`
Data map[string]interface{} `json:"Data"`
}
func newFirestoreDocument(client *firestore.Client, parent string, snapshot *firestore.DocumentSnapshot) *firestoreDocument {
doc := &firestoreDocument{
EntryBase: plugin.NewEntry(snapshot.Ref.ID),
client: client,
path: firestorePath(parent, snapshot.Ref.ID),
data: snapshot.Data(),
}
metadata := firestoreDocumentMetadata{
snapshot.CreateTime,
snapshot.UpdateTime,
snapshot.ReadTime,
snapshot.Data(),
}
doc.SetPartialMetadata(metadata).
Attributes().
SetCrtime(snapshot.CreateTime).
SetCtime(snapshot.UpdateTime).
SetMtime(snapshot.UpdateTime)
return doc
}
func (doc *firestoreDocument) List(ctx context.Context) ([]plugin.Entry, error) {
colls, err := doc.client.Doc(doc.path).Collections(ctx).GetAll()
if err != nil {
return nil, err
}
dataJSON, err := newFirestoreDocumentDataJSON(doc.data)
if err != nil {
return nil, err
}
collEntries := toCollectionEntries(doc.client, doc.path, colls)
return append([]plugin.Entry{dataJSON}, collEntries...), nil
}
func (doc *firestoreDocument) Delete(ctx context.Context) (bool, error) {
_, err := doc.client.Doc(doc.path).Delete(ctx)
return true, err
}
func (doc *firestoreDocument) Schema() *plugin.EntrySchema {
return plugin.NewEntrySchema(doc, "document").
SetPartialMetadataSchema(firestoreDocumentMetadata{}).
SetDescription(firestoreDocumentDescription)
}
func (doc *firestoreDocument) ChildSchemas() []*plugin.EntrySchema {
return []*plugin.EntrySchema{
(&firestoreDocumentDataJSON{}).Schema(),
(&firestoreCollection{}).Schema(),
}
}
const firestoreDocumentDescription = `
This is a Firestore document. See the 'firestore' directory's docs for
more details on why we have this kind of entry.
`
type firestoreDocumentDataJSON struct {
plugin.EntryBase
bytes []byte
}
func newFirestoreDocumentDataJSON(data map[string]interface{}) (*firestoreDocumentDataJSON, error) {
dataBytes, err := json.MarshalIndent(data, "", " ")
if err != nil {
// This should never happen
return nil, err
}
dataEntry := &firestoreDocumentDataJSON{
EntryBase: plugin.NewEntry("data.json"),
bytes: dataBytes,
}
dataEntry.DisableDefaultCaching()
return dataEntry, nil
}
func (data *firestoreDocumentDataJSON) Read(ctx context.Context) ([]byte, error) {
return data.bytes, nil
}
func (data *firestoreDocumentDataJSON) Schema() *plugin.EntrySchema {
return plugin.NewEntrySchema(data, "data.json").
IsSingleton().
SetDescription(firestoreDocumentDataJSONDescription)
}
const firestoreDocumentDataJSONDescription = `
This is a Firestore document's data as pretty-printed JSON. See the
'firestore' directory's docs for more details on why we have this
kind of entry.
`