Merge pull request #71 from kolo/is_zero
Support versions of Go < 1.13
Ilia Choly authored 4 years ago
GitHub committed 4 years ago
95 | 95 |
|
96 | 96 |
name := fieldType.Tag.Get("xmlrpc")
|
97 | 97 |
// if the tag has the omitempty property, skip it
|
98 | |
if strings.HasSuffix(name, ",omitempty") && fieldVal.IsZero() {
|
|
98 |
if strings.HasSuffix(name, ",omitempty") && isZero(fieldVal) {
|
99 | 99 |
continue
|
100 | 100 |
}
|
101 | 101 |
name = strings.TrimSuffix(name, ",omitempty")
|
|
0 |
package xmlrpc
|
|
1 |
|
|
2 |
import (
|
|
3 |
"math"
|
|
4 |
. "reflect"
|
|
5 |
)
|
|
6 |
|
|
7 |
func isZero(v Value) bool {
|
|
8 |
switch v.Kind() {
|
|
9 |
case Bool:
|
|
10 |
return !v.Bool()
|
|
11 |
case Int, Int8, Int16, Int32, Int64:
|
|
12 |
return v.Int() == 0
|
|
13 |
case Uint, Uint8, Uint16, Uint32, Uint64, Uintptr:
|
|
14 |
return v.Uint() == 0
|
|
15 |
case Float32, Float64:
|
|
16 |
return math.Float64bits(v.Float()) == 0
|
|
17 |
case Complex64, Complex128:
|
|
18 |
c := v.Complex()
|
|
19 |
return math.Float64bits(real(c)) == 0 && math.Float64bits(imag(c)) == 0
|
|
20 |
case Array:
|
|
21 |
for i := 0; i < v.Len(); i++ {
|
|
22 |
if !isZero(v.Index(i)) {
|
|
23 |
return false
|
|
24 |
}
|
|
25 |
}
|
|
26 |
return true
|
|
27 |
case Chan, Func, Interface, Map, Ptr, Slice, UnsafePointer:
|
|
28 |
return v.IsNil()
|
|
29 |
case String:
|
|
30 |
return v.Len() == 0
|
|
31 |
case Struct:
|
|
32 |
for i := 0; i < v.NumField(); i++ {
|
|
33 |
if !isZero(v.Field(i)) {
|
|
34 |
return false
|
|
35 |
}
|
|
36 |
}
|
|
37 |
return true
|
|
38 |
default:
|
|
39 |
// This should never happens, but will act as a safeguard for
|
|
40 |
// later, as a default value doesn't makes sense here.
|
|
41 |
panic(&ValueError{"reflect.Value.IsZero", v.Kind()})
|
|
42 |
}
|
|
43 |
}
|