From 9d980d3d4d0342c1f0d300a4c6f2d5659f305dd5 Mon Sep 17 00:00:00 2001 From: Adrian Lungu Date: Fri, 13 May 2016 23:36:30 +0300 Subject: [PATCH] Add switch to 'unquoteIfQuoted' to check for string as well as []byte types. --- decimal.go | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/decimal.go b/decimal.go index 287f2c5..4f0459e 100644 --- a/decimal.go +++ b/decimal.go @@ -638,15 +638,23 @@ func round(n float64) int64 { } func unquoteIfQuoted(value interface{}) (string, error) { - bytes, ok := value.([]byte) - if !ok { - return "", fmt.Errorf("Could not convert value '%+v' to byte array", - value) + + switch v := value.(type) { + + case string: + str := strings.Replace(v, `"`, ``, -1) + return str, nil + + case []byte: + if len(v) > 2 && v[0] == '"' && v[len(v)-1] == '"' { + v = v[1 : len(v)-1] + } + return string(v), nil + + default: + return "", fmt.Errorf("Could not convert value '%+v' to any type", + value) + } - // If the amount is quoted, strip the quotes - if len(bytes) > 2 && bytes[0] == '"' && bytes[len(bytes)-1] == '"' { - bytes = bytes[1 : len(bytes)-1] - } - return string(bytes), nil }