github.com/Jeffail/benthos/v3@v3.65.0/internal/metadata/exclude_filter.go (about) 1 package metadata 2 3 import ( 4 "strings" 5 6 "github.com/Jeffail/benthos/v3/internal/docs" 7 "github.com/Jeffail/benthos/v3/lib/types" 8 ) 9 10 // ExcludeFilterFields returns a docs spec for the fields within a metadata 11 // config struct. 12 func ExcludeFilterFields() docs.FieldSpecs { 13 return docs.FieldSpecs{ 14 docs.FieldString("exclude_prefixes", "Provide a list of explicit metadata key prefixes to be excluded when adding metadata to sent messages.").Array(), 15 } 16 } 17 18 // ExcludeFilterConfig describes actions to be performed on message metadata 19 // before being sent to an output destination. 20 type ExcludeFilterConfig struct { 21 ExcludePrefixes []string `json:"exclude_prefixes" yaml:"exclude_prefixes"` 22 } 23 24 // NewExcludeFilterConfig returns a Metadata configuration struct with default values. 25 func NewExcludeFilterConfig() ExcludeFilterConfig { 26 return ExcludeFilterConfig{ 27 ExcludePrefixes: []string{}, 28 } 29 } 30 31 // Filter attempts to construct a metadata filter. 32 func (m ExcludeFilterConfig) Filter() (*ExcludeFilter, error) { 33 return &ExcludeFilter{ 34 m.ExcludePrefixes, 35 }, nil 36 } 37 38 // ExcludeFilter provides a way to filter metadata keys based on a Metadata 39 // config. 40 type ExcludeFilter struct { 41 excludePrefixes []string 42 } 43 44 // Iter applies a function to each metadata key value pair that passes the 45 // filter. 46 func (f *ExcludeFilter) Iter(m types.Metadata, fn func(k, v string) error) error { 47 return m.Iter(func(k, v string) error { 48 for _, prefix := range f.excludePrefixes { 49 if strings.HasPrefix(k, prefix) { 50 return nil 51 } 52 } 53 return fn(k, v) 54 }) 55 }