github.com/pure-x-eth/consensus_tm@v0.0.0-20230502163723-e3c2ff987250/types/utils.go (about)

     1  package types
     2  
     3  import "reflect"
     4  
     5  // Go lacks a simple and safe way to see if something is a typed nil.
     6  // See:
     7  //   - https://dave.cheney.net/2017/08/09/typed-nils-in-go-2
     8  //   - https://groups.google.com/forum/#!topic/golang-nuts/wnH302gBa4I/discussion
     9  //   - https://github.com/golang/go/issues/21538
    10  func isTypedNil(o interface{}) bool {
    11  	rv := reflect.ValueOf(o)
    12  	switch rv.Kind() {
    13  	case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Slice:
    14  		return rv.IsNil()
    15  	default:
    16  		return false
    17  	}
    18  }
    19  
    20  // Returns true if it has zero length.
    21  func isEmpty(o interface{}) bool {
    22  	rv := reflect.ValueOf(o)
    23  	switch rv.Kind() {
    24  	case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:
    25  		return rv.Len() == 0
    26  	default:
    27  		return false
    28  	}
    29  }