From 8879c91b35bc0c2e2b8b1d7da821dbd0530b7d4c Mon Sep 17 00:00:00 2001 From: Chris Rivera Date: Wed, 3 Aug 2016 15:48:35 -0400 Subject: [PATCH] Handle strings being passed to Scan() lib/pq can now pass strings to Scan() as of: https://github.com/lib/pq/commit/e2402a7cd1e57e08a576b94cdfed36ae30366545 --- decimal.go | 10 ++++++++-- decimal_test.go | 22 +++++++++++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/decimal.go b/decimal.go index 287f2c5..ca653d4 100644 --- a/decimal.go +++ b/decimal.go @@ -638,8 +638,14 @@ func round(n float64) int64 { } func unquoteIfQuoted(value interface{}) (string, error) { - bytes, ok := value.([]byte) - if !ok { + var bytes []byte + + switch v := value.(type) { + case string: + bytes = []byte(v) + case []byte: + bytes = v + default: return "", fmt.Errorf("Could not convert value '%+v' to byte array", value) } diff --git a/decimal_test.go b/decimal_test.go index 9175622..b550429 100644 --- a/decimal_test.go +++ b/decimal_test.go @@ -849,7 +849,7 @@ func TestDecimal_Max(t *testing.T) { } func TestDecimal_Scan(t *testing.T) { - // test the Scan method the implements the + // test the Scan method that implements the // sql.Scanner interface // check for the for different type of values // that are possible to be received from the database @@ -895,6 +895,9 @@ func TestDecimal_Scan(t *testing.T) { value_str := "535.666" dbvalue_str := []byte(value_str) expected, err = NewFromString(value_str) + if err != nil { + t.Fatal(err) + } err = a.Scan(dbvalue_str) if err != nil { @@ -907,6 +910,23 @@ func TestDecimal_Scan(t *testing.T) { t.Errorf("%s does not equal to %s", a, expected) } } + + // lib/pq can also return strings + expected, err = NewFromString(value_str) + if err != nil { + t.Fatal(err) + } + + err = a.Scan(value_str) + if err != nil { + // Scan failed... no need to test result value + t.Errorf("a.Scan('535.666') failed with message: %s", err) + } else { + // Scan suceeded... test resulting values + if !a.Equals(expected) { + t.Errorf("%s does not equal to %s", a, expected) + } + } } // old tests after this line