mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 02:12:23 +00:00
common: implement sql scanner/valuer on Hash
This commit is contained in:
parent
d0806a2c04
commit
aaca11da13
2 changed files with 150 additions and 1 deletions
|
|
@ -17,6 +17,7 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
|
@ -121,6 +122,30 @@ func (h Hash) Generate(rand *rand.Rand, size int) reflect.Value {
|
|||
return reflect.ValueOf(h)
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
if len(srcB) != HashLength {
|
||||
return fmt.Errorf(
|
||||
"Hash Scan: len %d instead of expected %d",
|
||||
len(srcB),
|
||||
HashLength,
|
||||
)
|
||||
}
|
||||
|
||||
*h = BytesToHash(srcB)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Value implements valuer for database/sql
|
||||
func (h Hash) Value() (driver.Value, error) {
|
||||
return h.Bytes(), nil
|
||||
}
|
||||
|
||||
// UnprefixedHash allows marshaling a Hash without 0x prefix.
|
||||
type UnprefixedHash Hash
|
||||
|
||||
|
|
|
|||
|
|
@ -17,9 +17,10 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
|
||||
"math/big"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
|
@ -193,3 +194,126 @@ func TestMixedcaseAccount_Address(t *testing.T) {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
func TestHash_Scan(t *testing.T) {
|
||||
type args struct {
|
||||
src interface{}
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "working scan",
|
||||
args: args{src: []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,
|
||||
}},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "non working scan",
|
||||
args: args{src: int64(1234567890)},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "invalid length scan",
|
||||
args: args{src: []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,
|
||||
}},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
h := &Hash{}
|
||||
if err := h.Scan(tt.args.src); (err != nil) != tt.wantErr {
|
||||
t.Errorf("Hash.Scan() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
|
||||
if !tt.wantErr {
|
||||
for i := range h {
|
||||
if h[i] != tt.args.src.([]byte)[i] {
|
||||
t.Errorf(
|
||||
"Hash.Scan() didn't scan the %d src correctly (have %X, want %X)",
|
||||
i, h[i], tt.args.src.([]byte)[i],
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
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(b)
|
||||
tests := []struct {
|
||||
name string
|
||||
h Hash
|
||||
want driver.Value
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Working value",
|
||||
h: usedH,
|
||||
want: b,
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := tt.h.Value()
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("Hash.Value() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("Hash.Value() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue