Created
August 18, 2018 19:35
-
-
Save jonlundy/c2a0572cb23e34194ed60a48cff48589 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package model | |
import ( | |
"bytes" | |
"database/sql/driver" | |
"fmt" | |
"io" | |
"strings" | |
"time" | |
) | |
// NullTime implements a nullable timestamp for sql | |
// swagger:type string | |
type NullTime struct { | |
Time time.Time | |
Valid bool // Valid is true if String is not NULL | |
} | |
// Scan implements the Scanner interface for NullTime. | |
func (n *NullTime) Scan(value interface{}) (err error) { | |
n.Valid = true | |
switch value.(type) { | |
case time.Time: | |
n.Time = value.(time.Time) | |
default: | |
n.Time, n.Valid = time.Time{}, false | |
} | |
return | |
} | |
// Value implements the driver Valuer interface. | |
func (n NullTime) Value() (driver.Value, error) { | |
if !n.Valid { | |
return nil, nil | |
} | |
return n.Time, nil | |
} | |
// UnmarshalGQL implements the graphql.Marshaler interface for NullTime | |
func (n *NullTime) UnmarshalGQL(v interface{}) (err error) { | |
value, ok := v.(string) | |
if !ok { | |
return fmt.Errorf("points must be strings") | |
} | |
if value == "null" { | |
n.Valid = false | |
n.Time = time.Time{} | |
} else { | |
n.Valid = true | |
n.Time, err = time.Parse(time.RFC3339, strings.Trim(value, ` "`)) | |
} | |
return | |
} | |
// MarshalGQL implements the graphql.Marshaler interface for NullTime | |
func (n NullTime) MarshalGQL(w io.Writer) { | |
var err error | |
if n.Valid { | |
_, err = w.Write([]byte(`"` + n.Time.Format(time.RFC3339+`"`))) | |
} else { | |
_, err = w.Write([]byte(`null`)) | |
} | |
if err != nil { | |
return | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment