From f37f1ddf83051ae48e51d2083bc6672e861e25af Mon Sep 17 00:00:00 2001 From: sknr Date: Mon, 17 May 2021 20:22:29 +0200 Subject: [PATCH] Implement sql.Scanner and driver.Value interfaces for coinbase.Time type (#102) --- time.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/time.go b/time.go index 6efaa5d..53fa8e6 100644 --- a/time.go +++ b/time.go @@ -1,6 +1,7 @@ package coinbasepro import ( + "database/sql/driver" "fmt" "strings" "time" @@ -64,3 +65,18 @@ func (t Time) MarshalJSON() ([]byte, error) { func (t *Time) Time() time.Time { return time.Time(*t) } + +// Scan implements the sql.Scanner interface for database deserialization. +func (t *Time) Scan(value interface{}) error { + timeValue, ok := value.(time.Time) + if !ok { + return fmt.Errorf("failed to deserialize time: %#v", value) + } + *t = Time(timeValue) + return nil +} + +// Value implements the driver.Valuer interface for database serialization. +func (t Time) Value() (driver.Value, error) { + return t.Time(), nil +}