common: simplify sql driver methods

This commit is contained in:
Felix Lange 2018-07-24 14:40:59 +02:00
parent 25556b2aa7
commit 73fea57e7f
2 changed files with 12 additions and 84 deletions

View file

@ -122,28 +122,22 @@ func (h Hash) Generate(rand *rand.Rand, size int) reflect.Value {
return reflect.ValueOf(h)
}
// Scan implements Scanner for database/sql
// Scan implements Scanner for database/sql.
func (h *Hash) Scan(src interface{}) error {
srcB, ok := src.([]byte)
if !ok {
return fmt.Errorf("Hash Scan: couldn't scan %v into Hash", src)
return fmt.Errorf("can't scan %T into Hash", src)
}
if len(srcB) != HashLength {
return fmt.Errorf(
"Hash Scan: len %d instead of expected %d",
len(srcB),
HashLength,
)
return fmt.Errorf("can't scan []byte of len %d into Hash, want %d", len(srcB), HashLength)
}
*h = BytesToHash(srcB)
copy(h[:], srcB)
return nil
}
// Value implements valuer for database/sql
// Value implements valuer for database/sql.
func (h Hash) Value() (driver.Value, error) {
return h.Bytes(), nil
return h[:], nil
}
// UnprefixedHash allows marshaling a Hash without 0x prefix.
@ -255,28 +249,22 @@ func (a *Address) UnmarshalJSON(input []byte) error {
return hexutil.UnmarshalFixedJSON(addressT, input, a[:])
}
// Scan implements Scanner for database/sql
// Scan implements Scanner for database/sql.
func (a *Address) Scan(src interface{}) error {
srcB, ok := src.([]byte)
if !ok {
return fmt.Errorf("Address Scan: couldn't scan %v into Address", src)
return fmt.Errorf("can't scan %T into Address", src)
}
if len(srcB) != AddressLength {
return fmt.Errorf(
"Address Scan: len %d instead of expected %d",
len(srcB),
AddressLength,
)
return fmt.Errorf("can't scan []byte of len %d into Address, want %d", len(srcB), AddressLength)
}
*a = BytesToAddress(srcB)
copy(a[:], srcB)
return nil
}
// Value implements valuer for database/sql
// Value implements valuer for database/sql.
func (a Address) Value() (driver.Value, error) {
return a.Bytes(), nil
return a[:], nil
}
// UnprefixedAddress allows marshaling an Address without 0x prefix.

View file

@ -250,21 +250,6 @@ func TestHash_Scan(t *testing.T) {
}
}
func BenchmarkHash_Scan(b *testing.B) {
tst := []byte{
0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
0x10, 0x00,
}
h := &Hash{}
for i := 0; i < b.N; i++ {
if err := h.Scan(tst); err != nil {
b.Errorf("BenchmarkHash_Scan: error Scan on Hash %v", err)
}
}
}
func TestHash_Value(t *testing.T) {
b := []byte{
0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
@ -301,23 +286,6 @@ func TestHash_Value(t *testing.T) {
}
}
func BenchmarkHash_Value(b *testing.B) {
tst := []byte{
0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
0x10, 0x00,
}
var usedH Hash
usedH.SetBytes(tst)
for i := 0; i < b.N; i++ {
if _, err := usedH.Value(); err != nil {
b.Errorf("BenchmarkHash_Value: error Value on Hash %v", err)
return
}
}
}
func TestAddress_Scan(t *testing.T) {
type args struct {
src interface{}
@ -370,19 +338,6 @@ func TestAddress_Scan(t *testing.T) {
}
}
func BenchmarkAddress_Scan(b *testing.B) {
tst := []byte{
0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
}
a := &Address{}
for i := 0; i < b.N; i++ {
if err := a.Scan(tst); err != nil {
b.Errorf("BenchmarkAddress_Scan: error Scan on Address %v", err)
}
}
}
func TestAddress_Value(t *testing.T) {
b := []byte{
0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
@ -416,18 +371,3 @@ func TestAddress_Value(t *testing.T) {
})
}
}
func BenchmarkAddress_Value(b *testing.B) {
tst := []byte{
0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
}
var usedA Address
usedA.SetBytes(tst)
for i := 0; i < b.N; i++ {
if _, err := usedA.Value(); err != nil {
b.Errorf("BenchmarkAddress_Value: error Value on Address %v", err)
return
}
}
}