Skip to content

Commit

Permalink
update to allow exclusion of relationships from block
Browse files Browse the repository at this point in the history
  • Loading branch information
sbelkin88 committed Mar 17, 2017
1 parent de02fef commit f9e2fd4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const (
annotationAttribute = "attr"
annotationRelation = "relation"
annotationOmitEmpty = "omitempty"
annotationExclude = "exclude"
annotationISO8601 = "iso8601"
annotationSeperator = ","

Expand Down
18 changes: 16 additions & 2 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,20 @@ func visitModelNode(model interface{}, included *map[string]*Node,
}
} else if annotation == annotationRelation {
var omitEmpty bool
var include = true

//add support for 'omitempty' struct tag for marshaling as absent
if len(args) > 2 {
omitEmpty = args[2] == annotationOmitEmpty
if args[2] == annotationExclude {
include = false
}
}

if len(args) > 3 {
if args[3] == annotationExclude {
include = false
}
}

isSlice := fieldValue.Type().Kind() == reflect.Slice
Expand Down Expand Up @@ -395,7 +405,9 @@ func visitModelNode(model interface{}, included *map[string]*Node,
if sideload {
shallowNodes := []*Node{}
for _, n := range relationship.Data {
appendIncluded(included, n)
if include {
appendIncluded(included, n)
}
shallowNodes = append(shallowNodes, toShallowNode(n))
}

Expand Down Expand Up @@ -427,7 +439,9 @@ func visitModelNode(model interface{}, included *map[string]*Node,
}

if sideload {
appendIncluded(included, relationship)
if include {
appendIncluded(included, relationship)
}
node.Relationships[args[1]] = &RelationshipOneNode{
Data: toShallowNode(relationship),
Links: relLinks,
Expand Down

0 comments on commit f9e2fd4

Please sign in to comment.