-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnullify.go
More file actions
236 lines (203 loc) · 6.7 KB
/
nullify.go
File metadata and controls
236 lines (203 loc) · 6.7 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package nullify
import (
"encoding/json"
"reflect"
)
// Nullify returns the pointer version of any input, e.g. string becomes *string, int becomes *int
// and for a struct, the pointer version of the fields is returned as well. E.g.
//
// type Person struct {
// Name string
// }
//
// will be returned as
//
// type Person struct {
// Name *string
// }
//
// with `p := Person{}`, Nullify(p) returns a pointer to Person.
//
// This is especially useful in e.g. validating JSON input, see example.
func Nullify(obj any, options ...option) any {
typeOf := reflect.TypeOf(obj)
if typeOf == nil {
return nil // guard for nil interface{}
}
// default config
cfg := config{
bytesAsString: false,
nullifyArrayElem: true,
nullifySliceElem: true,
nullifyMapElem: true,
nullifyMapKey: true,
nullifyMarshalJson: false,
nullifyUnmarshalJson: false,
}
// process options
for _, opt := range options {
cfg = opt.update(cfg)
}
val := ptr(typeOf, cfg)
return reflect.New(val.Elem()).Interface()
}
// JsonOptions is a curated list of options that can be used for json.Marshal, json.Unmarshal.
// Use by spreading it onto the nullify function: `Nullify(t, JsonOptions...)
var JsonOptions = []option{
BytesAsString{Value: true},
NullifyMapKey{Value: false},
NullifyMapElem{Value: false},
NullifySliceElem{Value: false},
NullifyArrayElem{Value: false},
NullifyMarshalJson{Value: false},
NullifyUnmarshalJson{Value: false},
}
// config determines the behavior of the ptr function
type config struct {
bytesAsString bool
nullifyArrayElem bool
nullifySliceElem bool
nullifyMapElem bool
nullifyMapKey bool
nullifyMarshalJson bool
nullifyUnmarshalJson bool
}
// option functionally updates the ptr function
type option interface {
update(cfg config) config
}
// BytesAsString if true (default false) processes []uint8, []byte as string
// this is especially useful in json.Marshal, json.Unmarshal cases
type BytesAsString struct {
Value bool
}
func (o BytesAsString) update(cfg config) config {
cfg.bytesAsString = o.Value
return cfg
}
// NullifyArrayElem if true (default false) doesn't nullify the array element, e.g. []any instead of []*any
type NullifyArrayElem struct {
Value bool
}
func (o NullifyArrayElem) update(cfg config) config {
cfg.nullifyArrayElem = o.Value
return cfg
}
// NullifySliceElem if true (default false) doesn't nullify the slice element, e.g. []any instead of []*any
type NullifySliceElem struct {
Value bool
}
func (o NullifySliceElem) update(cfg config) config {
cfg.nullifySliceElem = o.Value
return cfg
}
// NullifyMapElem if true (default true) nullifies the map element, e.g. map[any]*any instead of map[any]any
type NullifyMapElem struct {
Value bool
}
func (o NullifyMapElem) update(cfg config) config {
cfg.nullifyMapElem = o.Value
return cfg
}
// NullifyMapKey if true (default true) nullifies the map element, e.g. map[*any]any instead of map[any]any
type NullifyMapKey struct {
Value bool
}
func (o NullifyMapKey) update(cfg config) config {
cfg.nullifyMapKey = o.Value
return cfg
}
// NullifyMarshalJson if true (default false) nullifies elements which implement the json.Marshaller interface
type NullifyMarshalJson struct {
Value bool
}
func (o NullifyMarshalJson) update(cfg config) config {
cfg.nullifyMarshalJson = o.Value
return cfg
}
// NullifyUnmarshalJson if true (default false) nullifies elements which implement the json.Unmarshaller interface
type NullifyUnmarshalJson struct {
Value bool
}
func (o NullifyUnmarshalJson) update(cfg config) config {
cfg.nullifyUnmarshalJson = o.Value
return cfg
}
// jsonMarshaler json.Marshaler type
var jsonMarshaler = reflect.TypeOf((*json.Marshaler)(nil)).Elem()
// jsonUnmarshaler json.Unmarshaler type
var jsonUnmarshaler = reflect.TypeOf((*json.Marshaler)(nil)).Elem()
// ptr recursively transforms the `reflect.Type` to a pointer kind.
func ptr(t reflect.Type, cfg config) reflect.Type {
if !cfg.nullifyMarshalJson && t.Implements(jsonMarshaler) {
return reflect.PointerTo(t)
}
if !cfg.nullifyUnmarshalJson && t.Implements(jsonUnmarshaler) {
return reflect.PointerTo(t)
}
switch t.Kind() {
case reflect.Struct:
structFields := make([]reflect.StructField, t.NumField())
for i := range structFields {
structFields[i] = t.Field(i)
structFields[i].Type = ptr(structFields[i].Type, cfg)
}
return reflect.PointerTo(reflect.StructOf(structFields))
case reflect.Array:
if cfg.bytesAsString && (t.Elem().Kind() == reflect.Uint8 || (t.Elem().Kind() == reflect.Pointer && t.Elem().Elem().Kind() == reflect.Uint8)) {
elemType := reflect.PointerTo(reflect.TypeOf(""))
return elemType
}
elemType := ptr(t.Elem(), cfg)
if cfg.nullifyArrayElem && elemType.Kind() != reflect.Pointer {
elemType = reflect.PointerTo(elemType)
}
if !cfg.nullifyArrayElem && elemType.Kind() == reflect.Pointer {
elemType = elemType.Elem()
}
return reflect.PointerTo(reflect.ArrayOf(t.Len(), elemType))
case reflect.Slice:
if cfg.bytesAsString && (t.Elem().Kind() == reflect.Uint8 || (t.Elem().Kind() == reflect.Pointer && t.Elem().Elem().Kind() == reflect.Uint8)) {
elemType := reflect.TypeOf("")
if cfg.nullifySliceElem {
elemType = reflect.PointerTo(elemType)
}
return elemType
}
elemType := ptr(t.Elem(), cfg)
if cfg.nullifySliceElem && elemType.Kind() != reflect.Pointer {
elemType = reflect.PointerTo(elemType)
}
if !cfg.nullifySliceElem && elemType.Kind() == reflect.Pointer {
elemType = elemType.Elem()
}
return reflect.PointerTo(reflect.SliceOf(elemType))
case reflect.Map:
elemType := ptr(t.Elem(), cfg)
if cfg.nullifyMapElem && elemType.Kind() != reflect.Pointer {
elemType = reflect.PointerTo(elemType)
}
if !cfg.nullifyMapElem && elemType.Kind() == reflect.Pointer {
elemType = elemType.Elem()
}
keyType := ptr(t.Key(), cfg)
if cfg.nullifyMapKey && keyType.Kind() != reflect.Pointer {
keyType = reflect.PointerTo(keyType)
}
if !cfg.nullifyMapKey && keyType.Kind() == reflect.Pointer {
keyType = keyType.Elem()
}
return reflect.PointerTo(reflect.MapOf(keyType, elemType))
// primitive types, just return the pointer value
case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128, reflect.String:
return reflect.PointerTo(t)
// recursively follow pointer and return the non-pointer version, then call ptr on that to resolve to a 1-depth pointer
case reflect.Pointer:
for ok := t.Kind() == reflect.Pointer; ok; ok = t.Kind() == reflect.Pointer {
t = t.Elem()
}
return ptr(t, cfg)
default:
return reflect.PointerTo(t)
}
}