Debian Codegraph @

internal/encoder/omit_zero.go

raw
1 package encoder
2
3 import (
4 "reflect"
5 "unsafe"
6
7 "github.com/goccy/go-json/internal/runtime"
8 )
9
10 // ZeroKind is what makes the value of a field zero for omitzero, as encoding/json decides it: by the IsZero
11 // method of the type if it has one, and else by reflect.Value.IsZero.
12 type ZeroKind uint8
13
14 const (
15 ZeroNever ZeroKind = iota
16 // ZeroBits8 to ZeroBits64 are a value of 1 to 8 bytes whose bits are all zero: a bool, an integer,
17 // a pointer, a map...
18 ZeroBits8
19 ZeroBits16
20 ZeroBits32
21 ZeroBits64
22 // ZeroFloat32 and ZeroFloat64 are a float which is 0 or -0: reflect.Value.IsZero compares a float by its
23 // value.
24 ZeroFloat32
25 ZeroFloat64
26 ZeroString // a string of no byte
27 ZeroNilSlice // a nil slice
28 ZeroNilInterface // a nil interface value
29 ZeroReflect // a struct, an array or a complex number, as reflect.Value.IsZero decides
30 ZeroMethod // a type which has IsZero, which decides it
31 )
32
33 type isZeroer interface {
34 IsZero() bool
35 }
36
37 var isZeroerType = reflect.TypeOf((*isZeroer)(nil)).Elem()
38
39 // zeroKindOf returns what makes a value of the type zero.
40 func zeroKindOf(typ reflect.Type) ZeroKind {
41 if typ.Implements(isZeroerType) || reflect.PointerTo(typ).Implements(isZeroerType) {
42 return ZeroMethod
43 }
44 switch typ.Kind() {
45 case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
46 reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
47 reflect.Ptr, reflect.Map, reflect.Chan, reflect.Func, reflect.UnsafePointer:
48 // the value is zero if and only if its bits are.
49 switch typ.Size() {
50 case 1:
51 return ZeroBits8
52 case 2:
53 return ZeroBits16
54 case 4:
55 return ZeroBits32
56 case 8:
57 return ZeroBits64
58 }
59 case reflect.Float32:
60 return ZeroFloat32
61 case reflect.Float64:
62 return ZeroFloat64
63 case reflect.String:
64 return ZeroString
65 case reflect.Slice:
66 return ZeroNilSlice
67 case reflect.Interface:
68 return ZeroNilInterface
69 }
70 return ZeroReflect
71 }
72
73 // IsZeroField is whether the value at p, of the field of the opcode, is zero for omitzero.
74 func IsZeroField(code *Opcode, p unsafe.Pointer) bool {
75 switch code.ZeroKind {
76 case ZeroBits8:
77 return *(*uint8)(p) == 0
78 case ZeroBits16:
79 return *(*uint16)(p) == 0
80 case ZeroBits32:
81 return *(*uint32)(p) == 0
82 case ZeroBits64:
83 return *(*uint64)(p) == 0
84 case ZeroFloat32:
85 return *(*float32)(p) == 0
86 case ZeroFloat64:
87 return *(*float64)(p) == 0
88 case ZeroString:
89 return len(*(*string)(p)) == 0
90 case ZeroNilSlice:
91 return (*runtime.SliceHeader)(p).Data == nil
92 case ZeroNilInterface:
93 return *(*unsafe.Pointer)(p) == nil
94 case ZeroReflect:
95 return reflect.NewAt(runtime.TypeOfPtr(code.Type), p).Elem().IsZero()
96 case ZeroMethod:
97 return isZeroByMethod(reflect.NewAt(runtime.TypeOfPtr(code.Type), p))
98 }
99 return false
100 }
101
102 // isZeroByMethod is whether the value which ptr points to is zero by its IsZero method, as encoding/json
103 // decides it: a nil pointer, a nil interface value or one which holds a nil pointer is zero without a call.
104 func isZeroByMethod(ptr reflect.Value) bool {
105 v := ptr.Elem()
106 typ := v.Type()
107 switch {
108 case typ.Kind() == reflect.Interface && typ.Implements(isZeroerType):
109 return v.IsNil() || (v.Elem().Kind() == reflect.Ptr && v.Elem().IsNil()) || v.Interface().(isZeroer).IsZero()
110 case typ.Kind() == reflect.Ptr && typ.Implements(isZeroerType):
111 return v.IsNil() || v.Interface().(isZeroer).IsZero()
112 case typ.Implements(isZeroerType):
113 return v.Interface().(isZeroer).IsZero()
114 }
115 return ptr.Interface().(isZeroer).IsZero()
116 }