Skip to content

Commit

Permalink
* rename to FieldFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
alecsammon authored and mweibel committed May 31, 2024
1 parent 7dd1ed9 commit 60a4260
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
22 changes: 11 additions & 11 deletions sheriff.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import (
"github.com/hashicorp/go-version"
)

// A Decider is a function that decides whether a field should be marshalled or not.
// A FieldFilter is a function that decides whether a field should be marshalled or not.
// If it returns true, the field will be marshalled, otherwise it will be skipped.
type Decider func(field reflect.StructField) (bool, error)
type FieldFilter func(field reflect.StructField) (bool, error)

// Options determine which struct fields are being added to the output map.
type Options struct {
// The Decider makes the decision whether a field should be marshalled or not.
// The FieldFilter makes the decision whether a field should be marshalled or not.
// It receives the reflect.StructField of the field and should return true if the field should be included.
// If this is not set then the default Decider will be used, which uses the Groups and ApiVersion fields.
// If this is not set then the default FieldFilter will be used, which uses the Groups and ApiVersion fields.
// Setting this value will result in the other options being ignored.
Decider Decider
FieldFilter FieldFilter

// Groups determine which fields are getting marshalled based on the groups tag.
// A field with multiple groups (comma-separated) will result in marshalling of that
Expand Down Expand Up @@ -77,8 +77,8 @@ func Marshal(options *Options, data interface{}) (interface{}, error) {
options.nestedGroupsMap = make(map[string][]string)
}

if options.Decider == nil {
options.Decider = createDefaultDecider(options)
if options.FieldFilter == nil {
options.FieldFilter = createDefaultFieldFilter(options)
}

if t.Kind() == reflect.Ptr {
Expand Down Expand Up @@ -150,7 +150,7 @@ func Marshal(options *Options, data interface{}) (interface{}, error) {
}

if !isEmbeddedField {
include, err := options.Decider(field)
include, err := options.FieldFilter(field)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -185,9 +185,9 @@ func Marshal(options *Options, data interface{}) (interface{}, error) {
return dest, nil
}

// createDefaultDecider creates a default Decider function which uses the options.Groups and options.ApiVersion fields
// in order to determine whether a field should be marshalled or not.
func createDefaultDecider(options *Options) Decider {
// createDefaultFieldFilter creates a default FieldFilter function which uses the options.Groups and options.ApiVersion
// fields in order to determine whether a field should be marshalled or not.
func createDefaultFieldFilter(options *Options) FieldFilter {
checkGroups := len(options.Groups) > 0

return func(field reflect.StructField) (bool, error) {
Expand Down
4 changes: 2 additions & 2 deletions sheriff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ func TestMarshal_User(t *testing.T) {
assert.Equal(t, `{"test":"12","testb":"true","testf":"12","tests":"\"test\""}`, string(d))
}

func TestMarshal_CustomDecider(t *testing.T) {
func TestMarshal_CustomFieldFilter(t *testing.T) {
type testStruct struct {
TestValue string `json:"test"`
SecretValue string `json:"secret" hidden:"true"`
Expand All @@ -866,7 +866,7 @@ func TestMarshal_CustomDecider(t *testing.T) {
}

o := &Options{
Decider: func(field reflect.StructField) (bool, error) {
FieldFilter: func(field reflect.StructField) (bool, error) {
return field.Tag.Get("hidden") == "", nil
},
}
Expand Down

0 comments on commit 60a4260

Please sign in to comment.