forked from Antipitch/orwell
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
141 lines (122 loc) · 2.75 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package orwell
import (
"fmt"
"reflect"
)
// NOE func
func NOE(value interface{}) bool {
if _, isNil := IsNil(value); isNil {
return true
}
if IsEmpty(value) {
return true
}
return false
}
// IsNil func
func IsNil(value interface{}) (interface{}, bool) {
if value == nil {
return nil, true
}
v := reflect.ValueOf(value)
switch v.Kind() {
case reflect.Invalid:
return nil, true
case reflect.Ptr, reflect.Interface:
if v.IsNil() {
return nil, true
}
return IsNil(v.Elem().Interface())
case reflect.Slice, reflect.Array, reflect.Map, reflect.Func, reflect.Chan:
if v.IsNil() {
return nil, true
}
}
return value, false
}
// IsEmpty func
func IsEmpty(value interface{}) bool {
if value == nil {
return true
}
v := reflect.ValueOf(value)
switch v.Kind() {
case reflect.Invalid:
return true
case reflect.Bool:
return !v.Bool()
case reflect.Array, reflect.Map, reflect.Slice:
return v.Len() == 0
case reflect.Interface, reflect.Ptr:
if v.IsNil() {
return true
}
return IsEmpty(v.Elem().Interface())
}
return v.Interface() == reflect.Zero(v.Type()).Interface()
}
// ToString func
func ToString(value interface{}) (string, error) {
if str, ok := value.(string); ok {
return str, nil
}
v := reflect.ValueOf(value)
if v.Type() == reflect.TypeOf([]byte(nil)) {
return string(v.Interface().([]byte)), nil
}
return "", fmt.Errorf("Argument could not be converted to string")
}
// ToInt64 func
func ToInt64(v interface{}) (int64, error) {
switch v.(type) {
case int:
return int64(v.(int)), nil
case int8:
return int64(v.(int8)), nil
case int16:
return int64(v.(int16)), nil
case int32:
return int64(v.(int32)), nil
case int64:
return v.(int64), nil
}
return 0, fmt.Errorf("Argument could not be converted to int64")
}
// ToUInt64 func
func ToUInt64(v interface{}) (uint64, error) {
switch v.(type) {
case uint:
return uint64(v.(uint)), nil
case uint8:
return uint64(v.(uint8)), nil
case uint16:
return uint64(v.(uint16)), nil
case uint32:
return uint64(v.(uint32)), nil
case uint64:
return v.(uint64), nil
}
return 0, fmt.Errorf("Argument could not be converted to uint64")
}
// ToFloat64 func
func ToFloat64(v interface{}) (float64, error) {
switch v.(type) {
case float32:
return float64(v.(float32)), nil
case float64:
return v.(float64), nil
}
return 0, fmt.Errorf("Argument could not be converted to float64")
}
// LengthOf func
func LengthOf(value interface{}) (int, error) {
v := reflect.ValueOf(value)
switch v.Kind() {
case reflect.String, reflect.Array, reflect.Slice, reflect.Map:
return v.Len(), nil
}
return 0, fmt.Errorf("Could not determine length of argument type %v", v.Kind())
}
func DeepEqual(s interface{}, c interface{}) bool {
return reflect.DeepEqual(s, c)
}