skip field if attr is not in the hash

This commit is contained in:
Sam Woodard 2015-07-09 12:27:03 -07:00
parent db9617ef6f
commit a228611aa0
2 changed files with 12 additions and 5 deletions

View File

@ -86,21 +86,29 @@ func unmarshalJsonApiNode(data *JsonApiNode, model reflect.Value) error {
if len(args) >= 2 { if len(args) >= 2 {
val := attributes[args[1]] val := attributes[args[1]]
// next if the attribute was not included in the request
if val == nil {
return false
}
v := reflect.ValueOf(val) v := reflect.ValueOf(val)
if fieldValue.Type() == reflect.TypeOf(time.Time{}) { if fieldValue.Type() == reflect.TypeOf(time.Time{}) {
var t time.Time
var at int64
if v.Kind() == reflect.Float64 { if v.Kind() == reflect.Float64 {
at := int64(v.Interface().(float64)) at = int64(v.Interface().(float64))
t = time.Unix(at, 0)
} else if v.Kind() == reflect.Int { } else if v.Kind() == reflect.Int {
t = time.Unix(v.Int(), 0) at = v.Int()
} else { } else {
er = errors.New("Only numbers can be parsed as dates, unix timestamps") er = errors.New("Only numbers can be parsed as dates, unix timestamps")
return false return false
} }
t := time.Unix(at, 0)
fieldValue.Set(reflect.ValueOf(t)) fieldValue.Set(reflect.ValueOf(t))
return false return false

View File

@ -181,7 +181,6 @@ func samplePayloadWithId() io.Reader {
Type: "blogs", Type: "blogs",
Attributes: map[string]interface{}{ Attributes: map[string]interface{}{
"title": "New blog", "title": "New blog",
"created_at": 1436216820,
"view_count": 1000, "view_count": 1000,
}, },
}, },