-
Notifications
You must be signed in to change notification settings - Fork 4
/
helpers.go
112 lines (87 loc) · 3.07 KB
/
helpers.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
package db
import (
"github.com/ottemo/commerce/env"
"github.com/ottemo/commerce/utils"
"strings"
)
// GetCollection returns database collection or error otherwise
func GetCollection(CollectionName string) (InterfaceDBCollection, error) {
dbEngine := GetDBEngine()
if dbEngine == nil {
return nil, env.ErrorNew(ConstErrorModule, ConstErrorLevel, "7f379d36-eb93-4add-b1ee-df2c3a35a590", "Can't get DBEngine")
}
return dbEngine.GetCollection(CollectionName)
}
// ConvertTypeFromDbToGo returns object that represents GO side value for given valueType
func ConvertTypeFromDbToGo(value interface{}, valueType string) interface{} {
switch {
case strings.HasPrefix(valueType, "[]"):
var result []interface{}
if value == nil {
return result
}
stringValue := utils.InterfaceToString(value)
if stringValue == "" {
return result
}
jsonArray, err := utils.DecodeJSONToArray(stringValue)
if err == nil {
return jsonArray
}
array := strings.Split(stringValue, ", ")
arrayType := strings.TrimPrefix(valueType, "[]")
for _, arrayValue := range array {
arrayValue = strings.Replace(arrayValue, "#2C;", ",", -1)
result = append(result, ConvertTypeFromDbToGo(arrayValue, arrayType))
}
return result
case strings.HasPrefix(valueType, ConstTypeBoolean):
return utils.InterfaceToBool(value)
case strings.HasPrefix(valueType, ConstTypeInteger):
return utils.InterfaceToInt(value)
case strings.HasPrefix(valueType, ConstTypeDecimal),
strings.HasPrefix(valueType, ConstTypeFloat),
strings.HasPrefix(valueType, ConstTypeMoney):
return utils.InterfaceToFloat64(value)
case valueType == ConstTypeDatetime:
return utils.InterfaceToTime(value)
case valueType == ConstTypeJSON:
resultMap, err := utils.DecodeJSONToStringKeyMap(value)
if err == nil {
return resultMap
}
result, _ := utils.DecodeJSONToInterface(value)
return result
case strings.HasPrefix(valueType, ConstTypeVarchar), valueType == ConstTypeText, valueType == ConstTypeID:
return utils.InterfaceToString(value)
}
return value
}
// TypeParse shortcut for utils.DataTypeParse
func TypeParse(typeName string) utils.DataType {
return utils.DataTypeParse(typeName)
}
// TypeWPrecisionAndScale shortcut for utils.DataTypeWPrecisionAndScale
func TypeWPrecisionAndScale(dataType string, precision int, scale int) string {
return utils.DataTypeWPrecisionAndScale(dataType, precision, scale)
}
// TypeWPrecision shortcut for utils.DataTypeWPrecision
func TypeWPrecision(dataType string, precision int) string {
return utils.DataTypeWPrecision(dataType, precision)
}
// TypeArrayOf shortcut for utils.DataTypeArrayOf
func TypeArrayOf(dataType string) string {
return utils.DataTypeArrayOf(dataType)
}
// TypeIsArray shortcut for utils.DataTypeIsArray
func TypeIsArray(dataType string) bool {
return utils.DataTypeIsArray(dataType)
}
// TypeIsString shortcut for utils.DataTypeIsString
func TypeIsString(dataType string) bool {
return utils.DataTypeIsString(dataType)
}
// TypeIsFloat shortcut for utils.DataTypeIsFloat
func TypeIsFloat(dataType string) bool {
return utils.DataTypeIsFloat(dataType)
}