-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirestore.go
158 lines (148 loc) · 3.7 KB
/
firestore.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
package firebase
import (
"context"
"fmt"
firebase "firebase.google.com/go"
"github.com/ziphttpd/ziphttpd.com/util"
"google.golang.org/api/iterator"
)
// Collection はfirestoreのコレクションを表します。
type Collection struct {
collection string
}
// NewCollection は Collecction のインスタンスを生成します。
func NewCollection(collection string) *Collection {
return &Collection{collection: collection}
}
// SetMap は Collection にマップを保存します。
func (f *Collection) SetMap(key string, data map[string]string) error {
util.LogOutput(fmt.Sprintf("key:%s, value:%+v", key, data))
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
app, err := firebase.NewApp(ctx, nil, GetOption())
if err != nil {
util.LogOutput(err.Error())
return err
}
client, err := app.Firestore(ctx)
if err != nil {
util.LogOutput(err.Error())
return err
}
defer client.Close()
// 書き出し
_, err = client.Collection(f.collection).Doc(key).Set(ctx, data)
if err != nil {
util.LogOutput(err.Error())
return err
}
util.LogOutput("done")
return nil
}
// Set は Collection に値を保存します。
func (f *Collection) Set(key, value string) error {
data := map[string]string{}
data["data"] = value
return f.SetMap(key, data)
}
// GetMap は生のドキュメントを取得します。
func (f *Collection) GetMap(key string) (map[string]string, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
app, err := firebase.NewApp(ctx, nil, GetOption())
if err != nil {
return nil, err
}
client, err := app.Firestore(ctx)
if err != nil {
return nil, err
}
defer client.Close()
// 読み出し
dsnap, err := client.Collection(f.collection).Doc(key).Get(ctx)
if err != nil {
return nil, err
}
if dsnap.Exists() == false {
return nil, err
}
data := dsnap.Data()
// map[string]string マップへの変換
ret := map[string]string{}
for subkey, intr := range data {
if value, ok := intr.(string); ok {
ret[subkey] = value
}
}
return ret, nil
}
// Get は Collection でキーから値を検索します。
func (f *Collection) Get(key string) (string, error) {
util.LogOutput(fmt.Sprintf("key:%s", key))
data, err := f.GetMap(key)
if err != nil {
util.LogOutput(err.Error())
return "", err
}
util.LogOutput(fmt.Sprintf("%+v", data))
if txt, ok := data["data"]; ok {
util.LogOutput(fmt.Sprintf("return \"%s\"", txt))
return txt, nil
}
util.LogOutput("return empty")
return "", nil
}
// Delete は Collection からキーを削除します。
func (f *Collection) Delete(key string) error {
util.LogOutput(fmt.Sprintf("key:%s", key))
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
app, err := firebase.NewApp(ctx, nil, GetOption())
if err != nil {
util.LogOutput(err.Error())
return err
}
client, err := app.Firestore(ctx)
if err != nil {
util.LogOutput(err.Error())
return err
}
defer client.Close()
// 削除
_, err = client.Collection(f.collection).Doc(key).Delete(ctx)
if err != nil {
util.LogOutput(err.Error())
return err
}
return err
}
// Keys はキーの一覧を返します。
func (f *Collection) Keys() []string {
ret := []string{}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
app, err := firebase.NewApp(ctx, nil, GetOption())
if err != nil {
util.LogOutput(err.Error())
return ret
}
client, err := app.Firestore(ctx)
if err != nil {
util.LogOutput(err.Error())
return ret
}
defer client.Close()
// キーの取得
di := client.Collection(f.collection).Documents(ctx)
for {
doc, err := di.Next()
if err == iterator.Done {
break
}
if err != nil {
break
}
ret = append(ret, doc.Ref.ID)
}
return ret
}