mirror of
https://github.com/tobiasehlert/teslamateapi.git
synced 2026-02-27 09:54:18 +08:00
adding support for null/nil
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
package main
|
||||
|
||||
// based on Gist:
|
||||
// https://gist.github.com/rsudip90/022c4ef5d98130a224c9239e0a1ab397
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
// NullTime is an alias for mysql.NullTime data type
|
||||
type NullTime struct {
|
||||
mysql.NullTime
|
||||
}
|
||||
|
||||
// MarshalJSON for NullTime
|
||||
func (nt *NullTime) MarshalJSON() ([]byte, error) {
|
||||
if !nt.Valid {
|
||||
return []byte("null"), nil
|
||||
}
|
||||
val := fmt.Sprintf("\"%s\"", nt.Time.Format(time.RFC3339))
|
||||
return []byte(val), nil
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
@@ -176,6 +177,17 @@ func getTimeInTimeZone(datestring string) string {
|
||||
return ReturnDate
|
||||
}
|
||||
|
||||
func isNil(i interface{}) bool {
|
||||
if i == nil {
|
||||
return true
|
||||
}
|
||||
switch reflect.TypeOf(i).Kind() {
|
||||
case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice:
|
||||
return reflect.ValueOf(i).IsNil()
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// getEnv func - read an environment or return a default value
|
||||
func getEnv(key string, defaultVal string) string {
|
||||
if value, exists := os.LookupEnv(key); exists {
|
||||
|
||||
Reference in New Issue
Block a user