github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/amino/utils.go (about) 1 package amino 2 3 import "reflect" 4 5 // IsTypedNil and IsEmpty are provided for users that prefer nil over empty bytes. 6 // Also, IsTypedNil is required to encode arbitrary top-level objects that may 7 // be typed-nil pointers -- for otherwise amino.Marshal would panic. 8 9 // Go lacks a simple and safe way to see if something is a typed nil. 10 // See: 11 // - https://dave.cheney.net/2017/08/09/typed-nils-in-go-2 12 // - https://groups.google.com/forum/#!topic/golang-nuts/wnH302gBa4I/discussion 13 // - https://github.com/golang/go/issues/21538 14 func IsTypedNil(o interface{}) bool { 15 rv := reflect.ValueOf(o) 16 switch rv.Kind() { 17 case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Slice: 18 return rv.IsNil() 19 default: 20 return false 21 } 22 } 23 24 // Returns true if it has zero length. 25 func IsEmpty(o interface{}) bool { 26 rv := reflect.ValueOf(o) 27 switch rv.Kind() { 28 case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String: 29 return rv.Len() == 0 30 default: 31 return false 32 } 33 }