-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathomitempty.go
66 lines (57 loc) · 1.57 KB
/
omitempty.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
package astconf
import "reflect"
type omitEmptyEncoder struct {
elemEnc encoderFunc
}
func newOmitEmptyEncoder(t reflect.Type, elemEnc encoderFunc) encoderFunc {
// TODO: Add support for types implementing the IsZero() interface?
// See: https://github.com/golang/go/issues/4357
// https://github.com/golang/go/issues/11939
enc := omitEmptyEncoder{elemEnc: elemEnc}
switch t.Kind() {
case reflect.Bool:
return enc.encodeBool
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return enc.encodeInt
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return enc.encodeUint
case reflect.String:
return enc.encodeString
case reflect.Slice:
return enc.encodeSlice
case reflect.Ptr:
return newIndirectEncoder(newOmitEmptyEncoder(t.Elem(), elemEnc))
default:
return elemEnc
}
}
func (oee omitEmptyEncoder) encodeBool(v reflect.Value, e *Encoder) error {
if v.Bool() == false {
return nil
}
return oee.elemEnc(v, e)
}
func (oee omitEmptyEncoder) encodeInt(v reflect.Value, e *Encoder) error {
if v.Int() == 0 {
return nil
}
return oee.elemEnc(v, e)
}
func (oee omitEmptyEncoder) encodeUint(v reflect.Value, e *Encoder) error {
if v.Uint() == 0 {
return nil
}
return oee.elemEnc(v, e)
}
func (oee omitEmptyEncoder) encodeString(v reflect.Value, e *Encoder) error {
if v.String() == "" {
return nil
}
return oee.elemEnc(v, e)
}
func (oee omitEmptyEncoder) encodeSlice(v reflect.Value, e *Encoder) error {
if v.Len() == 0 {
return nil
}
return oee.elemEnc(v, e)
}