Skip to content

Commit

Permalink
add comment to explain decision why a field gets marshalled or not
Browse files Browse the repository at this point in the history
  • Loading branch information
masseelch committed Mar 9, 2021
1 parent 946208f commit 4e7a74d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 14 additions & 1 deletion sheriff.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,21 @@ func Marshal(options *Options, data interface{}) (interface{}, error) {
if len(groups) == 0 && options.nestedGroupsMap[field.Name] != nil {
groups = append(groups, options.nestedGroupsMap[field.Name]...)
}

// Marshall the field if
// - it has at least one of the requested groups
// or
// - it has no group and 'IncludeEmptyTag' is set to true
shouldShow := listContains(groups, options.Groups) || (len(groups) == 0 && options.IncludeEmptyTag)
if !shouldShow || (len(groups) == 0 && !options.IncludeEmptyTag) {

// Prevent marshalling of the field if
// - it should not be shown (above)
// or
// - it has no groups and 'IncludeEmptyTag' is set to false
shouldHide := !shouldShow || (len(groups) == 0 && !options.IncludeEmptyTag)

if shouldHide {
// skip this field
continue
}
}
Expand Down
2 changes: 1 addition & 1 deletion sheriff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func TestMarshal_GroupsNoGroups(t *testing.T) {
assert.Equal(t, string(expected), string(actual))
}

func TestMarshal_GroupsNoGroupsIncludeEmptyTag(t *testing.T) {
func TestMarshal_GroupsValidGroupIncludeEmptyTag(t *testing.T) {
testModel := &TestGroupsModel{
DefaultMarshal: "DefaultMarshal",
OnlyGroupTest: "OnlyGroupTest",
Expand Down

0 comments on commit 4e7a74d

Please sign in to comment.