Feature/embeded structs fix tests (#105)

* fix TestEmbededStructs_nonNilStructPtr; bug on loop w/ (multiple) embedded structs; should just continue on success

* fix TestMarshal_duplicatePrimaryAnnotationFromEmbeddedStructs; fix order of processing of visitModelNode
This commit is contained in:
skimata 2017-08-01 11:48:27 -07:00 committed by Aren Patel
parent 5be05083d8
commit 662431e860
2 changed files with 27 additions and 6 deletions

View File

@ -218,10 +218,12 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)
assign(em.structField, tmp)
data = copy
}
return nil
} else {
// handle non-nil scenarios
if err := unmarshalNode(data, em.model, included); err != nil {
return err
}
}
// handle non-nil scenarios
return unmarshalNode(data, em.model, included)
}
return nil

View File

@ -202,8 +202,10 @@ func MarshalOnePayloadEmbedded(w io.Writer, model interface{}) error {
return nil
}
func visitModelNode(model interface{}, included *map[string]*Node,
sideload bool) (*Node, error) {
// visitModelNode converts models to jsonapi payloads
// it handles the deepest models first. (i.e.) embedded models
// this is so that upper-level attributes can overwrite lower-level attributes
func visitModelNode(model interface{}, included *map[string]*Node, sideload bool) (*Node, error) {
node := new(Node)
var er error
@ -211,12 +213,13 @@ func visitModelNode(model interface{}, included *map[string]*Node,
modelValue := reflect.ValueOf(model).Elem()
modelType := reflect.ValueOf(model).Type().Elem()
// handle just the embedded models first
for i := 0; i < modelValue.NumField(); i++ {
fieldValue := modelValue.Field(i)
fieldType := modelType.Field(i)
// skip if annotated w/ ignore
tag := fieldType.Tag.Get(annotationJSONAPI)
if shouldIgnoreField(tag) {
continue
}
@ -239,6 +242,22 @@ func visitModelNode(model interface{}, included *map[string]*Node,
break
}
node.merge(embNode)
}
}
// handle everthing else
for i := 0; i < modelValue.NumField(); i++ {
fieldValue := modelValue.Field(i)
fieldType := modelType.Field(i)
tag := fieldType.Tag.Get(annotationJSONAPI)
if shouldIgnoreField(tag) {
continue
}
// skip embedded because it was handled in a previous loop
if isEmbeddedStruct(fieldType) || isEmbeddedStructPtr(fieldType) {
continue
}