-
Notifications
You must be signed in to change notification settings - Fork 303
/
encoder.go
126 lines (103 loc) · 2.93 KB
/
encoder.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
package k8s
import (
"unsafe"
jsoniter "github.com/json-iterator/go"
"github.com/modern-go/reflect2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
)
var defaultJSONIterator = createDefaultJSONIterator()
var specJSONIterator = createSpecJSONIterator()
func createDefaultJSONConfig() jsoniter.Config {
return jsoniter.Config{
EscapeHTML: true,
SortMapKeys: true,
ValidateJsonRawMessage: true,
CaseSensitive: true,
}
}
func createDefaultJSONIterator() jsoniter.API {
return createDefaultJSONConfig().Froze()
}
// Create a JSON iterator that:
// - encodes "zero" metav1.Time values as empty instead of nil
// - encodes all status values as empty
func createSpecJSONIterator() jsoniter.API {
config := createDefaultJSONConfig().Froze()
config.RegisterExtension(newTimeExtension())
config.RegisterExtension(alwaysEmptyExtension{
typeIndex: createTypeIndex(allStatusTypes()),
})
return config
}
func allStatusTypes() []reflect2.Type {
result := []reflect2.Type{}
for _, typ := range scheme.Scheme.AllKnownTypes() {
typ2 := reflect2.Type2(typ)
sTyp2, ok := typ2.(reflect2.StructType)
if !ok {
continue
}
statusField := sTyp2.FieldByName("Status")
if statusField == nil {
continue
}
result = append(result, statusField.Type())
}
return result
}
type TypeIndex map[reflect2.Type]bool
func (idx TypeIndex) Contains(typ reflect2.Type) bool {
_, ok := idx[typ]
return ok
}
func createTypeIndex(ts []reflect2.Type) TypeIndex {
result := make(map[reflect2.Type]bool)
for _, t := range ts {
result[t] = true
}
return TypeIndex(result)
}
// Any type that matches this extension is considered empty,
// and skipped during json serialization.
type alwaysEmptyExtension struct {
*jsoniter.DummyExtension
typeIndex TypeIndex
}
func (e alwaysEmptyExtension) CreateEncoder(typ reflect2.Type) jsoniter.ValEncoder {
if e.typeIndex.Contains(typ) {
return alwaysEmptyEncoder{}
}
return nil
}
type alwaysEmptyEncoder struct {
}
func (alwaysEmptyEncoder) IsEmpty(ptr unsafe.Pointer) bool { return true }
func (alwaysEmptyEncoder) Encode(ptr unsafe.Pointer, stream *jsoniter.Stream) {}
type timeExtension struct {
*jsoniter.DummyExtension
timeType reflect2.Type
}
func newTimeExtension() timeExtension {
return timeExtension{
// memoize the type lookup
timeType: reflect2.TypeOf(metav1.Time{}),
}
}
func (e timeExtension) CreateEncoder(typ reflect2.Type) jsoniter.ValEncoder {
if e.timeType == typ {
return timeEncoder{delegate: defaultJSONIterator.EncoderOf(typ)}
}
return nil
}
type timeEncoder struct {
delegate jsoniter.ValEncoder
}
// Returns true if the time value is the zero value.
func (e timeEncoder) IsEmpty(ptr unsafe.Pointer) bool {
t := *((*metav1.Time)(ptr))
return t == metav1.Time{} || e.delegate.IsEmpty(ptr)
}
func (e timeEncoder) Encode(ptr unsafe.Pointer, stream *jsoniter.Stream) {
e.delegate.Encode(ptr, stream)
}