Split up types

This commit is contained in:
Matthew Dillon 2015-10-01 14:18:17 -07:00
parent 1bff626805
commit 4f3b4ec4bf
9 changed files with 245 additions and 211 deletions

38
types/null_time.go Normal file
View file

@ -0,0 +1,38 @@
package types
import (
"bytes"
"encoding/json"
"time"
"github.com/thermokarst/bactdb/Godeps/_workspace/src/github.com/lib/pq"
)
type NullTime struct {
pq.NullTime
}
func (t *NullTime) MarshalJSON() ([]byte, error) {
if !t.Valid {
return []byte("null"), nil
}
return json.Marshal(t.Time)
}
func (t *NullTime) UnmarshalJSON(b []byte) error {
if bytes.Equal(b, []byte("null")) {
var nt time.Time
t.Time = nt.In(time.UTC)
t.Valid = false
return nil
}
var x interface{}
var err error
json.Unmarshal(b, &x)
switch x.(type) {
case string:
err = json.Unmarshal(b, &t.Time)
}
t.Valid = true
return err
}