From b0c86642cc8e7b45ee1b4351699dcca7e46e313c Mon Sep 17 00:00:00 2001 From: Tobias Lindberg Date: Sat, 23 Sep 2023 07:47:23 +0200 Subject: [PATCH] fix NullString issue in several endpoints (#245) Co-authored-by: Vincent Barrier --- src/NullSupport.go | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/NullSupport.go b/src/NullSupport.go index dfd025e..e9997f3 100644 --- a/src/NullSupport.go +++ b/src/NullSupport.go @@ -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 }