wings/internal/models/models.go

33 lines
516 B
Go
Raw Normal View History

2022-07-24 15:43:48 +00:00
package models
import (
"database/sql"
2022-10-05 23:24:11 +00:00
2022-07-24 15:43:48 +00:00
"emperror.dev/errors"
"github.com/goccy/go-json"
)
type JsonNullString struct {
sql.NullString
}
func (v JsonNullString) MarshalJSON() ([]byte, error) {
if v.Valid {
return json.Marshal(v.String)
} else {
return json.Marshal(nil)
}
}
func (v *JsonNullString) UnmarshalJSON(data []byte) error {
var s *string
if err := json.Unmarshal(data, &s); err != nil {
return errors.WithStack(err)
}
if s != nil {
v.String = *s
}
v.Valid = s != nil
return nil
}