/
conv.go
205 lines (196 loc) · 4.64 KB
/
conv.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package gosml
import (
"bytes"
"encoding/json"
"fmt"
"github.com/goinggo/mapstructure"
"reflect"
"strconv"
"strings"
"time"
)
func ConvertDbVals(tp string, bs []byte) interface{} {
if bs == nil {
return nil
}
tp = strings.ToLower(tp)
if strings.Contains(tp, "int") {
val, _ := strconv.ParseInt(string(bs), 10, 64)
return val
} else if strings.EqualFold(tp, "double") {
val, _ := strconv.ParseFloat(string(bs), 64)
return val
} else if strings.Contains(tp, "date") || strings.Contains(tp, "time") {
return ConvertStrToDate(string(bs))
} else {
return string(bs)
}
}
func ConvertValueToRequestType(val string, tp string) interface{} {
switch tp {
case "char":
return val
case "number":
if strings.Contains(val, ".") {
v, err := strconv.ParseFloat(val, 64)
ThrowRuntime(err)
return v
} else {
v, err := strconv.ParseInt(val, 10, 64)
ThrowRuntime(err)
return v
}
case "array":
return strings.Split(val, ",")
case "date", "time":
return ConvertStrToDate(val)
case "array-date", "array-time":
{
vs := strings.Split(val, ",")
times := make([]time.Time, 0)
for _, v := range vs {
times = append(times, ConvertStrToDate(v))
}
return times
}
default:
return val
}
}
func ConvertStrToDate(val string) time.Time {
bs := []byte(val)
bf := bytes.NewBufferString("")
for _, v := range bs {
switch v {
case '-', ':', ' ', '/', 'T', 'Z':
continue
default:
bf.WriteByte(v)
}
}
format := []byte("200601021504050700")
v, err := time.Parse(string(format[:bf.Len()]), string(bf.Bytes()))
ThrowRuntime(err)
return v
}
func ThrowRuntime(err error) {
if err != nil {
panic(err)
}
}
func ToJson(obj interface{}) ([]byte, error) {
return json.Marshal(obj)
}
func ToJsonString(obj interface{}) string {
data, _ := ToJson(obj)
return string(data)
}
func FromJson(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}
func MapToStructure(m interface{}, rawVal interface{}) error {
return mapstructure.Decode(m, rawVal)
}
func debug(a ...interface{}) {
fmt.Println(a...)
}
func ConvertStructureToMap(pointer interface{}) map[string]interface{} {
result := make(map[string]interface{})
ConvertStructureAppendMap(pointer, result)
return result
}
func ConvertStructureAppendMap(pointer interface{}, m map[string]interface{}) {
elem := reflect.ValueOf(pointer).Elem()
relType := elem.Type()
for i := 0; i < relType.NumField(); i++ {
m[relType.Field(i).Name] = elem.Field(i).Interface()
}
}
func ConvertToString(i interface{}) string {
var result string
if reflect.TypeOf(i) == nil {
return result
}
kind := reflect.TypeOf(i).Kind()
rv := reflect.ValueOf(i)
switch {
case kind == reflect.String:
result = rv.String()
case kind >= reflect.Int && kind <= reflect.Int64:
result = strconv.FormatInt(rv.Int(), 10)
case kind >= reflect.Uint && kind <= reflect.Uint64:
result = strconv.FormatUint(rv.Uint(), 10)
case kind >= reflect.Float32 && kind <= reflect.Float64:
result = strconv.FormatFloat(rv.Float(), 'f', -1, 64)
case kind == reflect.Bool:
if rv.Bool() {
result = "1"
} else {
result = "0"
}
}
return result
}
func ConvertToInt(i interface{}) int64 {
var result int64
if reflect.TypeOf(i) == nil {
return result
}
kind := reflect.TypeOf(i).Kind()
rv := reflect.ValueOf(i)
switch {
case kind == reflect.String:
result, _ = strconv.ParseInt(rv.String(), 10, 64)
case kind >= reflect.Int && kind <= reflect.Int64:
result = rv.Int()
case kind >= reflect.Uint && kind <= reflect.Uint64:
result = int64(rv.Uint())
case kind >= reflect.Float32 && kind <= reflect.Float64:
result = int64(rv.Float())
case kind == reflect.Bool:
if rv.Bool() {
result = int64(1)
} else {
result = (int64)(0)
}
}
return result
}
func ConvertToFloat(i interface{}) float64 {
var result float64
if reflect.TypeOf(i) == nil {
return result
}
kind := reflect.TypeOf(i).Kind()
rv := reflect.ValueOf(i)
switch {
case kind == reflect.String:
result, _ = strconv.ParseFloat(rv.String(), 64)
case kind >= reflect.Int && kind <= reflect.Int64:
result = float64(rv.Int())
case kind >= reflect.Uint && kind <= reflect.Uint64:
result = float64(rv.Uint())
case kind >= reflect.Float32 && kind <= reflect.Float64:
result = float64(rv.Float())
case kind == reflect.Bool:
if rv.Bool() {
result = float64(1)
} else {
result = (float64)(0)
}
}
return result
}
func ConvertStringToMap(evalStr string) map[string]string {
m := make(map[string]string)
if !strings.HasPrefix(evalStr, " ") {
evalStr = " " + evalStr
}
kvs := strings.Split(evalStr, " --")
for _, v := range kvs {
if vs := strings.SplitN(v, "=", 2); len(vs) == 2 {
m[vs[0]] = vs[1]
}
}
return m
}