Skip to content

Commit

Permalink
Fix issue when value starts with two quotes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaholaz committed May 13, 2024
1 parent 149137f commit e70d136
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ func parseVal(rawVal string) (*string, error) {
}

// Remove all '"', and replace '\"' with '"'
nonEscapedQuotes := regexp.MustCompile(`[^\\]"`)
val = nonEscapedQuotes.ReplaceAllStringFunc(val, func(s string) string { return s[:len(s)-1] })
if val[0] == '"' {
val = val[1:]
}
nonEscapedQuotes := regexp.MustCompile(`[^\\]"`)
val = nonEscapedQuotes.ReplaceAllStringFunc(val, func(s string) string { return s[:len(s)-1] })
val = strings.ReplaceAll(val, `\"`, `"`)

return &val, nil
Expand Down
24 changes: 24 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,27 @@ Could not parse config correctly with quotes inside correctly.
got: %#v`, want, config)
}
}

func TestTwoQuoteStart(t *testing.T) {
type Config struct {
Foo string
}

config := Config{
Foo: "",
}
err := LoadConfig("test_configs/twoquotesstart.cfg", &config)
if err != nil {
t.Fatalf("Could not parse config with escaped quote: %s", err.Error())
}

want := Config{
Foo: "test",
}
if want != config {
t.Fatalf(`
Could not parse config correctly with quotes inside correctly.
expected: %#v
got: %#v`, want, config)
}
}
1 change: 1 addition & 0 deletions test_configs/twoquotesstart.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Foo = ""test"

0 comments on commit e70d136

Please sign in to comment.