Skip to content

fix(config): enables optional trailing semicolon in config string #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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