Skip to content

Commit

Permalink
Merge branch 'master' of github.com:jjeffery/kv
Browse files Browse the repository at this point in the history
# Conflicts:
#	logfmt_test.go
  • Loading branch information
jjeffery committed Jan 23, 2017
2 parents 41c27d8 + a2a9ec5 commit aee178c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
31 changes: 20 additions & 11 deletions logfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import (

// constant byte values
var (
bytesNull = []byte("null")
bytesPanic = []byte(`<PANIC>`)
bytesError = []byte(`<ERROR>`)
bytesNull = []byte("null")
bytesPanic = []byte(`<PANIC>`)
bytesError = []byte(`<ERROR>`)
bytesEmptyK = []byte(`<EMPTY>`)
bytesEmptyV = []byte(`""`)

escapeSequences = map[rune]string{
'\t': `\t`,
Expand Down Expand Up @@ -83,17 +85,12 @@ func writeBytesKey(buf *bytes.Buffer, b []byte) {
buf.Write(bytesNull)
return
}
index := bytes.IndexFunc(b, invalidKey)
if index < 0 {
buf.Write(b)
if len(b) == 0 {
buf.Write(bytesEmptyK)
return
}
if index > 0 {
buf.Write(b[0:index])
b = b[index:]
}
for {
index = bytes.IndexFunc(b, invalidKey)
index := bytes.IndexFunc(b, invalidKey)
if index < 0 {
break
}
Expand All @@ -109,6 +106,10 @@ func writeBytesKey(buf *bytes.Buffer, b []byte) {
}

func writeStringKey(buf *bytes.Buffer, s string) {
if s == "" {
buf.Write(bytesEmptyK)
return
}
index := strings.IndexFunc(s, invalidKey)
if index < 0 {
buf.WriteString(s)
Expand Down Expand Up @@ -182,6 +183,10 @@ func writeBytesValue(buf *bytes.Buffer, b []byte) {
buf.Write(bytesNull)
return
}
if len(b) == 0 {
buf.Write(bytesEmptyV)
return
}
index := bytes.IndexFunc(b, needsQuote)
if index < 0 {
buf.Write(b)
Expand Down Expand Up @@ -210,6 +215,10 @@ func writeBytesValue(buf *bytes.Buffer, b []byte) {
}

func writeStringValue(buf *bytes.Buffer, s string) {
if s == "" {
buf.Write(bytesEmptyV)
return
}
index := strings.IndexFunc(s, needsQuote)
if index < 0 {
buf.WriteString(s)
Expand Down
7 changes: 6 additions & 1 deletion logfmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ func TestWriteKeyValue(t *testing.T) {
value: complex(float64(100.99), float64(1.19e23)),
want: "(1+4i)=(100.99+1.19e+23i)",
},
{
key: "",
value: "",
want: `<EMPTY>=""`,
},
{
key: nil,
value: nil,
Expand Down Expand Up @@ -121,7 +126,7 @@ func TestWriteKeyValue(t *testing.T) {
var buf bytes.Buffer
writeKeyValue(&buf, key, value)
if got := buf.String(); got != want {
t.Errorf("%d: got=%s want=%s", i, got, want)
t.Errorf("%d: got `%s` want `%s`", i, got, want)
}
}
doTest(tt.key, tt.value, tt.want)
Expand Down

0 comments on commit aee178c

Please sign in to comment.