forked from dimkus/go-clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshal.go
238 lines (205 loc) · 4.95 KB
/
marshal.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package clickhouse
import (
"fmt"
"reflect"
"strconv"
"strings"
"time"
"encoding/json"
)
func escape(s string) string {
s = strings.Replace(s, `\`, `\\`, -1)
s = strings.Replace(s, `'`, `\'`, -1)
return s
}
func unescape(s string) string {
s = strings.Replace(s, `\\`, `\`, -1)
s = strings.Replace(s, `\'`, `'`, -1)
return s
}
func isArray(s string) bool {
return strings.HasPrefix(s, "[") && strings.HasSuffix(s, "]")
}
func isEmptyArray(s string) bool {
return s == "[]"
}
func splitStringToItems(s string) []string {
return strings.Split(string(s[1:len(s)-1]), ",")
}
func splitStringToItemsArray(s string) []string {
return strings.Split(string(s[2:len(s)-2]), "','")
}
func unmarshal(value interface{}, data string) (err error) {
var m interface{}
switch v := value.(type) {
case *int:
*v, err = strconv.Atoi(data)
return err
case *int8:
m, err = strconv.ParseInt(data, 10, 8)
*v = int8(m.(int64))
case *int16:
m, err = strconv.ParseInt(data, 10, 16)
*v = int16(m.(int64))
case *int32:
m, err = strconv.ParseInt(data, 10, 32)
*v = int32(m.(int64))
case *int64:
*v, err = strconv.ParseInt(data, 10, 64)
case *float32:
m, err = strconv.ParseFloat(data, 32)
*v = float32(m.(float64))
case *float64:
m, err = strconv.ParseFloat(data, 64)
*v = m.(float64)
case *string:
*v = unescape(data)
case *time.Time:
*v, err = time.ParseInLocation("2006-01-02 15:04:05", data, time.UTC)
case *[]int:
if !isArray(data) {
//noinspection GoPlaceholderCount
return fmt.Errorf("Column data is not of type []int")
}
if isEmptyArray(data) {
*v = []int{}
return
}
items := splitStringToItems(data)
res := make([]int, len(items))
for i := 0; i < len(items); i++ {
unmarshal(&res[i], items[i])
}
*v = res
case *[]float32:
if !isArray(data) {
//noinspection GoPlaceholderCount
return fmt.Errorf("Column data is not of type []int")
}
if isEmptyArray(data) {
*v = []float32{}
return
}
items := splitStringToItems(data)
res := make([]float32, len(items))
for i := 0; i < len(items); i++ {
unmarshal(&res[i], items[i])
}
*v = res
case *[]string:
if !isArray(data) {
//noinspection GoPlaceholderCount
return fmt.Errorf("Column data is not of type []string")
}
if isEmptyArray(data) {
*v = []string{}
return
}
items := splitStringToItemsArray(data)
res := make([]string, len(items))
for i := 0; i < len(items); i++ {
var s string
unmarshal(&s, items[i])
if len(s) > 1 {
res[i] = string(s) //string(s[1: len(s)-1])
} else {
res[i] = ""
}
}
*v = res
case *[]VisitParamsString:
if !isArray(data) {
return fmt.Errorf("Column data is not of type Array")
}
if isEmptyArray(data) {
*v = []VisitParamsString{}
return
}
items := splitStringToItemsArray(data)
res := make([]VisitParamsString, 0)
for _, item := range items {
buffer := VisitParamsString{}
err := json.Unmarshal([]byte(unescape(item)), &buffer)
if err != nil {
return fmt.Errorf(err.Error())
}
res = append(res, buffer)
}
*v = res
case *Array:
if !isArray(data) {
//noinspection GoPlaceholderCount
return fmt.Errorf("Column data is not of type Array")
}
if isEmptyArray(data) {
*v = Array{}
return
}
items := splitStringToItems(data)
res := make(Array, len(items))
var intval int
err = unmarshal(&intval, items[0])
if err == nil {
for i := 0; i < len(items); i++ {
unmarshal(&intval, items[i])
res[i] = intval
}
*v = res
return
}
var floatval float64
err = unmarshal(&floatval, items[0])
if err == nil {
for i := 0; i < len(items); i++ {
unmarshal(&floatval, items[i])
res[i] = floatval
}
*v = res
return
}
var stringval string
err = unmarshal(&stringval, items[0])
if err == nil {
for i := 0; i < len(items); i++ {
unmarshal(&stringval, items[i])
res[i] = string(stringval[1 : len(stringval)-1])
}
*v = res
return
}
default:
return fmt.Errorf("Type %T is not supported for unmarshaling", v)
}
return err
}
func marshal(value interface{}) string {
if reflect.TypeOf(value).Kind() == reflect.Slice {
var res []string
v := reflect.ValueOf(value)
for i := 0; i < v.Len(); i++ {
res = append(res, marshal(v.Index(i).Interface()))
}
return "[" + strings.Join(res, ",") + "]"
}
if t := reflect.TypeOf(value); t.Kind() == reflect.Struct && strings.HasSuffix(t.String(), "Func") {
return fmt.Sprintf("%s(%v)", value.(Func).Name, marshal(value.(Func).Args))
}
switch v := value.(type) {
case string:
return fmt.Sprintf("'%s'", escape(v))
case int, int8, int16, int32, int64,
uint, uint8, uint16, uint32, uint64,
float32, float64:
return fmt.Sprintf("%v", v)
//https://clickhouse.yandex/reference_en.html#Boolean values
case bool:
if value.(bool) {
return "1"
}
return "0"
//Convert time to Date type https://clickhouse.yandex/reference_en.html#Date
case time.Time:
return value.(time.Time).Format("2006-01-02")
}
return "''"
}