Skip to content

Commit

Permalink
service/logs: use strings.Cut
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastiaan van Stijn <[email protected]>
  • Loading branch information
thaJeztah committed Dec 29, 2022
1 parent 3fa1863 commit 42de5cc
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 23 deletions.
11 changes: 6 additions & 5 deletions service/logs/parse_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ func ParseLogDetails(details string) (map[string]string, error) {
pairs := strings.Split(details, ",")
detailsMap := make(map[string]string, len(pairs))
for _, pair := range pairs {
p := strings.SplitN(pair, "=", 2)
// if there is no equals sign, we will only get 1 part back
if len(p) != 2 {
k, v, ok := strings.Cut(pair, "=")
if !ok || k == "" {
// missing equal sign, or no key.
return nil, errors.New("invalid details format")
}
k, err := url.QueryUnescape(p[0])
var err error
k, err = url.QueryUnescape(k)
if err != nil {
return nil, err
}
v, err := url.QueryUnescape(p[1])
v, err = url.QueryUnescape(v)
if err != nil {
return nil, err
}
Expand Down
62 changes: 44 additions & 18 deletions service/logs/parse_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,59 @@ package logs
import (
"testing"

"github.com/pkg/errors"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)

func TestParseLogDetails(t *testing.T) {
testCases := []struct {
line string
expected map[string]string
err error
line string
expected map[string]string
expectedErr string
}{
{"key=value", map[string]string{"key": "value"}, nil},
{"key1=value1,key2=value2", map[string]string{"key1": "value1", "key2": "value2"}, nil},
{"key+with+spaces=value%3Dequals,asdf%2C=", map[string]string{"key with spaces": "value=equals", "asdf,": ""}, nil},
{"key=,=nothing", map[string]string{"key": "", "": "nothing"}, nil},
{"=", map[string]string{"": ""}, nil},
{"errors", nil, errors.New("invalid details format")},
{
line: "key=value",
expected: map[string]string{"key": "value"},
},
{
line: "key1=value1,key2=value2",
expected: map[string]string{"key1": "value1", "key2": "value2"},
},
{
line: "key+with+spaces=value%3Dequals,asdf%2C=",
expected: map[string]string{"key with spaces": "value=equals", "asdf,": ""},
},
{
line: "key=,key2=",
expected: map[string]string{"key": "", "key2": ""},
},
{
line: "key=,=nothing",
expectedErr: "invalid details format",
},
{
line: "=nothing",
expectedErr: "invalid details format",
},
{
line: "=",
expectedErr: "invalid details format",
},
{
line: "errors",
expectedErr: "invalid details format",
},
}
for _, testcase := range testCases {
testcase := testcase
t.Run(testcase.line, func(t *testing.T) {
actual, err := ParseLogDetails(testcase.line)
if testcase.err != nil {
assert.Error(t, err, testcase.err.Error())
return
for _, tc := range testCases {
tc := tc
t.Run(tc.line, func(t *testing.T) {
actual, err := ParseLogDetails(tc.line)
if tc.expectedErr != "" {
assert.Check(t, is.ErrorContains(err, tc.expectedErr))
} else {
assert.Check(t, err)
}
assert.Check(t, is.DeepEqual(testcase.expected, actual))
assert.Check(t, is.DeepEqual(tc.expected, actual))
})
}
}

0 comments on commit 42de5cc

Please sign in to comment.