|
15 | 15 | package patch |
16 | 16 |
|
17 | 17 | import ( |
18 | | - "encoding/json" |
19 | 18 | "reflect" |
| 19 | + "unsafe" |
20 | 20 |
|
21 | 21 | "github.com/goph/emperror" |
| 22 | + json "github.com/json-iterator/go" |
22 | 23 | "github.com/pkg/errors" |
| 24 | + "k8s.io/apimachinery/pkg/util/intstr" |
23 | 25 | ) |
24 | 26 |
|
| 27 | +func init() { |
| 28 | + // k8s.io/apimachinery/pkg/util/intstr.IntOrString behaves really badly |
| 29 | + // from JSON marshaling point of view, it can't be empty basically. |
| 30 | + // So we need to override the defined marshaling behaviour and write nil |
| 31 | + // instead of 0, because usually (in all observed cases) 0 means "not set" |
| 32 | + // for IntOrStr types. |
| 33 | + // To make this happen we need to pull in json-iterator and override the |
| 34 | + // factory marshaling overrides. |
| 35 | + json.RegisterTypeEncoderFunc("intstr.IntOrString", |
| 36 | + func(ptr unsafe.Pointer, stream *json.Stream) { |
| 37 | + i := (*intstr.IntOrString)(ptr) |
| 38 | + if i.IntValue() == 0 { |
| 39 | + stream.WriteNil() |
| 40 | + } else { |
| 41 | + stream.WriteInt(i.IntValue()) |
| 42 | + } |
| 43 | + }, |
| 44 | + func(ptr unsafe.Pointer) bool { |
| 45 | + i := (*intstr.IntOrString)(ptr) |
| 46 | + return i.IntValue() == 0 |
| 47 | + }, |
| 48 | + ) |
| 49 | +} |
| 50 | + |
25 | 51 | func DeleteNullInJson(jsonBytes []byte) ([]byte, map[string]interface{}, error) { |
26 | 52 | var patchMap map[string]interface{} |
27 | 53 |
|
@@ -115,6 +141,8 @@ func isZero(v reflect.Value) bool { |
115 | 141 | default: |
116 | 142 | z := reflect.Zero(v.Type()) |
117 | 143 | return v.Interface() == z.Interface() |
| 144 | + case reflect.Float64, reflect.Int64: |
| 145 | + return false |
118 | 146 | case reflect.Func, reflect.Map, reflect.Slice: |
119 | 147 | return v.IsNil() |
120 | 148 | case reflect.Array: |
|
0 commit comments