Skip to content

Commit 2909f6d

Browse files
vincentserpoulfjl
authored andcommitted
common: add database/sql support for Hash and Address (#15541)
1 parent d96ba77 commit 2909f6d

2 files changed

Lines changed: 219 additions & 2 deletions

File tree

‎common/types.go‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package common
1818

1919
import (
20+
"database/sql/driver"
2021
"encoding/hex"
2122
"encoding/json"
2223
"fmt"
@@ -31,7 +32,9 @@ import (
3132

3233
// Lengths of hashes and addresses in bytes.
3334
const (
34-
HashLength = 32
35+
// HashLength is the expected length of the hash
36+
HashLength = 32
37+
// AddressLength is the expected length of the adddress
3538
AddressLength = 20
3639
)
3740

@@ -120,6 +123,24 @@ func (h Hash) Generate(rand *rand.Rand, size int) reflect.Value {
120123
return reflect.ValueOf(h)
121124
}
122125

126+
// Scan implements Scanner for database/sql.
127+
func (h *Hash) Scan(src interface{}) error {
128+
srcB, ok := src.([]byte)
129+
if !ok {
130+
return fmt.Errorf("can't scan %T into Hash", src)
131+
}
132+
if len(srcB) != HashLength {
133+
return fmt.Errorf("can't scan []byte of len %d into Hash, want %d", len(srcB), HashLength)
134+
}
135+
copy(h[:], srcB)
136+
return nil
137+
}
138+
139+
// Value implements valuer for database/sql.
140+
func (h Hash) Value() (driver.Value, error) {
141+
return h[:], nil
142+
}
143+
123144
// UnprefixedHash allows marshaling a Hash without 0x prefix.
124145
type UnprefixedHash Hash
125146

@@ -229,6 +250,24 @@ func (a *Address) UnmarshalJSON(input []byte) error {
229250
return hexutil.UnmarshalFixedJSON(addressT, input, a[:])
230251
}
231252

253+
// Scan implements Scanner for database/sql.
254+
func (a *Address) Scan(src interface{}) error {
255+
srcB, ok := src.([]byte)
256+
if !ok {
257+
return fmt.Errorf("can't scan %T into Address", src)
258+
}
259+
if len(srcB) != AddressLength {
260+
return fmt.Errorf("can't scan []byte of len %d into Address, want %d", len(srcB), AddressLength)
261+
}
262+
copy(a[:], srcB)
263+
return nil
264+
}
265+
266+
// Value implements valuer for database/sql.
267+
func (a Address) Value() (driver.Value, error) {
268+
return a[:], nil
269+
}
270+
232271
// UnprefixedAddress allows marshaling an Address without 0x prefix.
233272
type UnprefixedAddress Address
234273

‎common/types_test.go‎

Lines changed: 179 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
package common
1818

1919
import (
20+
"database/sql/driver"
2021
"encoding/json"
21-
2222
"math/big"
23+
"reflect"
2324
"strings"
2425
"testing"
2526
)
@@ -193,3 +194,180 @@ func TestMixedcaseAccount_Address(t *testing.T) {
193194
}
194195

195196
}
197+
198+
func TestHash_Scan(t *testing.T) {
199+
type args struct {
200+
src interface{}
201+
}
202+
tests := []struct {
203+
name string
204+
args args
205+
wantErr bool
206+
}{
207+
{
208+
name: "working scan",
209+
args: args{src: []byte{
210+
0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
211+
0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
212+
0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
213+
0x10, 0x00,
214+
}},
215+
wantErr: false,
216+
},
217+
{
218+
name: "non working scan",
219+
args: args{src: int64(1234567890)},
220+
wantErr: true,
221+
},
222+
{
223+
name: "invalid length scan",
224+
args: args{src: []byte{
225+
0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
226+
0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
227+
0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
228+
}},
229+
wantErr: true,
230+
},
231+
}
232+
for _, tt := range tests {
233+
t.Run(tt.name, func(t *testing.T) {
234+
h := &Hash{}
235+
if err := h.Scan(tt.args.src); (err != nil) != tt.wantErr {
236+
t.Errorf("Hash.Scan() error = %v, wantErr %v", err, tt.wantErr)
237+
}
238+
239+
if !tt.wantErr {
240+
for i := range h {
241+
if h[i] != tt.args.src.([]byte)[i] {
242+
t.Errorf(
243+
"Hash.Scan() didn't scan the %d src correctly (have %X, want %X)",
244+
i, h[i], tt.args.src.([]byte)[i],
245+
)
246+
}
247+
}
248+
}
249+
})
250+
}
251+
}
252+
253+
func TestHash_Value(t *testing.T) {
254+
b := []byte{
255+
0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
256+
0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
257+
0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
258+
0x10, 0x00,
259+
}
260+
var usedH Hash
261+
usedH.SetBytes(b)
262+
tests := []struct {
263+
name string
264+
h Hash
265+
want driver.Value
266+
wantErr bool
267+
}{
268+
{
269+
name: "Working value",
270+
h: usedH,
271+
want: b,
272+
wantErr: false,
273+
},
274+
}
275+
for _, tt := range tests {
276+
t.Run(tt.name, func(t *testing.T) {
277+
got, err := tt.h.Value()
278+
if (err != nil) != tt.wantErr {
279+
t.Errorf("Hash.Value() error = %v, wantErr %v", err, tt.wantErr)
280+
return
281+
}
282+
if !reflect.DeepEqual(got, tt.want) {
283+
t.Errorf("Hash.Value() = %v, want %v", got, tt.want)
284+
}
285+
})
286+
}
287+
}
288+
289+
func TestAddress_Scan(t *testing.T) {
290+
type args struct {
291+
src interface{}
292+
}
293+
tests := []struct {
294+
name string
295+
args args
296+
wantErr bool
297+
}{
298+
{
299+
name: "working scan",
300+
args: args{src: []byte{
301+
0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
302+
0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
303+
}},
304+
wantErr: false,
305+
},
306+
{
307+
name: "non working scan",
308+
args: args{src: int64(1234567890)},
309+
wantErr: true,
310+
},
311+
{
312+
name: "invalid length scan",
313+
args: args{src: []byte{
314+
0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
315+
0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a,
316+
}},
317+
wantErr: true,
318+
},
319+
}
320+
for _, tt := range tests {
321+
t.Run(tt.name, func(t *testing.T) {
322+
a := &Address{}
323+
if err := a.Scan(tt.args.src); (err != nil) != tt.wantErr {
324+
t.Errorf("Address.Scan() error = %v, wantErr %v", err, tt.wantErr)
325+
}
326+
327+
if !tt.wantErr {
328+
for i := range a {
329+
if a[i] != tt.args.src.([]byte)[i] {
330+
t.Errorf(
331+
"Address.Scan() didn't scan the %d src correctly (have %X, want %X)",
332+
i, a[i], tt.args.src.([]byte)[i],
333+
)
334+
}
335+
}
336+
}
337+
})
338+
}
339+
}
340+
341+
func TestAddress_Value(t *testing.T) {
342+
b := []byte{
343+
0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
344+
0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
345+
}
346+
var usedA Address
347+
usedA.SetBytes(b)
348+
tests := []struct {
349+
name string
350+
a Address
351+
want driver.Value
352+
wantErr bool
353+
}{
354+
{
355+
name: "Working value",
356+
a: usedA,
357+
want: b,
358+
wantErr: false,
359+
},
360+
}
361+
for _, tt := range tests {
362+
t.Run(tt.name, func(t *testing.T) {
363+
got, err := tt.a.Value()
364+
if (err != nil) != tt.wantErr {
365+
t.Errorf("Address.Value() error = %v, wantErr %v", err, tt.wantErr)
366+
return
367+
}
368+
if !reflect.DeepEqual(got, tt.want) {
369+
t.Errorf("Address.Value() = %v, want %v", got, tt.want)
370+
}
371+
})
372+
}
373+
}

0 commit comments

Comments
 (0)