From 68b5e9ac370c5aa7dca9a009a16199d2433ad016 Mon Sep 17 00:00:00 2001 From: Vincent Serpoul Date: Thu, 21 Sep 2017 11:25:02 +0800 Subject: [PATCH] common: add Hash Scan and Value --- common/types.go | 28 +++++++++++ common/types_test.go | 112 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 140 insertions(+) diff --git a/common/types.go b/common/types.go index d31bbf741b..b73ba2da0c 100644 --- a/common/types.go +++ b/common/types.go @@ -17,6 +17,7 @@ package common import ( + "database/sql/driver" "encoding/hex" "fmt" "math/big" @@ -120,6 +121,33 @@ func EmptyHash(h Hash) bool { return h == Hash{} } +// Value converts the Hash into a SQL driver value which can be used to +// directly use the HASH as parameter to a SQL query. +func (h Hash) Value() (driver.Value, error) { + return h.Bytes(), nil +} + +// Scan allows scanning from (byte slice) to *Hash +func (h *Hash) Scan(src interface{}) error { + switch v := src.(type) { + case []byte: + return h.scan(v) + case string: + return h.scan([]byte(v)) + default: + return fmt.Errorf("Scan: unable to scan type %T into Hash", v) + } +} + +func (h *Hash) scan(b []byte) error { + if len(b) != HashLength { + return fmt.Errorf("Scan: unable to scan into Hash, wrong size %d", len(b)) + } + + *h = BytesToHash(b) + return nil +} + // UnprefixedHash allows marshaling a Hash without 0x prefix. type UnprefixedHash Hash diff --git a/common/types_test.go b/common/types_test.go index 6f3b315761..ce77126b94 100644 --- a/common/types_test.go +++ b/common/types_test.go @@ -125,3 +125,115 @@ func BenchmarkAddressHex(b *testing.B) { testAddr.Hex() } } + +func TestHash_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, + 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, + 0x10, 0x00, + }, + 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, 0x15, + 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, + 0x10, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + h := &Hash{} + if err := h.Scan(tt.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.src.([]byte)[i] { + t.Errorf( + "Hash.Scan() didn't scan the %d src correctly (have %X, want %X)", + i, h[i], tt.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, + } + for i := 0; i < b.N; i++ { + h := &Hash{} + _ = h.Scan(tst) + } +} + +func TestHash_Value(t *testing.T) { + tests := []struct { + name string + h Hash + wantErr bool + }{ + { + name: "Working hash", + h: Hash([HashLength]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, + }, + } + 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 + } + for i, gotI := range got.([]byte) { + if gotI != tt.h[i] { + t.Errorf("Hash.Value() non matching data") + return + } + } + }) + } +} + +func BenchmarkHash_Value(b *testing.B) { + h := Hash([HashLength]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, + }) + for i := 0; i < b.N; i++ { + _, _ = h.Value() + } +}