-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelper.go
62 lines (58 loc) · 1.17 KB
/
helper.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
package enumeration
import "strconv"
func ScanEnum(src interface{}, offset int) (int, error) {
return ScanIntEnumStringer(src, offset)
}
func ScanIntEnumStringer(src interface{}, offset int) (int, error) {
n, err := toInteger(src, offset)
if err != nil {
return offset, err
}
return n - offset, nil
}
func toInteger(src interface{}, defaultInteger int) (int, error) {
switch v := src.(type) {
case []byte:
if len(v) > 0 {
i, err := strconv.ParseInt(string(v), 10, 64)
if err != nil {
return defaultInteger, err
}
return int(i), err
}
return defaultInteger, nil
case string:
if v != "" {
i, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return defaultInteger, err
}
return int(i), err
}
return defaultInteger, nil
case int:
return v, nil
case int8:
return int(v), nil
case int16:
return int(v), nil
case int32:
return int(v), nil
case int64:
return int(v), nil
case uint:
return int(v), nil
case uint8:
return int(v), nil
case uint16:
return int(v), nil
case uint32:
return int(v), nil
case uint64:
return int(v), nil
case nil:
return defaultInteger, nil
default:
return defaultInteger, nil
}
}