-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.go
147 lines (128 loc) · 2.96 KB
/
utils.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
package bson
import (
"reflect"
"sort"
"strings"
"unicode"
)
func must[T any](v T, err error) T {
if err != nil {
panic(err)
}
return v
}
type pairRefl struct {
Key string
Val any
}
type docRefl []pairRefl
func (doc docRefl) Len() int { return len(doc) }
func (doc docRefl) Less(i, j int) bool { return doc[i].Key < doc[j].Key }
func (doc docRefl) Swap(i, j int) { doc[i], doc[j] = doc[j], doc[i] }
func sortPairRefl(doc docRefl) {
// TODO(cristaloleg): 20 might be too high/low.
if len(doc) <= 20 {
// for i := 1; i < len(ss); i++ {
// for j := i; j > 0 && ss[j] < ss[j-1]; j-- {
// ss[j], ss[j-1] = ss[j-1], ss[j]
// }
// }
// below is the code above, but (almost) without bound checks
for i := 1; i < len(doc); i++ {
for j := i; j > 0; j-- {
if doc[j].Key >= doc[j-1].Key {
break
}
doc[j], doc[j-1] = doc[j-1], doc[j]
}
}
} else {
sort.Sort(doc)
}
}
func walkStruct(dict docRefl, v reflect.Value) (docRefl, error) {
t := v.Type()
for i := 0; i < t.NumField(); i++ {
strField := t.Field(i)
field := v.FieldByIndex(strField.Index)
if !field.CanInterface() || isNil(field) {
continue
}
tag, ok := fieldTag(strField, field)
if !ok {
continue
}
if tag == "" && strField.Anonymous &&
strField.Type.Kind() == reflect.Struct {
var err error
dict, err = walkStruct(dict, field)
if err != nil {
return nil, err
}
} else {
dict = append(dict, pairRefl{Key: tag, Val: field})
}
}
return dict, nil
}
func fieldTag(field reflect.StructField, v reflect.Value) (string, bool) {
tag := field.Tag.Get("bson")
var opts string
switch {
case tag == "":
return field.Name, true
case tag == "-":
return "", false
default:
if idx := strings.Index(tag, ","); idx != -1 {
tag, opts = tag[:idx], tag[idx:]
}
}
switch {
case strings.Contains(opts, ",omitempty") && isZero(v):
return "", false
case !isValidTag(tag):
return field.Name, true
default:
return tag, true
}
}
func isValidTag(key string) bool {
if key == "" {
return false
}
for _, c := range key {
if c != ' ' && c != '$' && c != '-' && c != '_' && c != '.' &&
!unicode.IsLetter(c) && !unicode.IsDigit(c) {
return false
}
}
return true
}
func isNil(v reflect.Value) bool {
switch v.Kind() {
case reflect.Interface, reflect.Ptr:
return v.IsNil()
default:
return false
}
}
func isZero(v reflect.Value) bool {
switch v.Kind() {
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
return v.Len() == 0
case reflect.Ptr, reflect.Interface:
return v.IsNil()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return v.Float() == 0
case reflect.Bool:
return !v.Bool()
// TODO(cristaloleg): supporting reflect.Struct might be hard.
default:
return false
}
}