@@ -11,8 +11,10 @@ import (
11
11
"fmt"
12
12
)
13
13
14
+ var jsonNull = []byte ("null" )
15
+
14
16
// NullUUID represents a UUID that may be null.
15
- // NullUUID implements the Scanner interface so
17
+ // NullUUID implements the SQL driver. Scanner interface so
16
18
// it can be used as a scan destination:
17
19
//
18
20
// var u uuid.NullUUID
@@ -29,7 +31,7 @@ type NullUUID struct {
29
31
Valid bool // Valid is true if UUID is not NULL
30
32
}
31
33
32
- // Scan implements the Scanner interface.
34
+ // Scan implements the SQL driver. Scanner interface.
33
35
func (nu * NullUUID ) Scan (value interface {}) error {
34
36
if value == nil {
35
37
nu .UUID , nu .Valid = Nil , false
@@ -80,7 +82,7 @@ func (nu NullUUID) MarshalText() ([]byte, error) {
80
82
return nu .UUID .MarshalText ()
81
83
}
82
84
83
- return [] byte { 110 , 117 , 108 , 108 } , nil
85
+ return jsonNull , nil
84
86
}
85
87
86
88
// UnmarshalText implements encoding.TextUnmarshaler.
@@ -101,20 +103,16 @@ func (nu NullUUID) MarshalJSON() ([]byte, error) {
101
103
return json .Marshal (nu .UUID )
102
104
}
103
105
104
- return json . Marshal ( nil )
106
+ return jsonNull , nil
105
107
}
106
108
107
109
// UnmarshalJSON implements json.Unmarshaler.
108
110
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 {}
111
113
return nil // valid null UUID
112
114
}
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
120
118
}
0 commit comments