gopkg.in/tools/godep.v67@v67.0.0-20160513230433-2d182dfe781d/Godeps/_workspace/src/github.com/kr/pretty/zero.go (about)

     1  package pretty
     2  
     3  import (
     4  	"reflect"
     5  )
     6  
     7  func nonzero(v reflect.Value) bool {
     8  	switch v.Kind() {
     9  	case reflect.Bool:
    10  		return v.Bool()
    11  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
    12  		return v.Int() != 0
    13  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
    14  		return v.Uint() != 0
    15  	case reflect.Float32, reflect.Float64:
    16  		return v.Float() != 0
    17  	case reflect.Complex64, reflect.Complex128:
    18  		return v.Complex() != complex(0, 0)
    19  	case reflect.String:
    20  		return v.String() != ""
    21  	case reflect.Struct:
    22  		for i := 0; i < v.NumField(); i++ {
    23  			if nonzero(getField(v, i)) {
    24  				return true
    25  			}
    26  		}
    27  		return false
    28  	case reflect.Array:
    29  		for i := 0; i < v.Len(); i++ {
    30  			if nonzero(v.Index(i)) {
    31  				return true
    32  			}
    33  		}
    34  		return false
    35  	case reflect.Map, reflect.Interface, reflect.Slice, reflect.Ptr, reflect.Chan, reflect.Func:
    36  		return !v.IsNil()
    37  	case reflect.UnsafePointer:
    38  		return v.Pointer() != 0
    39  	}
    40  	return true
    41  }