Skip to content

Commit

Permalink
Minor cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
pborman committed Jul 12, 2021
1 parent e28eb7b commit 44b5fee
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions null.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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
}

0 comments on commit 44b5fee

Please sign in to comment.