mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 01:13:45 +00:00
common: add Address Scan and Value
This commit is contained in:
parent
372d7124d3
commit
cc81ceaaf5
2 changed files with 129 additions and 0 deletions
|
|
@ -259,6 +259,33 @@ func (a *Address) UnmarshalJSON(input []byte) error {
|
|||
return hexutil.UnmarshalFixedJSON(addressT, input, a[:])
|
||||
}
|
||||
|
||||
// Value converts the Address into a SQL driver value which can be used to
|
||||
// directly use the Address as parameter to a SQL query.
|
||||
func (a Address) Value() (driver.Value, error) {
|
||||
return a.Bytes(), nil
|
||||
}
|
||||
|
||||
// Scan allows scanning from (byte slice) to *Address
|
||||
func (a *Address) Scan(src interface{}) error {
|
||||
switch v := src.(type) {
|
||||
case []byte:
|
||||
return a.scan(v)
|
||||
case string:
|
||||
return a.scan([]byte(v))
|
||||
default:
|
||||
return fmt.Errorf("Scan: unable to scan type %T into Address", v)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Address) scan(b []byte) error {
|
||||
if len(b) != AddressLength {
|
||||
return fmt.Errorf("Scan: unable to scan into Address, wrong size %d", len(b))
|
||||
}
|
||||
|
||||
*a = BytesToAddress(b)
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnprefixedHash allows marshaling an Address without 0x prefix.
|
||||
type UnprefixedAddress Address
|
||||
|
||||
|
|
|
|||
|
|
@ -237,3 +237,105 @@ func BenchmarkHash_Value(b *testing.B) {
|
|||
_, _ = h.Value()
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddress_Scan(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
src interface{}
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "working scan",
|
||||
src: []byte{
|
||||
0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
|
||||
0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "non working scan, int",
|
||||
src: int64(1234567890),
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "non working scan, wrong size",
|
||||
src: []byte{
|
||||
0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
|
||||
0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a,
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
a := &Address{}
|
||||
if err := a.Scan(tt.src); (err != nil) != tt.wantErr {
|
||||
t.Errorf("Address.Scan() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
|
||||
if !tt.wantErr {
|
||||
for i := range a {
|
||||
if a[i] != tt.src.([]byte)[i] {
|
||||
t.Errorf(
|
||||
"Address.Scan() didn't scan the %d src correctly (have %X, want %X)",
|
||||
i, a[i], tt.src.([]byte)[i],
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
a := &Address{}
|
||||
_ = a.Scan(tst)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddress_Value(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
a Address
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Working Address",
|
||||
a: Address([AddressLength]byte{
|
||||
0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
|
||||
0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
|
||||
}),
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := tt.a.Value()
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("Address.Value() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
for i, gotI := range got.([]byte) {
|
||||
if gotI != tt.a[i] {
|
||||
t.Errorf("Address.Value() non matching data")
|
||||
return
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkAddress_Value(b *testing.B) {
|
||||
a := Address([AddressLength]byte{
|
||||
0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
|
||||
0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
|
||||
})
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, _ = a.Value()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue