From e09a353737229c89c93d7db2dd20db03e76bacb4 Mon Sep 17 00:00:00 2001 From: Marvin Blum Date: Tue, 9 Nov 2021 20:16:42 +0100 Subject: [PATCH] Back to int64 because driver.Value doesn't support unsigned types. --- README.md | 4 ++-- id.go | 8 ++++---- id_test.go | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 376c602..dd81c83 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ Consider the following struct: ``` type User struct { - Id uint64 `json:"id"` + Id int64 `json:"id"` Username string `json:"username"` } ``` @@ -46,7 +46,7 @@ type User struct { } ``` -Notice that the `uint64` ID got replaced by the `hide.ID`, which internally is represented as an `uint64` as well, but implements the marshal interface. +Notice that the `int64` ID got replaced by the `hide.ID`, which internally is represented as an `int64` as well, but implements the marshal interface. This allows you to cast between them and use `hide.ID` as a replacement. The resulting JSON changes to the following: ``` diff --git a/id.go b/id.go index 37dd1ab..3bbaa21 100644 --- a/id.go +++ b/id.go @@ -7,10 +7,10 @@ import ( "strings" ) -// ID type that can be used as an replacement for uint64. +// ID type that can be used as an replacement for int64. // It is converted to/from a hash value when marshalled to/from JSON. // Value 0 is considered null. -type ID uint64 +type ID int64 // Scan implements the Scanner interface. func (hide *ID) Scan(value interface{}) error { @@ -19,7 +19,7 @@ func (hide *ID) Scan(value interface{}) error { return nil } - id, ok := value.(uint64) + id, ok := value.(int64) if !ok { return errors.New("unexpected type") @@ -35,7 +35,7 @@ func (hide ID) Value() (driver.Value, error) { return nil, nil } - return uint64(hide), nil + return int64(hide), nil } // MarshalJSON implements the encoding json interface. diff --git a/id_test.go b/id_test.go index c655cc5..8448a0d 100644 --- a/id_test.go +++ b/id_test.go @@ -90,7 +90,7 @@ func TestUnmarshalIDStruct(t *testing.T) { func TestScan(t *testing.T) { var id ID - value := uint64(123) + value := int64(123) if err := id.Scan(value); err != nil { t.Fatal(err) @@ -109,10 +109,10 @@ func TestValue(t *testing.T) { t.Fatal(err) } - _, ok := driverValue.(uint64) + _, ok := driverValue.(int64) if !ok { - t.Fatal("Driver value must be of type uint64") + t.Fatal("Driver value must be of type int64") } }