aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Borman <paul@borman.com>2021-07-12 17:33:52 -0500
committerPaul Borman <paul@borman.com>2021-07-12 17:33:52 -0500
commit44b5fee7c49cf3bcdf723f106b36d56ef13ccc88 (patch)
tree4e757ac7fcce29777b41e8fb1cdde506fa48ba43
parente28eb7b914dd26e1a4e861df5e3226d0f2c256c5 (diff)
downloadgoogle-uuid-44b5fee7c49cf3bcdf723f106b36d56ef13ccc88.tar.gz
Minor cleanup.
-rw-r--r--null.go24
1 files changed, 11 insertions, 13 deletions
diff --git a/null.go b/null.go
index 95bb632..d7fcbf2 100644
--- a/null.go
+++ b/null.go
@@ -11,8 +11,10 @@ import (
"fmt"
)
+var jsonNull = []byte("null")
+
// NullUUID represents a UUID that may be null.
-// NullUUID implements the Scanner interface so
+// NullUUID implements the SQL driver.Scanner interface so
// it can be used as a scan destination:
//
// var u uuid.NullUUID
@@ -29,7 +31,7 @@ type NullUUID struct {
Valid bool // Valid is true if UUID is not NULL
}
-// Scan implements the Scanner interface.
+// Scan implements the SQL driver.Scanner interface.
func (nu *NullUUID) Scan(value interface{}) error {
if value == nil {
nu.UUID, nu.Valid = Nil, false
@@ -80,7 +82,7 @@ func (nu NullUUID) MarshalText() ([]byte, error) {
return nu.UUID.MarshalText()
}
- return []byte{110, 117, 108, 108}, nil
+ return jsonNull, nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
@@ -101,20 +103,16 @@ func (nu NullUUID) MarshalJSON() ([]byte, error) {
return json.Marshal(nu.UUID)
}
- return json.Marshal(nil)
+ return jsonNull, nil
}
// UnmarshalJSON implements json.Unmarshaler.
func (nu *NullUUID) UnmarshalJSON(data []byte) error {
- null := []byte{110, 117, 108, 108}
- if bytes.Equal(data, null) {
+ if bytes.Equal(data, jsonNull) {
+ *nu = NullUUID{}
return nil // valid null UUID
}
-
- var u UUID
- // tossing as we know u is valid
- _ = json.Unmarshal(data, &u)
- nu.Valid = true
- nu.UUID = u
- return nil
+ err := json.Unmarshal(data, &nu.UUID)
+ nu.Valid = err == nil
+ return err
}