Skip to content

Commit

Permalink
fix: supports empty values for logfmt (#3300)
Browse files Browse the repository at this point in the history
  • Loading branch information
amir20 authored Sep 30, 2024
1 parent b0a4a4c commit 36e5abf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
7 changes: 0 additions & 7 deletions internal/docker/logfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ func ParseLogFmt(log string) (*orderedmap.OrderedMap[string, string], error) {

for i := 0; i < len(log); i++ {
char := log[i]

if isKey {
if char == '=' {
if i == start {
Expand Down Expand Up @@ -48,9 +47,6 @@ func ParseLogFmt(log string) (*orderedmap.OrderedMap[string, string], error) {
inQuotes = true
start = i + 1
} else if char == ' ' {
if i == start {
return nil, errors.New("invalid format: value is empty")
}
value = log[start:i]
result.Set(key, value)
isKey = true
Expand All @@ -75,9 +71,6 @@ func ParseLogFmt(log string) (*orderedmap.OrderedMap[string, string], error) {
if inQuotes {
return nil, errors.New("invalid format: unclosed quotes")
}
if start >= len(log) {
return nil, errors.New("invalid format: value is empty")
}
value = log[start:]
result.Set(key, value)
}
Expand Down
26 changes: 21 additions & 5 deletions internal/docker/logfmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
)

func TestParseLog(t *testing.T) {

tests := []struct {
name string
log string
Expand All @@ -35,10 +34,27 @@ func TestParseLog(t *testing.T) {
wantErr: true,
},
{
name: "Invalid log with key without value",
log: "key1=value1 key2=",
want: nil,
wantErr: true,
name: "Valid log with key and trailing no value",
log: "key1=value1 key2=",
want: orderedmap.New[string, string](
orderedmap.WithInitialData(
orderedmap.Pair[string, string]{Key: "key1", Value: "value1"},
orderedmap.Pair[string, string]{Key: "key2", Value: ""},
),
),
wantErr: false,
},
{
name: "Valid log with key and no values",
log: "key1=value1 key2= key3=bar",
want: orderedmap.New[string, string](
orderedmap.WithInitialData(
orderedmap.Pair[string, string]{Key: "key1", Value: "value1"},
orderedmap.Pair[string, string]{Key: "key2", Value: ""},
orderedmap.Pair[string, string]{Key: "key3", Value: "bar"},
),
),
wantErr: false,
},
{
name: "Valid log",
Expand Down

0 comments on commit 36e5abf

Please sign in to comment.