Skip to content

Commit

Permalink
fix(config): enables optional trailing semicolon in config string (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
sklarsa authored Apr 12, 2024
1 parent 0e8ef7d commit 2190485
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
6 changes: 5 additions & 1 deletion conf_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func parseConfigStr(conf string) (configData, error) {
}

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

keyValueStr := []rune(conf)
Expand Down Expand Up @@ -216,6 +216,10 @@ func parseConfigStr(conf string) (configData, error) {
continue
}

if value.Len() == 0 {
return result, NewInvalidConfigStrError("empty value for key %q", key)
}

result.KeyValuePairs[key.String()] = value.String()

key.Reset()
Expand Down
42 changes: 36 additions & 6 deletions conf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,16 @@ func TestParserHappyCases(t *testing.T) {
},
},
},
{
name: "no trailing semicolon",
config: fmt.Sprintf("http::addr=%s", addr),
expected: qdb.ConfigData{
Schema: "http",
KeyValuePairs: map[string]string{
"addr": addr,
},
},
},
}

for _, tc := range testCases {
Expand All @@ -231,6 +241,11 @@ func TestParserPathologicalCases(t *testing.T) {
config: "",
expectedErrMsgContains: "no schema separator found",
},
{
name: "empty config with semicolon",
config: ";",
expectedErrMsgContains: "no schema separator found",
},
{
name: "no schema",
config: "addr=localhost:9000",
Expand All @@ -242,10 +257,15 @@ func TestParserPathologicalCases(t *testing.T) {
expectedErrMsgContains: "'addr' key not found",
},
{
name: "unescaped semicolon in password leads to invalid key character",
name: "unescaped semicolon in password leads to unexpected end of string (with trailing semicolon)",
config: "http::addr=localhost:9000;username=test;password=pass;word;",
expectedErrMsgContains: "unexpected end of",
},
{
name: "unescaped semicolon in password leads to unexpected end of string (no trailing semicolon)",
config: "http::addr=localhost:9000;username=test;password=pass;word",
expectedErrMsgContains: "unexpected end of",
},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -344,7 +364,7 @@ func TestHappyCasesFromConf(t *testing.T) {
},
{
name: "password before username",
config: fmt.Sprintf("http::addr=%s;password=%s;username=%s;",
config: fmt.Sprintf("http::addr=%s;password=%s;username=%s",
addr, pass, user),
expectedOpts: []qdb.LineSenderOption{
qdb.WithHttp(),
Expand All @@ -364,7 +384,7 @@ func TestHappyCasesFromConf(t *testing.T) {
},
{
name: "bearer token",
config: fmt.Sprintf("http::addr=%s;token=%s;",
config: fmt.Sprintf("http::addr=%s;token=%s",
addr, token),
expectedOpts: []qdb.LineSenderOption{
qdb.WithHttp(),
Expand Down Expand Up @@ -453,9 +473,19 @@ func TestPathologicalCasesFromConf(t *testing.T) {
expectedErrMsgContains: "unsupported option",
},
{
name: "trailing semicolon required",
config: "http::addr=localhost:9000",
expectedErrMsgContains: "trailing semicolon",
name: "partial key at end",
config: "http::addr=localhost:9000;test",
expectedErrMsgContains: "unexpected end of string",
},
{
name: "no value at end",
config: "http::addr=localhost:9000;username=",
expectedErrMsgContains: "empty value for key",
},
{
name: "no value at end with semicolon",
config: "http::addr=localhost:9000;username=;",
expectedErrMsgContains: "empty value for key",
},
}

Expand Down

0 comments on commit 2190485

Please sign in to comment.