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

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