From b9cfd8264512c3534698605bc841c6b71521a69f Mon Sep 17 00:00:00 2001 From: Paul Seiffert Date: Wed, 12 Jul 2017 17:15:13 +0200 Subject: [PATCH 1/4] Quote non-string values if necessary --- text_formatter.go | 7 ++++++- text_formatter_test.go | 11 +++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/text_formatter.go b/text_formatter.go index e125015..6f573a2 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -184,7 +184,12 @@ func (f *TextFormatter) appendValue(b *bytes.Buffer, value interface{}) { b.WriteString(f.quoteString(errmsg)) } default: - fmt.Fprint(b, value) + s := fmt.Sprint(value) + if !f.needsQuoting(s) { + b.WriteString(s) + } else { + b.WriteString(f.quoteString(s)) + } } } diff --git a/text_formatter_test.go b/text_formatter_test.go index 1fc55ae..93f47b7 100644 --- a/text_formatter_test.go +++ b/text_formatter_test.go @@ -6,6 +6,7 @@ import ( "strings" "testing" "time" + "fmt" ) func TestQuoting(t *testing.T) { @@ -83,6 +84,16 @@ func TestEscaping_DefaultQuoteCharacter(t *testing.T) { } } +func TestEscaping_Time(t *testing.T) { + tf := &TextFormatter{DisableColors: true} + ts := time.Now() + + b, _ := tf.Format(WithField("test", ts)) + if !bytes.Contains(b, []byte(fmt.Sprintf("\"%s\"", ts.Format("2006-01-02 15:04:05.999999999 -0700 MST")))) { + t.Errorf("escaping expected for %q (result was %q)", ts, string(b)) + } +} + func TestEscaping_CustomQuoteCharacter(t *testing.T) { tf := &TextFormatter{DisableColors: true} From 4c4851c96ab0aefe478354f1cbce49060b3c474f Mon Sep 17 00:00:00 2001 From: Paul Seiffert Date: Wed, 12 Jul 2017 17:16:13 +0200 Subject: [PATCH 2/4] Reduce duplicate code --- text_formatter.go | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/text_formatter.go b/text_formatter.go index 6f573a2..6fe24fc 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -169,27 +169,21 @@ func (f *TextFormatter) appendKeyValue(b *bytes.Buffer, key string, value interf } func (f *TextFormatter) appendValue(b *bytes.Buffer, value interface{}) { + var stringVal string + switch value := value.(type) { case string: - if !f.needsQuoting(value) { - b.WriteString(value) - } else { - b.WriteString(f.quoteString(value)) - } + stringVal = value case error: - errmsg := value.Error() - if !f.needsQuoting(errmsg) { - b.WriteString(errmsg) - } else { - b.WriteString(f.quoteString(errmsg)) - } + stringVal = value.Error() default: - s := fmt.Sprint(value) - if !f.needsQuoting(s) { - b.WriteString(s) - } else { - b.WriteString(f.quoteString(s)) - } + stringVal = fmt.Sprint(value) + } + + if !f.needsQuoting(stringVal) { + b.WriteString(stringVal) + } else { + b.WriteString(f.quoteString(stringVal)) } } From 5f89343f84f01bd781cfc680d045148ea0a6d178 Mon Sep 17 00:00:00 2001 From: Paul Seiffert Date: Wed, 12 Jul 2017 17:29:56 +0200 Subject: [PATCH 3/4] Generalize test case --- text_formatter_test.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/text_formatter_test.go b/text_formatter_test.go index 93f47b7..d7b3bcb 100644 --- a/text_formatter_test.go +++ b/text_formatter_test.go @@ -84,13 +84,24 @@ func TestEscaping_DefaultQuoteCharacter(t *testing.T) { } } -func TestEscaping_Time(t *testing.T) { +func TestEscaping_Interface(t *testing.T) { tf := &TextFormatter{DisableColors: true} + ts := time.Now() - b, _ := tf.Format(WithField("test", ts)) - if !bytes.Contains(b, []byte(fmt.Sprintf("\"%s\"", ts.Format("2006-01-02 15:04:05.999999999 -0700 MST")))) { - t.Errorf("escaping expected for %q (result was %q)", ts, string(b)) + testCases := []struct { + value interface{} + expected string + }{ + {ts, fmt.Sprintf("\"%s\"", ts.String())}, + {errors.New("error: something went wrong"), "\"error: something went wrong\""}, + } + + for _, tc := range testCases { + b, _ := tf.Format(WithField("test", tc.value)) + if !bytes.Contains(b, []byte(tc.expected)) { + t.Errorf("escaping expected for %q (result was %q instead of %q)", tc.value, string(b), tc.expected) + } } } From b019ab48c544b5429f6c2f2908a5f86f235daefb Mon Sep 17 00:00:00 2001 From: Paul Seiffert Date: Wed, 12 Jul 2017 17:33:04 +0200 Subject: [PATCH 4/4] Error is not a special case anymore --- text_formatter.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/text_formatter.go b/text_formatter.go index 6fe24fc..6aa48cf 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -169,14 +169,8 @@ func (f *TextFormatter) appendKeyValue(b *bytes.Buffer, key string, value interf } func (f *TextFormatter) appendValue(b *bytes.Buffer, value interface{}) { - var stringVal string - - switch value := value.(type) { - case string: - stringVal = value - case error: - stringVal = value.Error() - default: + stringVal, ok := value.(string) + if !ok { stringVal = fmt.Sprint(value) }