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

internal.IsAssigned   C

Complexity

Conditions 10

Size

Total Lines 23
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 22
dl 0
loc 23
rs 5.9999
c 0
b 0
f 0
nop 1

How to fix   Complexity   

Complexity

Complex classes like internal.IsAssigned often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
package internal
2
3
import (
4
	"fmt"
5
	"reflect"
6
)
7
8
// IsAssigned checks the given parameter has value also if it's slice or array that checks has element.
9
// Example:
10
// 		result := IsAssigned("test data")
11
// 		// Output: true
12
func IsAssigned(a interface{}) bool {
13
	if a == nil {
14
		return false
15
	}
16
	val := reflect.ValueOf(a)
17
	switch val.Kind() {
18
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
19
		return (val.Int()) != 0
20
	case reflect.Float32, reflect.Float64:
21
		return (val.Float()) != 0.0
22
	case reflect.String:
23
		return (fmt.Sprintf("%s", a)) != ""
24
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
25
		return (val.Uint()) != 0
26
	case reflect.Bool:
27
		return (val.Bool())
28
	case reflect.Chan, reflect.Func, reflect.Struct, reflect.UnsafePointer:
29
		tt := reflect.Zero(val.Type())
30
		return !reflect.DeepEqual(val, tt)
31
	case reflect.Array, reflect.Slice:
32
		return val.Len() > 0
33
	default:
34
		return false
35
	}
36
}
37