Skip to content

Commit

Permalink
Support Len to return size of string, slice, array or map
Browse files Browse the repository at this point in the history
  • Loading branch information
shockerli committed Nov 28, 2023
1 parent 761934e commit a6aabc5
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
56 changes: 56 additions & 0 deletions cvte.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,62 @@ var formatOutOfLimitInt = "%w, out of max limit value(%d)"
var formatOutOfLimitFloat = "%w, out of max limit value(%f)"
var formatExtend = "%v, %w"

// Len return size of string, slice, array or map
func Len(v interface{}) int {
if v == nil {
return 0
}

switch vv := v.(type) {
case string:
return len(vv)
case []bool:
return len(vv)
case []byte:
return len(vv)
case []rune:
return len(vv)
case []string:
return len(vv)
case []int:
return len(vv)
case []int8:
return len(vv)
case []int16:
return len(vv)
case []int64:
return len(vv)
case []uint:
return len(vv)
case []uint16:
return len(vv)
case []uint32:
return len(vv)
case []uint64:
return len(vv)
case []float32:
return len(vv)
case []float64:
return len(vv)
case []interface{}:
return len(vv)
case map[string]interface{}:
return len(vv)
case map[string]string:
return len(vv)
case map[string]int:
return len(vv)
}

_, rv := Indirect(v)
switch rv.Kind() {
case reflect.Slice, reflect.Array, reflect.Map:
return rv.Len()
}

return -1
}

// Field return the field value from map/struct, with default value
func Field(v interface{}, field interface{}, def ...interface{}) interface{} {
if v, err := FieldE(v, field); err == nil {
Expand Down
61 changes: 61 additions & 0 deletions cvte_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,67 @@ func TestIndirect(t *testing.T) {
}
}

func TestLen(t *testing.T) {
tests := []struct {
expect int
input interface{}
}{
// supported type
{0, nil},
{0, ""},
{3, "123"},
{3, []interface{}{1, "2", 3.0}},
{2, []bool{true, false}},
{2, []rune("中国")},
{3, []byte("123")},
{0, []int(nil)},
{2, [2]int{1, 2}},
{2, []int{1, 2}},
{2, []int8{1, 2}},
{2, []int16{1, 2}},
{2, []int32{1, 2}},
{2, []int64{1, 2}},
{2, []uint{1, 2}},
{2, []uint8{1, 2}},
{2, []uint16{1, 2}},
{2, []uint32{1, 2}},
{2, []uint64{1, 2}},
{2, [2]string{"1", "2"}},
{2, []string{"1", "2"}},
{2, []float32{1.1, 2.2}},
{2, []float64{1.1, 2.2}},
{2, &[]float64{1.1, 2.2}},
{2, AliasTypeBytes{1, 2}},
{2, &AliasTypeBytes{1, 2}},
{2, []AliasTypeInt{1, 2}},
{2, []AliasTypeString{"1", "2"}},
{0, map[int]interface{}(nil)},
{2, map[int]interface{}{1: 1, 2: 2}},
{2, map[string]interface{}{"1": 1, "2": 2}},
{2, map[string]string{"1": "1", "2": "2"}},
{2, map[string]int{"1": 1, "2": 2}},
{2, map[AliasTypeString]interface{}{"1": 1, "2": 2}},
{2, map[AliasTypeString]AliasTypeInt{"1": 1, "2": 2}},

// unsupported type
{-1, 123},
{-1, 123.0},
{-1, true},
{-1, struct{}{}},
}

for i, tt := range tests {
msg := fmt.Sprintf(
"i = %d, input[%+v], expect[%+v]",
i, tt.input, tt.expect,
)

val := cvt.Len(tt.input)

assertEqual(t, tt.expect, val, "[NoErr] "+msg)
}
}

/* ------------------------------------------------------------------------------ */

// [testing assert functions]
Expand Down

1 comment on commit a6aabc5

@vercel
Copy link

@vercel vercel bot commented on a6aabc5 Nov 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

cvt – ./

cvt-shockerli.vercel.app
cvt-git-master-shockerli.vercel.app
cvt.shockerli.net

Please sign in to comment.