-
Notifications
You must be signed in to change notification settings - Fork 9
/
conf_parse.go
257 lines (233 loc) · 6.46 KB
/
conf_parse.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*******************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* Copyright (c) 2014-2019 Appsicle
* Copyright (c) 2019-2022 QuestDB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
package questdb
import (
"fmt"
"strconv"
"strings"
"time"
)
type configData struct {
Schema string
KeyValuePairs map[string]string
}
func confFromStr(conf string) (*lineSenderConfig, error) {
var senderConf *lineSenderConfig
data, err := parseConfigStr(conf)
if err != nil {
return nil, err
}
switch data.Schema {
case "http":
senderConf = newLineSenderConfig(httpSenderType)
case "https":
senderConf = newLineSenderConfig(httpSenderType)
senderConf.tlsMode = tlsEnabled
case "tcp":
senderConf = newLineSenderConfig(tcpSenderType)
case "tcps":
senderConf = newLineSenderConfig(tcpSenderType)
senderConf.tlsMode = tlsEnabled
default:
return nil, fmt.Errorf("invalid schema: %s", data.Schema)
}
for k, v := range data.KeyValuePairs {
switch k {
case "addr":
senderConf.address = v
case "username":
switch senderConf.senderType {
case httpSenderType:
senderConf.httpUser = v
case tcpSenderType:
senderConf.tcpKeyId = v
default:
panic("add a case for " + k)
}
case "password":
if senderConf.senderType != httpSenderType {
return nil, NewInvalidConfigStrError("%s is only supported for HTTP sender", k)
}
senderConf.httpPass = v
case "token":
switch senderConf.senderType {
case httpSenderType:
senderConf.httpToken = v
case tcpSenderType:
senderConf.tcpKey = v
default:
panic("add a case for " + k)
}
case "token_x":
case "token_y":
// Some clients require public key.
// But since Go sender doesn't need it, we ignore the values.
continue
case "auto_flush":
if v == "off" {
senderConf.autoFlushRows = 0
senderConf.autoFlushInterval = 0
} else if v != "on" {
return nil, NewInvalidConfigStrError("invalid %s value, %q is not 'on' or 'off'", k, v)
}
case "auto_flush_rows":
if v == "off" {
senderConf.autoFlushRows = 0
continue
}
parsedVal, err := strconv.Atoi(v)
if err != nil {
return nil, NewInvalidConfigStrError("invalid %s value, %q is not a valid int", k, v)
}
senderConf.autoFlushRows = parsedVal
case "auto_flush_interval":
if v == "off" {
senderConf.autoFlushInterval = 0
continue
}
parsedVal, err := strconv.Atoi(v)
if err != nil {
return nil, NewInvalidConfigStrError("invalid %s value, %q is not a valid int", k, v)
}
senderConf.autoFlushInterval = time.Duration(parsedVal) * time.Millisecond
case "request_min_throughput", "init_buf_size", "max_buf_size":
parsedVal, err := strconv.Atoi(v)
if err != nil {
return nil, NewInvalidConfigStrError("invalid %s value, %q is not a valid int", k, v)
}
switch k {
case "request_min_throughput":
senderConf.minThroughput = parsedVal
case "init_buf_size":
senderConf.initBufSize = parsedVal
case "max_buf_size":
senderConf.maxBufSize = parsedVal
default:
panic("add a case for " + k)
}
case "request_timeout", "retry_timeout":
timeout, err := strconv.Atoi(v)
if err != nil {
return nil, NewInvalidConfigStrError("invalid %s value, %q is not a valid int", k, v)
}
timeoutDur := time.Duration(timeout) * time.Millisecond
switch k {
case "request_timeout":
senderConf.requestTimeout = timeoutDur
case "retry_timeout":
senderConf.retryTimeout = timeoutDur
default:
panic("add a case for " + k)
}
case "tls_verify":
switch v {
case "on":
senderConf.tlsMode = tlsEnabled
case "unsafe_off":
senderConf.tlsMode = tlsInsecureSkipVerify
default:
return nil, NewInvalidConfigStrError("invalid tls_verify value, %q is not 'on' or 'unsafe_off", v)
}
case "tls_roots":
return nil, NewInvalidConfigStrError("tls_roots is not available in the go client")
case "tls_roots_password":
return nil, NewInvalidConfigStrError("tls_roots_password is not available in the go client")
default:
return nil, NewInvalidConfigStrError("unsupported option %q", k)
}
}
return senderConf, nil
}
func parseConfigStr(conf string) (configData, error) {
var (
key = &strings.Builder{}
value = &strings.Builder{}
isKey = true
result = configData{
KeyValuePairs: map[string]string{},
}
nextRune rune
isEscaping bool
)
schemaStr, conf, found := strings.Cut(conf, "::")
if !found {
return result, NewInvalidConfigStrError("no schema separator found '::'")
}
result.Schema = schemaStr
if len(conf) == 0 {
return result, NewInvalidConfigStrError("'addr' key not found")
}
if !strings.HasSuffix(conf, ";") {
conf += ";"
}
keyValueStr := []rune(conf)
for idx, rune := range keyValueStr {
if idx < len(conf)-1 {
nextRune = keyValueStr[idx+1]
} else {
nextRune = 0
}
switch rune {
case ';':
if isKey {
if nextRune == 0 {
return result, NewInvalidConfigStrError("unexpected end of string")
}
return result, NewInvalidConfigStrError("invalid key character ';'")
}
if !isEscaping && nextRune == ';' {
isEscaping = true
continue
}
if isEscaping {
value.WriteRune(rune)
isEscaping = false
continue
}
if value.Len() == 0 {
return result, NewInvalidConfigStrError("empty value for key %q", key)
}
result.KeyValuePairs[key.String()] = value.String()
key.Reset()
value.Reset()
isKey = true
case '=':
if isKey {
isKey = false
} else {
value.WriteRune(rune)
}
default:
if isKey {
key.WriteRune(rune)
} else {
value.WriteRune(rune)
}
}
}
if isEscaping {
return result, NewInvalidConfigStrError("unescaped ';'")
}
return result, nil
}