Skip to content

Feature/embeded structs fix tests #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 22 additions & 3 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,21 +202,24 @@ 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

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
}
Expand All @@ -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
}

Expand Down