-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.go
160 lines (134 loc) · 3.33 KB
/
query.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
package mongox
import (
"errors"
"reflect"
"strings"
"time"
"go.mongodb.org/mongo-driver/v2/bson"
)
// M is a map containing query operators to filter documents.
type M bson.M
// NewM creates a new Filter based on pairs.
// Pairs must be in the form NewF(key1, value1, key2, value2, ...)
func NewM(pairs ...any) M {
return newMapFromPairs(pairs...)
}
// Add adds pairs to the Filter.
func (f M) Add(pairs ...any) M {
addPairs(f, pairs...)
return f
}
// Prepare returns a bson.D representation of the Filter that can be used in a MongoDB query.
func (f M) Prepare() bson.D {
filter := make(bson.D, 0, len(f))
for k, v := range f {
filter = append(filter, bson.E{Key: k, Value: v})
}
return filter
}
// String returns a string representation of the Filter.
func (f M) String() string {
return f.Prepare().String()
}
func newMapFromPairs(pairs ...any) map[string]any {
out := make(map[string]any, len(pairs)/2)
addPairs(out, pairs...)
return out
}
func addPairs(m map[string]any, pairs ...any) {
for i := 0; i < len(pairs); i += 2 {
key, ok := pairs[i].(string)
if ok && i+1 < len(pairs) {
m[key] = pairs[i+1]
}
}
}
func prepareUpdates(upd map[string]any, op string) bson.D {
res := make(bson.D, 0, len(upd))
for k, v := range upd {
res = append(res, bson.E{Key: k, Value: v})
}
return bson.D{bson.E{Key: op, Value: res}}
}
func diffToUpdates(diff any) (bson.D, error) {
upd, err := processDiffStruct(diff, "")
if err != nil {
return nil, err
}
return prepareUpdates(upd, Set), nil
}
func processDiffStruct(diff any, parentField string) (map[string]any, error) {
req := reflect.ValueOf(diff)
if req.Kind() == reflect.Pointer {
req = req.Elem()
}
if req.Kind() != reflect.Struct {
return nil, errors.New("only struct fields are allowed")
}
upd := make(map[string]any)
for n := range req.NumField() {
fieldNameRaw := strings.SplitN(req.Type().Field(n).Tag.Get("bson"), ",", 2)
fieldName := fieldNameRaw[0]
// skip field
if fieldName == "-" {
continue
}
// There is no bson tag, use the actual field name
if fieldName == "" {
fieldName = req.Type().Field(n).Name
}
if parentField != "" {
fieldName = parentField + "." + fieldName
}
field := req.Field(n)
if !field.CanInterface() {
// unexported field
continue
}
kind := field.Kind()
if kind != reflect.Ptr && kind != reflect.Slice && kind != reflect.Map {
// expect pointers or slice/
continue
}
if field.IsNil() {
// nil == no update for field
continue
}
if kind == reflect.Pointer {
// pointer value, not map or slice
field = field.Elem()
}
if val, ok := field.Interface().(bson.ValueMarshaler); ok {
upd[fieldName] = val
continue
}
if val, ok := field.Interface().(bson.Marshaler); ok {
upd[fieldName] = val
continue
}
if field.Kind() == reflect.Struct {
i := field.Interface()
if _, ok := i.(time.Time); ok {
upd[fieldName] = i
continue
}
parentField := fieldName
if len(fieldNameRaw) > 1 && strings.Contains(fieldNameRaw[1], "inline") {
parentField = ""
}
structUpd, err := processDiffStruct(field.Interface(), parentField)
if err != nil {
continue
}
for k, v := range structUpd {
upd[k] = v
}
continue
}
upd[fieldName] = field.Interface()
}
if len(upd) == 0 {
return nil, errors.New("updates are empty")
}
return upd, nil
}