-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
114 lines (112 loc) · 2.76 KB
/
utils.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
package fastjson
import (
"errors"
"fmt"
"os"
"reflect"
"strconv"
)
// override go.utils.GetEntOrDefault
func GetEnvOrDefault(k string, dv interface{}) interface{} {
v, ok := os.LookupEnv(k)
if !ok {
return dv
} else {
if dv == nil {
return v
}
var err error
switch dv.(type) {
case string:
return v
case bool:
var ret bool
ret, err = strconv.ParseBool(v)
if err == nil {
return ret
}
break
case int:
var ret int
ret, err = strconv.Atoi(v)
if err == nil {
return ret
}
break
case int64:
var ret int64
ret, err = strconv.ParseInt(v, 10, 64)
if err == nil {
return ret
}
break
case int32:
var ret int64
ret, err = strconv.ParseInt(v, 10, 32)
if err == nil {
return ret
}
break
case float64:
var ret float64
ret, err = strconv.ParseFloat(v, 64)
if err == nil {
return ret
}
break
case float32:
var ret float64
ret, err = strconv.ParseFloat(v, 32)
if err == nil {
return ret
}
break
case map[string]interface{}:
ret := make(map[string]interface{})
err = json.UnmarshalFromString(v, &ret)
if err == nil {
return ret
}
case JSONObject:
ret := ParseObject(v)
if ret == nil {
err = errors.New("parse to json failed")
} else {
return *ret
}
break
case *JSONObject:
ret := ParseObject(v)
if ret == nil {
err = errors.New("parse to json failed")
} else {
return ret
}
break
case JSONArray:
ret := ParseArray(v)
if ret == nil {
err = errors.New("parse to json failed")
} else {
return *ret
}
break
case *JSONArray:
ret := ParseArray(v)
if ret == nil {
err = errors.New("parse to json failed")
} else {
return ret
}
break
default:
fmt.Printf("type: %v", reflect.TypeOf(dv))
return v
}
if err != nil {
// TODO: 此处返回值可商榷
return dv
}
return nil
}
}