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

internal/is.go   A

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 41
eloc 110
dl 0
loc 188
rs 9.1199
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A internal.IsInteger 0 17 2
A internal.IsSlice 0 6 2
A internal.IsArray 0 6 2
B internal.checkKindMultiple 0 11 6
A internal.IsPointer 0 8 2
A internal.IsByteArray 0 5 2
A internal.checkKind 0 2 1
A internal.IsNumber 0 9 3
A internal.IsString 0 6 2
A internal.IsEqual 0 9 5
A internal.IsChan 0 6 2
A internal.IsBool 0 6 2
A internal.IsStruct 0 6 2
A internal.IsNil 0 2 1
A internal.IsFunc 0 6 2
A internal.IsMap 0 6 2
A internal.IsEmpty 0 2 1
A internal.IsFloat 0 9 2
1
package internal
2
3
import (
4
	"reflect"
5
)
6
7
// IsArray checks the given parameter is an array
8
func IsArray(a interface{}) bool {
9
	if a == nil {
10
		return false
11
	}
12
	val := reflect.ValueOf(a)
13
	return checkKind(val.Kind(), reflect.Array)
14
}
15
16
// IsSlice checks the given parameter is a slice
17
func IsSlice(a interface{}) bool {
18
	if a == nil {
19
		return false
20
	}
21
	val := reflect.ValueOf(a)
22
	return checkKind(val.Kind(), reflect.Slice)
23
}
24
25
// IsString checks the given parameter is a string
26
func IsString(a interface{}) bool {
27
	if a == nil {
28
		return false
29
	}
30
	val := reflect.ValueOf(a)
31
	return checkKind(val.Kind(), reflect.String)
32
}
33
34
// IsBool checks the given parameter is a bool
35
func IsBool(a interface{}) bool {
36
	if a == nil {
37
		return false
38
	}
39
	val := reflect.ValueOf(a)
40
	return checkKind(val.Kind(), reflect.Bool)
41
}
42
43
// IsInteger checks the given parameter is an integer
44
// int, int8, int16, int32, int64, uint, uint8, uint16, uint32, unit64
45
func IsInteger(a interface{}) bool {
46
	if a == nil {
47
		return false
48
	}
49
	val := reflect.ValueOf(a)
50
	return checkKindMultiple(
51
		val.Kind(),
52
		reflect.Int,
53
		reflect.Int8,
54
		reflect.Int16,
55
		reflect.Int32,
56
		reflect.Int64,
57
		reflect.Uint,
58
		reflect.Uint8,
59
		reflect.Uint16,
60
		reflect.Uint32,
61
		reflect.Uint64,
62
	)
63
}
64
65
// IsFloat checks the given parameter is an float
66
// float32, float64
67
func IsFloat(a interface{}) bool {
68
	if a == nil {
69
		return false
70
	}
71
	val := reflect.ValueOf(a)
72
	return checkKindMultiple(
73
		val.Kind(),
74
		reflect.Float32,
75
		reflect.Float64,
76
	)
77
}
78
79
// IsNumber checks the given parameter is type of any golang number
80
// int, int8, int16, int32, int64, uint, uint8, uint16, uint32, unit64, float32, float64
81
func IsNumber(a interface{}) bool {
82
	if a == nil {
83
		return false
84
	}
85
	val := reflect.ValueOf(a)
86
	return IsInteger(a) || checkKindMultiple(
87
		val.Kind(),
88
		reflect.Float32,
89
		reflect.Float64,
90
	)
91
}
92
93
// IsFunc checks the given parameter is a func
94
// int, int8, int16, int32, int64, uint, uint8, uint16, uint32, unit64, float32, float64
95
func IsFunc(a interface{}) bool {
96
	if a == nil {
97
		return false
98
	}
99
	val := reflect.ValueOf(a)
100
	return checkKind(val.Kind(), reflect.Func)
101
}
102
103
// IsStruct checks the given parameter is a struct
104
func IsStruct(a interface{}) bool {
105
	if a == nil {
106
		return false
107
	}
108
	val := reflect.ValueOf(a)
109
	return checkKind(val.Kind(), reflect.Struct)
110
}
111
112
// IsPointer checks the given parameter is a pointer
113
func IsPointer(a interface{}) bool {
114
	if a == nil {
115
		return false
116
	}
117
	val := reflect.ValueOf(a)
118
	return checkKindMultiple(
119
		val.Kind(),
120
		reflect.Ptr,
121
	)
122
}
123
124
// IsChan checks the given parameter is a channel
125
func IsChan(a interface{}) bool {
126
	if a == nil {
127
		return false
128
	}
129
	val := reflect.ValueOf(a)
130
	return checkKind(val.Kind(), reflect.Chan)
131
}
132
133
// IsMap checks the given parameter is a map
134
func IsMap(a interface{}) bool {
135
	if a == nil {
136
		return false
137
	}
138
	val := reflect.ValueOf(a)
139
	return checkKind(val.Kind(), reflect.Map)
140
}
141
142
// IsByteArray checks the given parameter is a byte array
143
func IsByteArray(a interface{}) bool {
144
	if a == nil {
145
		return false
146
	}
147
	return reflect.TypeOf(a) == reflect.TypeOf([]byte(nil))
148
}
149
150
// IsEqual checks the given any 2 parameters are equal
151
func IsEqual(a interface{}, b interface{}) bool {
152
	if a == nil && b == nil {
153
		return true
154
	} else if a == nil {
155
		return false
156
	} else if b == nil {
157
		return false
158
	}
159
	return reflect.DeepEqual(a, b)
160
}
161
162
// IsEmpty checks the given parameter has not value also if it's slice or array that checks has not any element.
163
// It works same IsNotAssigned()
164
func IsEmpty(a interface{}, b interface{}) bool {
165
	return IsNotAssigned(a)
166
}
167
168
// IsNil checks the given parameter has not value also if it's slice or array that checks has not any element.
169
// It works same IsNotAssigned()
170
func IsNil(a interface{}, b interface{}) bool {
171
	return IsNotAssigned(a)
172
}
173
174
func checkKind(k reflect.Kind, t reflect.Kind) bool {
175
	return k == t
176
}
177
178
func checkKindMultiple(k reflect.Kind, t ...reflect.Kind) bool {
179
	if r, err := FindBy(t, func(val interface{}, i int) bool {
180
		if kk, ok := val.(reflect.Kind); ok && kk == k {
181
			return true
182
		} else {
183
			return false
184
		}
185
	}); err == nil && r != nil {
186
		return true
187
	} else {
188
		return false
189
	}
190
}
191