Passed
Pull Request — master (#66)
by Hayrullah
56s
created

internal.Size   A

Complexity

Conditions 5

Size

Total Lines 13
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
dl 0
loc 13
rs 9.3333
c 0
b 0
f 0
nop 1
1
package internal
2
3
import (
4
	"reflect"
5
6
	"github.com/gotilty/gotil/internal/errs"
7
)
8
9
// Size returns a length of given parameter
10
// 		data := []int64{100, 30, -100, -5}
11
// 		result, _ := Size(data)
12
// 		result2, _ := Size("gotil")
13
// 		// Output: 4
14
// 		// Output2: 5
15
func Size(val interface{}) (int, error) {
16
	value := reflect.ValueOf(val)
17
	switch value.Kind() {
18
	case reflect.Map, reflect.Struct, reflect.Array, reflect.Slice:
19
		return value.Len(), nil
20
	case reflect.String:
21
		val_str, err := ToString(val)
22
		if err == nil {
23
			return len(val_str), nil
24
		}
25
		return 0, errs.CustomError(value.Kind().String())
26
	default:
27
		return 0, errs.CustomError(value.Kind().String())
28
	}
29
}
30