Codebase list golang-github-kolo-xmlrpc / 201c6bfc-30f0-4a24-94b9-cf7bac487a91/upstream
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
2 changed file(s) with 45 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
9595
9696 name := fieldType.Tag.Get("xmlrpc")
9797 // 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) {
9999 continue
100100 }
101101 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 }