mirror of
https://github.com/tobiasehlert/teslamateapi.git
synced 2026-02-27 09:54:18 +08:00
fix NullString issue in several endpoints (#245)
Co-authored-by: Vincent Barrier <vbarrier@kagilum.com>
This commit is contained in:
co-authored by
Vincent Barrier
parent
a9a3ee1038
commit
b0c86642cc
+19
-8
@@ -5,7 +5,9 @@ package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// NullInt64 is an alias for sql.NullInt64 data type
|
||||
@@ -47,15 +49,24 @@ func (nf *NullFloat64) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(nf.Float64)
|
||||
}
|
||||
|
||||
// NullString is an alias for sql.NullString data type
|
||||
type NullString struct {
|
||||
sql.NullString
|
||||
type NullString string
|
||||
|
||||
func (s *NullString) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
*s = ""
|
||||
return nil
|
||||
}
|
||||
strVal, ok := value.(string)
|
||||
if !ok {
|
||||
return errors.New("value is not a string")
|
||||
}
|
||||
*s = NullString(strVal)
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalJSON for NullString
|
||||
func (ns *NullString) MarshalJSON() ([]byte, error) {
|
||||
if !ns.Valid {
|
||||
return []byte("null"), nil
|
||||
func (s NullString) Value() (driver.Value, error) {
|
||||
if len(s) == 0 { // if nil or empty string
|
||||
return nil, nil
|
||||
}
|
||||
return json.Marshal(ns.String)
|
||||
return string(s), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user