From 73fea57e7f31488ed0ebc855fa8010fbafd46e88 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Tue, 24 Jul 2018 14:40:59 +0200 Subject: [PATCH] common: simplify sql driver methods --- common/types.go | 36 +++++++++----------------- common/types_test.go | 60 -------------------------------------------- 2 files changed, 12 insertions(+), 84 deletions(-) diff --git a/common/types.go b/common/types.go index 9787aaf04f..42a8181b53 100644 --- a/common/types.go +++ b/common/types.go @@ -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. diff --git a/common/types_test.go b/common/types_test.go index 443bf285a7..7095ccd019 100644 --- a/common/types_test.go +++ b/common/types_test.go @@ -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 - } - } -}