diff --git a/text_formatter.go b/text_formatter.go index e125015..6aa48cf 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -169,22 +169,15 @@ func (f *TextFormatter) appendKeyValue(b *bytes.Buffer, key string, value interf } func (f *TextFormatter) appendValue(b *bytes.Buffer, value interface{}) { - switch value := value.(type) { - case string: - if !f.needsQuoting(value) { - b.WriteString(value) - } else { - b.WriteString(f.quoteString(value)) - } - case error: - errmsg := value.Error() - if !f.needsQuoting(errmsg) { - b.WriteString(errmsg) - } else { - b.WriteString(f.quoteString(errmsg)) - } - default: - fmt.Fprint(b, value) + stringVal, ok := value.(string) + if !ok { + stringVal = fmt.Sprint(value) + } + + if !f.needsQuoting(stringVal) { + b.WriteString(stringVal) + } else { + b.WriteString(f.quoteString(stringVal)) } } diff --git a/text_formatter_test.go b/text_formatter_test.go index 1fc55ae..d7b3bcb 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,27 @@ func TestEscaping_DefaultQuoteCharacter(t *testing.T) { } } +func TestEscaping_Interface(t *testing.T) { + tf := &TextFormatter{DisableColors: true} + + ts := time.Now() + + 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) + } + } +} + func TestEscaping_CustomQuoteCharacter(t *testing.T) { tf := &TextFormatter{DisableColors: true}