From f9e2fd49397569cba579763f13c21f7fc7d0e500 Mon Sep 17 00:00:00 2001 From: Samantha Belkin Date: Fri, 17 Mar 2017 11:38:40 -0600 Subject: [PATCH] update to allow exclusion of relationships from block --- constants.go | 1 + response.go | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/constants.go b/constants.go index 23288d3..39f3bda 100644 --- a/constants.go +++ b/constants.go @@ -8,6 +8,7 @@ const ( annotationAttribute = "attr" annotationRelation = "relation" annotationOmitEmpty = "omitempty" + annotationExclude = "exclude" annotationISO8601 = "iso8601" annotationSeperator = "," diff --git a/response.go b/response.go index d6a4154..0e7ea21 100644 --- a/response.go +++ b/response.go @@ -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 @@ -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)) } @@ -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,