forked from Mirrors/jsonapi
omit zero times from the response
This commit is contained in:
parent
6f271d2ac7
commit
db9617ef6f
|
@ -123,6 +123,12 @@ func visitModelNode(model interface{}, sideload bool) (*JsonApiNode, []*JsonApiN
|
||||||
|
|
||||||
if len(args) >= 2 {
|
if len(args) >= 2 {
|
||||||
if fieldValue.Type() == reflect.TypeOf(time.Time{}) {
|
if fieldValue.Type() == reflect.TypeOf(time.Time{}) {
|
||||||
|
isZeroMethod := fieldValue.MethodByName("IsZero")
|
||||||
|
isZero := isZeroMethod.Call(make([]reflect.Value, 0))[0].Interface().(bool)
|
||||||
|
if isZero {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
unix := fieldValue.MethodByName("Unix")
|
unix := fieldValue.MethodByName("Unix")
|
||||||
val := unix.Call(make([]reflect.Value, 0))[0]
|
val := unix.Call(make([]reflect.Value, 0))[0]
|
||||||
node.Attributes[args[1]] = val.Int()
|
node.Attributes[args[1]] = val.Int()
|
||||||
|
|
|
@ -102,6 +102,29 @@ func TestSupportsAttributes(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestOmitsZeroTimes(t *testing.T) {
|
||||||
|
testModel := &Blog{
|
||||||
|
Id: 5,
|
||||||
|
Title: "Title 1",
|
||||||
|
CreatedAt: time.Time{},
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := MarshalJsonApiOnePayload(testModel)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
response := resp.Data
|
||||||
|
|
||||||
|
if response.Attributes == nil {
|
||||||
|
t.Fatalf("Expected attributes")
|
||||||
|
}
|
||||||
|
|
||||||
|
if response.Attributes["created_at"] != nil {
|
||||||
|
t.Fatalf("Created at was serialized even though it was a zero Time")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestRelations(t *testing.T) {
|
func TestRelations(t *testing.T) {
|
||||||
testModel := &Blog{
|
testModel := &Blog{
|
||||||
Id: 5,
|
Id: 5,
|
||||||
|
|
Loading…
Reference in New Issue