MarshalText, UnmarshalText for Time

This commit is contained in:
Greg 2015-09-14 05:33:34 +09:00
parent e001fb0f0c
commit 4ac4f00378
4 changed files with 99 additions and 3 deletions

22
time.go
View file

@ -66,7 +66,7 @@ func (t Time) MarshalJSON() ([]byte, error) {
if !t.Valid {
return []byte("null"), nil
}
return json.Marshal(t.Time)
return t.Time.MarshalJSON()
}
// UnmarshalJSON implements json.Unmarshaler.
@ -100,6 +100,26 @@ func (t *Time) UnmarshalJSON(data []byte) error {
return err
}
func (t Time) MarshalText() ([]byte, error) {
if !t.Valid {
return []byte("null"), nil
}
return t.Time.MarshalText()
}
func (t *Time) UnmarshalText(text []byte) error {
str := string(text)
if str == "" || str == "null" {
t.Valid = false
return nil
}
if err := t.Time.UnmarshalText(text); err != nil {
return err
}
t.Valid = true
return nil
}
// SetValid changes this Time's value and sets it to be non-null.
func (t *Time) SetValid(v time.Time) {
t.Time = v

View file

@ -16,7 +16,7 @@ var (
badObject = []byte(`{"hello": "world"}`)
)
func TestUnmarshalTimeString(t *testing.T) {
func TestUnmarshalTimeJSON(t *testing.T) {
var ti Time
err := json.Unmarshal(timeJSON, &ti)
maybePanic(err)
@ -59,6 +59,33 @@ func TestUnmarshalTimeString(t *testing.T) {
assertNullTime(t, wrongType, "wrong type object json")
}
func TestUnmarshalTimeText(t *testing.T) {
ti := TimeFrom(timeValue)
txt, err := ti.MarshalText()
maybePanic(err)
assertJSONEquals(t, txt, timeString, "marshal text")
var unmarshal Time
err = unmarshal.UnmarshalText(txt)
maybePanic(err)
assertTime(t, unmarshal, "unmarshal text")
var null Time
err = null.UnmarshalText(nullJSON)
maybePanic(err)
assertNullTime(t, null, "unmarshal null text")
txt, err = null.MarshalText()
maybePanic(err)
assertJSONEquals(t, txt, string(nullJSON), "marshal null text")
var invalid Time
err = invalid.UnmarshalText([]byte("hello world"))
if err == nil {
t.Error("expected error")
}
assertNullTime(t, invalid, "bad string")
}
func TestMarshalTime(t *testing.T) {
ti := TimeFrom(timeValue)
data, err := json.Marshal(ti)

View file

@ -107,6 +107,27 @@ func (t *Time) UnmarshalJSON(data []byte) error {
}
}
func (t Time) MarshalText() ([]byte, error) {
ti := t.Time
if !t.Valid {
ti = time.Time{}
}
return ti.MarshalText()
}
func (t *Time) UnmarshalText(text []byte) error {
str := string(text)
if str == "" || str == "null" {
t.Valid = false
return nil
}
if err := t.Time.UnmarshalText(text); err != nil {
return err
}
t.Valid = true
return nil
}
// SetValid changes this Time's value and
// sets it to be non-null.
func (t *Time) SetValid(v time.Time) {

View file

@ -9,6 +9,7 @@ import (
var (
timeString = "2012-12-21T21:21:21Z"
timeJSON = []byte(`"` + timeString + `"`)
zeroTimeStr = "0001-01-01T00:00:00Z"
zeroTimeJSON = []byte(`"0001-01-01T00:00:00Z"`)
blankTimeJSON = []byte(`null`)
timeValue, _ = time.Parse(time.RFC3339, timeString)
@ -17,7 +18,7 @@ var (
badObject = []byte(`{"hello": "world"}`)
)
func TestUnmarshalTimeString(t *testing.T) {
func TestUnmarshalTimeJSON(t *testing.T) {
var ti Time
err := json.Unmarshal(timeObject, &ti)
maybePanic(err)
@ -89,6 +90,33 @@ func TestMarshalTime(t *testing.T) {
assertJSONEquals(t, data, string(zeroTimeJSON), "empty json marshal")
}
func TestUnmarshalTimeText(t *testing.T) {
ti := TimeFrom(timeValue)
txt, err := ti.MarshalText()
maybePanic(err)
assertJSONEquals(t, txt, timeString, "marshal text")
var unmarshal Time
err = unmarshal.UnmarshalText(txt)
maybePanic(err)
assertTime(t, unmarshal, "unmarshal text")
var null Time
err = null.UnmarshalText(nullJSON)
maybePanic(err)
assertNullTime(t, null, "unmarshal null text")
txt, err = null.MarshalText()
maybePanic(err)
assertJSONEquals(t, txt, zeroTimeStr, "marshal null text")
var invalid Time
err = invalid.UnmarshalText([]byte("hello world"))
if err == nil {
t.Error("expected error")
}
assertNullTime(t, invalid, "bad string")
}
func TestTimeFrom(t *testing.T) {
ti := TimeFrom(timeValue)
assertTime(t, ti, "TimeFrom() time.Time")