This repository was archived by the owner on Dec 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc_gen.go
189 lines (164 loc) · 4.41 KB
/
misc_gen.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Code generated by firestore-repo. DO NOT EDIT.
// generated version: 1.12.0
package examples
import (
"reflect"
"strings"
"time"
"cloud.google.com/go/firestore"
"google.golang.org/genproto/googleapis/type/latlng"
)
// SetLastThreeToZero - set the last three digits to zero
func SetLastThreeToZero(t time.Time) time.Time {
return time.Unix(t.Unix(), int64(t.Nanosecond()/1000*1000))
}
// GetOption - option to include logical deletion data
type GetOption struct {
IncludeSoftDeleted bool
}
// DeleteMode - delete mode
type DeleteMode int
const (
DeleteModeSoft DeleteMode = iota + 1 // logical delete mode
DeleteModeHard // physical delete mode
)
// DeleteOption - option to delete logically or physically
// default: physical deletion
// use `DeleteModeSoft` or `DeleteModeHard
type DeleteOption struct {
Mode DeleteMode
}
func isReservedType(value reflect.Value) bool {
switch value.Interface().(type) {
case time.Time, *time.Time,
latlng.LatLng, *latlng.LatLng,
firestore.DocumentRef, *firestore.DocumentRef:
return true
}
return false
}
func tagMap(v interface{}) map[string]string {
rv := reflect.Indirect(reflect.ValueOf(v))
rt := rv.Type()
tags := make(map[string]string)
for i := 0; i < rt.NumField(); i++ {
ft := rt.Field(i)
fv := rv.Field(i)
if ft.Anonymous {
for key, val := range tagMap(fv.Interface()) {
if _, ok := tags[key]; ok {
panic("fields with the same name cannot be used")
}
tags[key] = val
}
continue
}
tag := ft.Name
if firestoreTag, ok := ft.Tag.Lookup("firestore"); ok {
tag = strings.Split(firestoreTag, ",")[0]
}
switch fv.Kind() {
case reflect.Ptr:
ptrType := reflect.PtrTo(fv.Type()).Elem()
fv = reflect.New(ptrType.Elem())
fallthrough
case reflect.Struct:
if isReservedType(fv) {
break
}
for key, value := range tagMap(fv.Interface()) {
compositeKey := strings.Join([]string{tag, key}, ".")
if _, ok := tags[compositeKey]; ok {
panic("fields with the same name cannot be used")
}
compositeValue := strings.Join([]string{tag, value}, ".")
tags[compositeKey] = compositeValue
}
continue
}
if _, ok := tags[ft.Name]; ok {
panic("fields with the same name cannot be used")
}
tags[ft.Name] = tag
}
return tags
}
func updater(v, param interface{}) []firestore.Update {
updates := make([]firestore.Update, 0)
for _, update := range updateBuilder(v, param) {
updates = append(updates, update)
}
return updates
}
func updateBuilder(v, param interface{}) map[string]firestore.Update {
rv := reflect.Indirect(reflect.ValueOf(v))
rt := rv.Type()
pv := reflect.Indirect(reflect.ValueOf(param))
pt := pv.Type()
updateMap := make(map[string]firestore.Update)
for i := 0; i < rt.NumField(); i++ {
ft := rt.Field(i)
fv := rv.Field(i)
if ft.Anonymous {
for key, val := range updateBuilder(fv.Interface(), param) {
if _, ok := updateMap[key]; ok {
panic("fields with the same name cannot be used")
}
updateMap[key] = val
}
continue
}
if _, ok := pt.FieldByName(ft.Name); !ok {
continue
}
path := ft.Name
if firestoreTag, ok := ft.Tag.Lookup("firestore"); ok {
path = strings.Split(firestoreTag, ",")[0]
}
pfv := pv.FieldByName(ft.Name)
switch fv.Kind() {
case reflect.Ptr:
ptrType := reflect.PtrTo(fv.Type()).Elem()
fv = reflect.New(ptrType.Elem())
fallthrough
case reflect.Struct:
if isReservedType(fv) {
break
}
for key, update := range updateBuilder(fv.Interface(), pfv.Interface()) {
update.FieldPath = append(firestore.FieldPath{path}, update.FieldPath...)
fp := make(firestore.FieldPath, len(update.FieldPath))
copy(fp, update.FieldPath)
sp := strings.Split(key, ".")
fieldKey := strings.Join(append(fp[:len(update.FieldPath)-1], sp[len(sp)-1]), ".")
if _, ok := updateMap[fieldKey]; ok {
panic("fields with the same name cannot be used")
}
updateMap[fieldKey] = update
}
continue
}
if _, ok := updateMap[ft.Name]; ok {
panic("fields with the same name cannot be used")
}
var isValid bool
switch pfv.Kind() {
case reflect.Interface, reflect.Ptr:
if !pfv.IsNil() {
isValid = true
}
default:
if !pfv.IsZero() {
isValid = true
}
}
update := firestore.Update{FieldPath: firestore.FieldPath{path}}
if isValid {
update.Value = pfv.Interface()
}
if update.Value != nil {
updateMap[ft.Name] = update
}
}
return updateMap
}