Skip to content

Commit 44b5fee

Browse files
committed
Minor cleanup.
1 parent e28eb7b commit 44b5fee

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

null.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import (
1111
"fmt"
1212
)
1313

14+
var jsonNull = []byte("null")
15+
1416
// NullUUID represents a UUID that may be null.
15-
// NullUUID implements the Scanner interface so
17+
// NullUUID implements the SQL driver.Scanner interface so
1618
// it can be used as a scan destination:
1719
//
1820
// var u uuid.NullUUID
@@ -29,7 +31,7 @@ type NullUUID struct {
2931
Valid bool // Valid is true if UUID is not NULL
3032
}
3133

32-
// Scan implements the Scanner interface.
34+
// Scan implements the SQL driver.Scanner interface.
3335
func (nu *NullUUID) Scan(value interface{}) error {
3436
if value == nil {
3537
nu.UUID, nu.Valid = Nil, false
@@ -80,7 +82,7 @@ func (nu NullUUID) MarshalText() ([]byte, error) {
8082
return nu.UUID.MarshalText()
8183
}
8284

83-
return []byte{110, 117, 108, 108}, nil
85+
return jsonNull, nil
8486
}
8587

8688
// UnmarshalText implements encoding.TextUnmarshaler.
@@ -101,20 +103,16 @@ func (nu NullUUID) MarshalJSON() ([]byte, error) {
101103
return json.Marshal(nu.UUID)
102104
}
103105

104-
return json.Marshal(nil)
106+
return jsonNull, nil
105107
}
106108

107109
// UnmarshalJSON implements json.Unmarshaler.
108110
func (nu *NullUUID) UnmarshalJSON(data []byte) error {
109-
null := []byte{110, 117, 108, 108}
110-
if bytes.Equal(data, null) {
111+
if bytes.Equal(data, jsonNull) {
112+
*nu = NullUUID{}
111113
return nil // valid null UUID
112114
}
113-
114-
var u UUID
115-
// tossing as we know u is valid
116-
_ = json.Unmarshal(data, &u)
117-
nu.Valid = true
118-
nu.UUID = u
119-
return nil
115+
err := json.Unmarshal(data, &nu.UUID)
116+
nu.Valid = err == nil
117+
return err
120118
}

0 commit comments

Comments
 (0)