From 4c4851c96ab0aefe478354f1cbce49060b3c474f Mon Sep 17 00:00:00 2001 From: Paul Seiffert Date: Wed, 12 Jul 2017 17:16:13 +0200 Subject: [PATCH] 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)) } }