forked from Barbery/phpGo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisset.go
More file actions
71 lines (55 loc) · 1.28 KB
/
isset.go
File metadata and controls
71 lines (55 loc) · 1.28 KB
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
package phpGo
import (
"reflect"
)
func Isset(args ...interface{}) bool {
arrVal := reflect.ValueOf(args[0])
if !arrVal.IsValid() {
return false
}
if len(args) < 2 || args[1] == nil {
return issetValue(args[0])
}
switch arrVal.Kind() {
case reflect.Array, reflect.Slice:
if index, ok := args[1].(int); ok {
return arrVal.Index(index).IsValid()
}
return false
case reflect.Map:
indexVal := arrVal.MapIndex(reflect.ValueOf(args[1]))
if indexVal.IsValid() == false {
return false
}
return true
case reflect.Struct:
key := reflect.ValueOf(args[1]).String()
if !arrVal.FieldByName(key).IsValid() {
return false
} else {
return true
}
}
return false
}
func issetValue(value interface{}) bool {
if value == nil {
return false
}
val := reflect.ValueOf(value)
switch val.Kind() {
case reflect.Bool:
return val.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return val.Int() != 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return val.Uint() != 0
case reflect.Float32, reflect.Float64:
return val.Float() != 0.00
case reflect.Complex64, reflect.Complex128:
return val.Complex() != 0+0i
case reflect.String:
return val.String() != ""
}
return val.IsValid()
}