go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/assert/len.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package assert
     9  
    10  import "reflect"
    11  
    12  // Len returns the length of a given reference if it is a slice, string, channel, or map.
    13  func Len(object any) int {
    14  	switch object {
    15  	case nil:
    16  		return 0
    17  	case "":
    18  		return 0
    19  	}
    20  	objValue := reflect.ValueOf(object)
    21  	switch objValue.Kind() {
    22  	case reflect.Map, reflect.Slice, reflect.Chan, reflect.String:
    23  		return objValue.Len()
    24  	default:
    25  		return 0
    26  	}
    27  }