Files
archived-teslamateapi/src/NullSupport.go
T
Tobias Lindberg 65ad3b3b34 removing NullTime from NullSupport.go
removing github.com/go-sql-driver/mysql mod
go mod tidy
2021-04-01 10:21:04 +02:00

62 lines
1.2 KiB
Go

package main
// based on Gist:
// https://gist.github.com/rsudip90/022c4ef5d98130a224c9239e0a1ab397
import (
"database/sql"
"encoding/json"
)
// NullInt64 is an alias for sql.NullInt64 data type
type NullInt64 struct {
sql.NullInt64
}
// MarshalJSON for NullInt64
func (ni *NullInt64) MarshalJSON() ([]byte, error) {
if !ni.Valid {
return []byte("null"), nil
}
return json.Marshal(ni.Int64)
}
// NullBool is an alias for sql.NullBool data type
type NullBool struct {
sql.NullBool
}
// MarshalJSON for NullBool
func (nb *NullBool) MarshalJSON() ([]byte, error) {
if !nb.Valid {
return []byte("null"), nil
}
return json.Marshal(nb.Bool)
}
// NullFloat64 is an alias for sql.NullFloat64 data type
type NullFloat64 struct {
sql.NullFloat64
}
// MarshalJSON for NullFloat64
func (nf *NullFloat64) MarshalJSON() ([]byte, error) {
if !nf.Valid {
return []byte("null"), nil
}
return json.Marshal(nf.Float64)
}
// NullString is an alias for sql.NullString data type
type NullString struct {
sql.NullString
}
// MarshalJSON for NullString
func (ns *NullString) MarshalJSON() ([]byte, error) {
if !ns.Valid {
return []byte("null"), nil
}
return json.Marshal(ns.String)
}