aboutsummaryrefslogtreecommitdiffstats
path: root/common/types.go
diff options
context:
space:
mode:
authorVincent Serpoul <vincent@serpoul.com>2018-07-24 21:15:07 +0800
committerFelix Lange <fjl@users.noreply.github.com>2018-07-24 21:15:07 +0800
commit2909f6d7a2ceb5b1cdeb4cc3966531018a0b8334 (patch)
treebd1fb46b09efed7478daeea0ad34c3bb643de448 /common/types.go
parentd96ba77113e1a87e0402fa4eb6a5776786f8e005 (diff)
downloadgo-tangerine-2909f6d7a2ceb5b1cdeb4cc3966531018a0b8334.tar
go-tangerine-2909f6d7a2ceb5b1cdeb4cc3966531018a0b8334.tar.gz
go-tangerine-2909f6d7a2ceb5b1cdeb4cc3966531018a0b8334.tar.bz2
go-tangerine-2909f6d7a2ceb5b1cdeb4cc3966531018a0b8334.tar.lz
go-tangerine-2909f6d7a2ceb5b1cdeb4cc3966531018a0b8334.tar.xz
go-tangerine-2909f6d7a2ceb5b1cdeb4cc3966531018a0b8334.tar.zst
go-tangerine-2909f6d7a2ceb5b1cdeb4cc3966531018a0b8334.zip
common: add database/sql support for Hash and Address (#15541)
Diffstat (limited to 'common/types.go')
-rw-r--r--common/types.go41
1 files changed, 40 insertions, 1 deletions
diff --git a/common/types.go b/common/types.go
index 4d374ad24..71fe5c95c 100644
--- a/common/types.go
+++ b/common/types.go
@@ -17,6 +17,7 @@
package common
import (
+ "database/sql/driver"
"encoding/hex"
"encoding/json"
"fmt"
@@ -31,7 +32,9 @@ import (
// Lengths of hashes and addresses in bytes.
const (
- HashLength = 32
+ // HashLength is the expected length of the hash
+ HashLength = 32
+ // AddressLength is the expected length of the adddress
AddressLength = 20
)
@@ -120,6 +123,24 @@ 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("can't scan %T into Hash", src)
+ }
+ if len(srcB) != HashLength {
+ return fmt.Errorf("can't scan []byte of len %d into Hash, want %d", len(srcB), HashLength)
+ }
+ copy(h[:], srcB)
+ return nil
+}
+
+// Value implements valuer for database/sql.
+func (h Hash) Value() (driver.Value, error) {
+ return h[:], nil
+}
+
// UnprefixedHash allows marshaling a Hash without 0x prefix.
type UnprefixedHash Hash
@@ -229,6 +250,24 @@ func (a *Address) UnmarshalJSON(input []byte) error {
return hexutil.UnmarshalFixedJSON(addressT, input, a[:])
}
+// Scan implements Scanner for database/sql.
+func (a *Address) Scan(src interface{}) error {
+ srcB, ok := src.([]byte)
+ if !ok {
+ return fmt.Errorf("can't scan %T into Address", src)
+ }
+ if len(srcB) != AddressLength {
+ return fmt.Errorf("can't scan []byte of len %d into Address, want %d", len(srcB), AddressLength)
+ }
+ copy(a[:], srcB)
+ return nil
+}
+
+// Value implements valuer for database/sql.
+func (a Address) Value() (driver.Value, error) {
+ return a[:], nil
+}
+
// UnprefixedAddress allows marshaling an Address without 0x prefix.
type UnprefixedAddress Address