You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
817 B
46 lines
817 B
3 months ago
|
package dts
|
||
|
|
||
|
import (
|
||
|
"database/sql"
|
||
|
"database/sql/driver"
|
||
|
"encoding/json"
|
||
|
)
|
||
|
|
||
|
type NullString sql.NullString
|
||
|
|
||
|
// Scan implements the [Scanner] interface.
|
||
|
func (ns *NullString) Scan(value any) error {
|
||
|
ss := new(sql.NullString)
|
||
|
err := ss.Scan(value)
|
||
|
ns.String = ss.String
|
||
|
ns.Valid = ss.Valid
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// Value implements the [driver.Valuer] interface.
|
||
|
func (ns NullString) Value() (driver.Value, error) {
|
||
|
if !ns.Valid {
|
||
|
return nil, nil
|
||
|
}
|
||
|
return ns.String, nil
|
||
|
}
|
||
|
|
||
|
func (ns NullString) MarshalJSON() ([]byte, error) {
|
||
|
if ns.Valid {
|
||
|
return json.Marshal(ns.String)
|
||
|
}
|
||
|
return json.Marshal(nil)
|
||
|
}
|
||
|
|
||
|
func (ns *NullString) UnmarshalJSON(b []byte) error {
|
||
|
if string(b) == "null" {
|
||
|
ns.Valid = false
|
||
|
return nil
|
||
|
}
|
||
|
err := json.Unmarshal(b, &ns.String)
|
||
|
if err == nil {
|
||
|
ns.Valid = true
|
||
|
}
|
||
|
return err
|
||
|
}
|