Skip to content

Commit 2190485

Browse files
authored
fix(config): enables optional trailing semicolon in config string (#32)
1 parent 0e8ef7d commit 2190485

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

conf_parse.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func parseConfigStr(conf string) (configData, error) {
186186
}
187187

188188
if !strings.HasSuffix(conf, ";") {
189-
return result, NewInvalidConfigStrError("trailing semicolon ';' required")
189+
conf += ";"
190190
}
191191

192192
keyValueStr := []rune(conf)
@@ -216,6 +216,10 @@ func parseConfigStr(conf string) (configData, error) {
216216
continue
217217
}
218218

219+
if value.Len() == 0 {
220+
return result, NewInvalidConfigStrError("empty value for key %q", key)
221+
}
222+
219223
result.KeyValuePairs[key.String()] = value.String()
220224

221225
key.Reset()

conf_test.go

+36-6
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,16 @@ func TestParserHappyCases(t *testing.T) {
213213
},
214214
},
215215
},
216+
{
217+
name: "no trailing semicolon",
218+
config: fmt.Sprintf("http::addr=%s", addr),
219+
expected: qdb.ConfigData{
220+
Schema: "http",
221+
KeyValuePairs: map[string]string{
222+
"addr": addr,
223+
},
224+
},
225+
},
216226
}
217227

218228
for _, tc := range testCases {
@@ -231,6 +241,11 @@ func TestParserPathologicalCases(t *testing.T) {
231241
config: "",
232242
expectedErrMsgContains: "no schema separator found",
233243
},
244+
{
245+
name: "empty config with semicolon",
246+
config: ";",
247+
expectedErrMsgContains: "no schema separator found",
248+
},
234249
{
235250
name: "no schema",
236251
config: "addr=localhost:9000",
@@ -242,10 +257,15 @@ func TestParserPathologicalCases(t *testing.T) {
242257
expectedErrMsgContains: "'addr' key not found",
243258
},
244259
{
245-
name: "unescaped semicolon in password leads to invalid key character",
260+
name: "unescaped semicolon in password leads to unexpected end of string (with trailing semicolon)",
246261
config: "http::addr=localhost:9000;username=test;password=pass;word;",
247262
expectedErrMsgContains: "unexpected end of",
248263
},
264+
{
265+
name: "unescaped semicolon in password leads to unexpected end of string (no trailing semicolon)",
266+
config: "http::addr=localhost:9000;username=test;password=pass;word",
267+
expectedErrMsgContains: "unexpected end of",
268+
},
249269
}
250270

251271
for _, tc := range testCases {
@@ -344,7 +364,7 @@ func TestHappyCasesFromConf(t *testing.T) {
344364
},
345365
{
346366
name: "password before username",
347-
config: fmt.Sprintf("http::addr=%s;password=%s;username=%s;",
367+
config: fmt.Sprintf("http::addr=%s;password=%s;username=%s",
348368
addr, pass, user),
349369
expectedOpts: []qdb.LineSenderOption{
350370
qdb.WithHttp(),
@@ -364,7 +384,7 @@ func TestHappyCasesFromConf(t *testing.T) {
364384
},
365385
{
366386
name: "bearer token",
367-
config: fmt.Sprintf("http::addr=%s;token=%s;",
387+
config: fmt.Sprintf("http::addr=%s;token=%s",
368388
addr, token),
369389
expectedOpts: []qdb.LineSenderOption{
370390
qdb.WithHttp(),
@@ -453,9 +473,19 @@ func TestPathologicalCasesFromConf(t *testing.T) {
453473
expectedErrMsgContains: "unsupported option",
454474
},
455475
{
456-
name: "trailing semicolon required",
457-
config: "http::addr=localhost:9000",
458-
expectedErrMsgContains: "trailing semicolon",
476+
name: "partial key at end",
477+
config: "http::addr=localhost:9000;test",
478+
expectedErrMsgContains: "unexpected end of string",
479+
},
480+
{
481+
name: "no value at end",
482+
config: "http::addr=localhost:9000;username=",
483+
expectedErrMsgContains: "empty value for key",
484+
},
485+
{
486+
name: "no value at end with semicolon",
487+
config: "http::addr=localhost:9000;username=;",
488+
expectedErrMsgContains: "empty value for key",
459489
},
460490
}
461491

0 commit comments

Comments
 (0)