Skip to content

Commit

Permalink
fix(parse): get removes quotes only if they match on both sides of th…
Browse files Browse the repository at this point in the history
…e string
  • Loading branch information
qdm12 committed Nov 12, 2024
1 parent bf1e24c commit 40b5b2c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
25 changes: 18 additions & 7 deletions internal/parse/get.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package parse

import "strings"
import (
"slices"
"strings"
)

// Get returns the first value found at the given key from
// the given sources in order, as a string pointer.
Expand Down Expand Up @@ -91,15 +94,23 @@ func postProcessValue(value string, settings settings) string {
cutSet["\n"] = struct{}{}
}

if *settings.trimQuotes {
cutSet[`"`] = struct{}{}
cutSet[`'`] = struct{}{}
}

cutSetString := ""
for s := range cutSet {
cutSetString += s
}

return strings.Trim(value, cutSetString)
quotes := []rune{'\'', '"', '`'}
for {
value = strings.Trim(value, cutSetString)
const minCharactersForQuotes = 2
if *settings.trimQuotes &&
len(value) >= minCharactersForQuotes &&
slices.Contains(quotes, rune(value[0])) &&
slices.Contains(quotes, rune(value[len(value)-1])) {
value = value[1 : len(value)-1] // remove quotes
continue // re-trim cut set
}
break
}
return value
}
4 changes: 2 additions & 2 deletions internal/parse/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,14 @@ func Test_postProcessValue(t *testing.T) {
result: "value",
},
"combined": {
value: "\t\"'Value\"'\t\n\t\r\n\t",
value: "\t\"'Value 'hello'\"'\t\n\t\r\n\t",
settings: settings{
trimLineEndings: ptrTo(true),
trimSpace: ptrTo(true),
trimQuotes: ptrTo(true),
forceLowercase: ptrTo(true),
},
result: "value",
result: "value 'hello'",
},
}

Expand Down

0 comments on commit 40b5b2c

Please sign in to comment.