github.com/elves/elvish@v0.15.0/pkg/eval/vals/len.go (about)

     1  package vals
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/xiaq/persistent/vector"
     7  )
     8  
     9  // Lener wraps the Len method.
    10  type Lener interface {
    11  	// Len computes the length of the receiver.
    12  	Len() int
    13  }
    14  
    15  var _ Lener = vector.Vector(nil)
    16  
    17  // Len returns the length of the value, or -1 if the value does not have a
    18  // well-defined length. It is implemented for the builtin type string, StructMap
    19  // types, and types satisfying the Lener interface. For other types, it returns
    20  // -1.
    21  func Len(v interface{}) int {
    22  	switch v := v.(type) {
    23  	case string:
    24  		return len(v)
    25  	case Lener:
    26  		return v.Len()
    27  	case StructMap:
    28  		return getStructMapInfo(reflect.TypeOf(v)).filledFields
    29  	}
    30  	return -1
    31  }