-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshal.go
174 lines (154 loc) · 3.67 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
package clickhouse
import (
"fmt"
"reflect"
"strconv"
"strings"
"time"
)
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 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 *[]string:
if !isArray(data) {
//noinspection GoPlaceholderCount
return fmt.Errorf("Column data is not of type []string")
}
if isEmptyArray(data) {
*v = []string{}
return
}
items := splitStringToItems(data)
res := make([]string, len(items))
for i := 0; i < len(items); i++ {
var s string
unmarshal(&s, items[i])
res[i] = string(s[1 : len(s)-1])
}
*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)
}
return "''"
}