Merge pull request #27 from chrismrivera/master

Handle strings being passed to Scan()
This commit is contained in:
Vadim Graboys 2016-09-18 12:47:28 -07:00 committed by GitHub
commit 61927b7caf
2 changed files with 29 additions and 3 deletions

View file

@ -638,8 +638,14 @@ func round(n float64) int64 {
} }
func unquoteIfQuoted(value interface{}) (string, error) { func unquoteIfQuoted(value interface{}) (string, error) {
bytes, ok := value.([]byte) var bytes []byte
if !ok {
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", return "", fmt.Errorf("Could not convert value '%+v' to byte array",
value) value)
} }

View file

@ -849,7 +849,7 @@ func TestDecimal_Max(t *testing.T) {
} }
func TestDecimal_Scan(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 // sql.Scanner interface
// check for the for different type of values // check for the for different type of values
// that are possible to be received from the database // that are possible to be received from the database
@ -895,6 +895,9 @@ func TestDecimal_Scan(t *testing.T) {
value_str := "535.666" value_str := "535.666"
dbvalue_str := []byte(value_str) dbvalue_str := []byte(value_str)
expected, err = NewFromString(value_str) expected, err = NewFromString(value_str)
if err != nil {
t.Fatal(err)
}
err = a.Scan(dbvalue_str) err = a.Scan(dbvalue_str)
if err != nil { if err != nil {
@ -907,6 +910,23 @@ func TestDecimal_Scan(t *testing.T) {
t.Errorf("%s does not equal to %s", a, expected) 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 // old tests after this line