-
Notifications
You must be signed in to change notification settings - Fork 0
/
nil.go
137 lines (120 loc) · 3.65 KB
/
nil.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
package jsoniterpb
import (
"reflect"
"sync"
"unsafe"
jsoniter "github.com/json-iterator/go"
"github.com/modern-go/reflect2"
"google.golang.org/protobuf/types/known/structpb"
)
// https://github.com/golang/protobuf/issues/1487
var doubleQuote = []byte{'"', '"'}
func (e *ProtoExtension) decorateEncoderForNilCollection(typ reflect2.Type, encoder jsoniter.ValEncoder) jsoniter.ValEncoder {
// - marshal nil []byte to ""
// - marshal nil slice to []
// - marshal nil map to {}
isList := typ.Kind() == reflect.Slice
isLikeMap := typ.Kind() == reflect.Map
if isList || isLikeMap {
isBytes := typ.Kind() == reflect.Slice && typ.(reflect2.SliceType).Elem().Kind() == reflect.Uint8
return &funcEncoder{
fun: func(ptr unsafe.Pointer, stream *jsoniter.Stream) {
if *((*unsafe.Pointer)(ptr)) == nil {
if isBytes {
stream.Write(doubleQuote)
return
}
if isList {
stream.WriteEmptyArray()
return
}
if isLikeMap {
stream.WriteEmptyObject()
return
}
}
encoder.Encode(ptr, stream)
},
isEmptyFunc: func(ptr unsafe.Pointer) bool {
return encoder.IsEmpty(ptr)
},
}
}
return nil
}
var wktValuePtrType = reflect2.TypeOfPtr((*structpb.Value)(nil))
func (e *ProtoExtension) decorateDecoderForNil(typ reflect2.Type, dec jsoniter.ValDecoder) jsoniter.ValDecoder {
// - unmarshal null to NULL value
if typ == wktValuePtrType {
return &funcDecoder{
fun: func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
if iter.ReadNil() {
v := structpb.NewNullValue()
*((**structpb.Value)(ptr)) = v
} else {
dec.Decode(ptr, iter)
}
},
}
}
return nil
}
type lazyValue struct {
once sync.Once
ret interface{}
creator func() interface{}
}
func newLazyValue(creator func() interface{}) *lazyValue {
return &lazyValue{
creator: creator,
}
}
func (v *lazyValue) Get() interface{} {
v.once.Do(func() {
v.ret = v.creator()
})
return v.ret
}
var lazyPtrWithZeroValueMap sync.Map
// - marshal []type{a,nil,c} to [a,zero,c]
// - marshal map[string]type to {"a":"valueA",b:zero,c:"valueC"}
func noNullElemEncoderForCollection(valueType reflect2.Type, encoder jsoniter.ValEncoder) jsoniter.ValEncoder {
return &funcEncoder{
fun: func(ptr unsafe.Pointer, stream *jsoniter.Stream) {
if valueType.Kind() == reflect.Ptr {
if *(*unsafe.Pointer)(ptr) == nil {
v, _ := lazyPtrWithZeroValueMap.LoadOrStore(valueType, newLazyValue(func() interface{} {
ptrType := valueType.(reflect2.PtrType)
elemType := ptrType.Elem()
elemPtr := elemType.UnsafeNew()
// record first
newPtr := ptrType.UnsafeNew()
*(*unsafe.Pointer)(newPtr) = elemPtr
for elemType.Kind() == reflect.Ptr {
ptrType = elemType.(reflect2.PtrType)
elemType = ptrType.Elem()
newElemPtr := elemType.UnsafeNew()
*(*unsafe.Pointer)(elemPtr) = newElemPtr
elemPtr = newElemPtr
}
return newPtr
}))
ptr = v.(*lazyValue).Get().(unsafe.Pointer)
}
}
encoder.Encode(ptr, stream)
},
isEmptyFunc: func(ptr unsafe.Pointer) bool {
return encoder.IsEmpty(ptr)
},
}
}
func (e *ProtoExtension) updateMapEncoderConstructorForNonNull(v *jsoniter.MapEncoderConstructor) {
v.ElemEncoder = noNullElemEncoderForCollection(v.MapType.Elem(), v.ElemEncoder)
}
func (e *ProtoExtension) updateSliceEncoderConstructorForNonNull(v *jsoniter.SliceEncoderConstructor) {
v.ElemEncoder = noNullElemEncoderForCollection(v.SliceType.Elem(), v.ElemEncoder)
}
func (e *ProtoExtension) updateArrayEncoderConstructorForNonNull(v *jsoniter.ArrayEncoderConstructor) {
v.ElemEncoder = noNullElemEncoderForCollection(v.ArrayType.Elem(), v.ElemEncoder)
}