Handle strings being passed to Scan()

lib/pq can now pass strings to Scan() as of:
e2402a7cd1
This commit is contained in:
Chris Rivera 2016-08-03 15:48:35 -04:00
parent 74d668a796
commit 8879c91b35
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