1
0
mirror of https://github.com/amir20/dozzle.git synced 2025-12-24 06:28:42 +01:00

fix: supports empty values for logfmt (#3300)

This commit is contained in:
Amir Raminfar
2024-09-30 11:17:17 -07:00
committed by GitHub
parent b0a4a4c6e4
commit 36e5abfa64
2 changed files with 21 additions and 12 deletions

View File

@@ -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 {
@@ -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
@@ -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)
}

View File

@@ -9,7 +9,6 @@ import (
)
func TestParseLog(t *testing.T) {
tests := []struct {
name string
log string
@@ -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",