src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/eval/vals/len.go (about) 1 package vals 2 3 import ( 4 "reflect" 5 6 "src.elv.sh/pkg/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 any) 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 lenStructMap(v) 29 } 30 return -1 31 } 32 33 func lenStructMap(m StructMap) int { 34 return getStructMapInfo(reflect.TypeOf(m)).filledFields 35 }