/
firestore.go
109 lines (100 loc) · 2.82 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
package cloudfunctions
import (
"context"
"fmt"
"log"
"reflect"
"strconv"
"time"
"cloud.google.com/go/firestore"
"github.com/zond/corpulentpangolin/backend/data"
)
var FirestoreClient *firestore.Client
func firestoreInit() {
var err error
if FirestoreClient, err = firestore.NewClient(context.Background(), ProjectID); err != nil {
log.Panicf("Unable to initialize Firestore client: %v", err)
}
}
type FirestoreEvent struct {
OldValue FirestoreValue `json:"oldValue"`
Value FirestoreValue `json:"value"`
UpdateMask struct {
FieldPaths []string `json:"fieldPaths"`
} `json:"updateMask"`
}
type FirestoreValue struct {
CreateTime time.Time `json:"createTime"`
Name string `json:"name"`
UpdateTime time.Time `json:"updateTime"`
Fields map[string]interface{} `json:"fields"`
}
func (f *FirestoreValue) Path() string {
return data.NameToPath(f.Name)
}
func (f *FirestoreValue) assignFirestoreValue(val reflect.Value, fv map[string]interface{}) error {
switch val.Type().Kind() {
case reflect.String:
val.SetString(fmt.Sprint(fv["stringValue"]))
case reflect.Bool:
val.SetBool(fmt.Sprint(fv["booleanValue"]) == "true")
case reflect.Float64:
strVal, found := fv["doubleValue"]
if !found {
strVal, found = fv["floatValue"]
}
if !found {
strVal = fv["integerValue"]
}
f, err := strconv.ParseFloat(fmt.Sprint(strVal), 64)
if err != nil {
return err
}
val.SetFloat(f)
case reflect.Uint64:
i, err := strconv.ParseInt(fmt.Sprint(fv["integerValue"]), 10, 64)
if err != nil {
return err
}
val.SetUint(uint64(i))
case reflect.Int64:
i, err := strconv.ParseInt(fmt.Sprint(fv["integerValue"]), 10, 64)
if err != nil {
return err
}
val.SetInt(int64(i))
case reflect.Slice:
if av, found := fv["arrayValue"]; found {
if v, found := av.(map[string]interface{})["values"]; found {
newSlice := reflect.MakeSlice(val.Type(), 0, 0)
val.Set(newSlice)
for _, e := range v.([]interface{}) {
newValPtr := reflect.New(val.Type().Elem())
if err := f.assignFirestoreValue(newValPtr.Elem(), e.(map[string]interface{})); err != nil {
return err
}
val.Set(reflect.Append(val, newValPtr.Elem()))
}
}
}
default:
return fmt.Errorf("Unknown value type %v", val)
}
return nil
}
func (f *FirestoreValue) Unserialize(i interface{}) error {
val := reflect.ValueOf(i)
if val.Kind() != reflect.Ptr && val.Elem().Kind() != reflect.Struct {
return fmt.Errorf("%#v is not pointer to a struct", i)
}
structType := val.Type().Elem()
for idx := 0; idx < structType.NumField(); idx++ {
field := structType.Field(idx)
if fieldVal, found := f.Fields[field.Name]; found {
if err := f.assignFirestoreValue(val.Elem().Field(idx), fieldVal.(map[string]interface{})); err != nil {
return err
}
}
}
return nil
}