-
Notifications
You must be signed in to change notification settings - Fork 3
/
converter.go
132 lines (120 loc) · 2.81 KB
/
converter.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
package casting
import (
"reflect"
"strconv"
"strings"
)
type Converter struct {
inputTypeMap map[string]string
}
func NewConverter(inputTypeMap map[string]string) *Converter {
converter := &Converter{
inputTypeMap: inputTypeMap}
return converter
}
func (converter *Converter) CastSingleElement(inputName string, inputData interface{}) interface{} {
if columnType, ok := converter.inputTypeMap[inputName]; ok {
switch strings.ToLower(columnType) {
case "string":
return castToString(inputData)
case "integer":
return castToInt(inputData)
case "float":
return castToFloat(inputData)
case "boolean":
return castToBool(inputData)
default:
return inputData
}
} else {
return inputData
}
}
func (converter *Converter) CastArrayOfData(inputNameArray []string, inputDataArray []interface{}) []interface{} {
castedColumns := make([]interface{}, len(inputDataArray))
for i := range inputDataArray {
castedColumns[i] = converter.CastSingleElement(inputNameArray[i], inputDataArray[i])
}
return castedColumns
}
func castToString(inputVar interface{}) string {
switch varType := reflect.TypeOf(inputVar).String(); varType {
case "bool":
return strconv.FormatBool(inputVar.(bool))
case "float64":
return strconv.FormatFloat(inputVar.(float64), 'f', -1, 64)
case "int":
return strconv.Itoa(inputVar.(int))
}
return inputVar.(string)
}
func castToInt(inputVar interface{}) int {
switch varType := reflect.TypeOf(inputVar).String(); varType {
case "string":
if strings.TrimSpace(inputVar.(string)) == "" {
return 0
}
res, err := strconv.Atoi(inputVar.(string)) //have to manage this error
if err != nil {
return 0
}
return res
case "bool":
if inputVar.(bool) {
return 1
} else {
return 0
}
case "float64":
return int(inputVar.(float64))
case "int64":
return int(inputVar.(int64))
}
return inputVar.(int)
}
func castToFloat(inputVar interface{}) float64 {
switch varType := reflect.TypeOf(inputVar).String(); varType {
case "string":
if strings.TrimSpace(inputVar.(string)) == "" {
return 0.
}
inputVar = strings.ReplaceAll(inputVar.(string), ",", "")
res, err := strconv.ParseFloat(inputVar.(string), 64) //have to manage this error
if err != nil {
return 0.
}
return res
case "bool":
if inputVar.(bool) {
return 1.
} else {
return 0.
}
case "int":
return float64(inputVar.(int))
}
return inputVar.(float64)
}
func castToBool(inputVar interface{}) bool {
switch varType := reflect.TypeOf(inputVar).String(); varType {
case "string":
if strings.EqualFold("true", inputVar.(string)) {
return true
} else {
return false
}
case "float64":
if inputVar.(float64) == 0. {
return false
} else {
return true
}
case "int":
if inputVar.(int) == 0 {
return false
} else {
return true
}
}
return inputVar.(bool)
}