-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstruct.go
56 lines (52 loc) · 1.58 KB
/
struct.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
package json
import (
"fmt"
"reflect"
)
// checkStructField checks:
// - optional and nullable tags are not used with omitempty tag
// - optional and nullable fields have enough indirection to represent optional and nullable values
func checkStructField(structType reflect.Type, f *field) error {
requiredIndirectLevel := 0
if f.optional {
requiredIndirectLevel++
if f.omitEmpty {
return fmt.Errorf("json: field %q cannot have both omitempty and optional tags", f.name)
}
}
if f.nullable {
requiredIndirectLevel++
if f.omitEmpty {
return fmt.Errorf("json: field %q cannot have both omitempty and nullable tags", f.name)
}
}
if requiredIndirectLevel == 0 {
return nil // no required indirection for optional/nullable handling
}
// Find the field.
fieldType := structType
for _, i := range f.index {
if fieldType.Kind() == reflect.Ptr {
fieldType = fieldType.Elem()
}
fieldType = fieldType.Field(i).Type
}
// Check if the field has enough indirection.
ft := fieldType
for ft.Kind() == reflect.Ptr && requiredIndirectLevel > 0 {
ft = ft.Elem()
requiredIndirectLevel--
}
if requiredIndirectLevel > 0 {
if f.optional && f.nullable {
return fmt.Errorf("json: optional nullable field %q requires 2+ levels of indirection, type = %q", f.name, fieldType.String())
}
if f.optional {
return fmt.Errorf("json: optional field %q requires 1+ levels of indirection, type = %q", f.name, fieldType.String())
}
if f.nullable {
return fmt.Errorf("json: nullable field %q requires 1+ levels of indirection, type = %q", f.name, fieldType.String())
}
}
return nil
}