diff --git a/internal/processor/string-filter.go b/internal/processor/filter-regex.go similarity index 58% rename from internal/processor/string-filter.go rename to internal/processor/filter-regex.go index 03e3df8..0dba8b1 100644 --- a/internal/processor/string-filter.go +++ b/internal/processor/filter-regex.go @@ -9,38 +9,38 @@ import ( "github.com/jwetzell/showbridge-go/internal/config" ) -type StringFilter struct { +type FilterRegex struct { config config.ProcessorConfig Pattern *regexp.Regexp } -func (sf *StringFilter) Process(ctx context.Context, payload any) (any, error) { +func (fr *FilterRegex) Process(ctx context.Context, payload any) (any, error) { payloadString, ok := GetAnyAs[string](payload) if !ok { - return nil, errors.New("string.filter processor only accepts a string") + return nil, errors.New("filter.regex processor only accepts a string") } - if !sf.Pattern.MatchString(payloadString) { + if !fr.Pattern.MatchString(payloadString) { return nil, nil } return payloadString, nil } -func (sf *StringFilter) Type() string { - return sf.config.Type +func (fr *FilterRegex) Type() string { + return fr.config.Type } func init() { RegisterProcessor(ProcessorRegistration{ - Type: "string.filter", + Type: "filter.regex", New: func(config config.ProcessorConfig) (Processor, error) { params := config.Params patternString, err := params.GetString("pattern") if err != nil { - return nil, fmt.Errorf("string.filter pattern error: %w", err) + return nil, fmt.Errorf("filter.regex pattern error: %w", err) } patternRegexp, err := regexp.Compile(patternString) @@ -49,7 +49,7 @@ func init() { return nil, err } - return &StringFilter{config: config, Pattern: patternRegexp}, nil + return &FilterRegex{config: config, Pattern: patternRegexp}, nil }, }) } diff --git a/internal/processor/test/string-filter_test.go b/internal/processor/test/filter-regex_test.go similarity index 64% rename from internal/processor/test/string-filter_test.go rename to internal/processor/test/filter-regex_test.go index 38ac379..224f879 100644 --- a/internal/processor/test/string-filter_test.go +++ b/internal/processor/test/filter-regex_test.go @@ -9,23 +9,23 @@ import ( ) func TestStringFilterFromRegistry(t *testing.T) { - registration, ok := processor.ProcessorRegistry["string.filter"] + registration, ok := processor.ProcessorRegistry["filter.regex"] if !ok { - t.Fatalf("string.filter processor not registered") + t.Fatalf("filter.regex processor not registered") } processorInstance, err := registration.New(config.ProcessorConfig{ - Type: "string.filter", + Type: "filter.regex", Params: map[string]any{ "pattern": "hello", }, }) if err != nil { - t.Fatalf("failed to create string.filter processor: %s", err) + t.Fatalf("failed to create filter.regex processor: %s", err) } - if processorInstance.Type() != "string.filter" { - t.Fatalf("string.filter processor has wrong type: %s", processorInstance.Type()) + if processorInstance.Type() != "filter.regex" { + t.Fatalf("filter.regex processor has wrong type: %s", processorInstance.Type()) } payload := "hello" @@ -33,17 +33,17 @@ func TestStringFilterFromRegistry(t *testing.T) { got, err := processorInstance.Process(t.Context(), payload) if err != nil { - t.Fatalf("string.filter processing failed: %s", err) + t.Fatalf("filter.regex processing failed: %s", err) } gotString, ok := got.(string) if !ok { - t.Fatalf("string.filter should return byte slice") + t.Fatalf("filter.regex should return byte slice") } if gotString != expected { - t.Fatalf("string.filter got %+v, expected %+v", got, expected) + t.Fatalf("filter.regex got %+v, expected %+v", got, expected) } } @@ -76,40 +76,40 @@ func TestGoodStringFilter(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - registration, ok := processor.ProcessorRegistry["string.filter"] + registration, ok := processor.ProcessorRegistry["filter.regex"] if !ok { - t.Fatalf("string.filter processor not registered") + t.Fatalf("filter.regex processor not registered") } processorInstance, err := registration.New(config.ProcessorConfig{ - Type: "string.filter", + Type: "filter.regex", Params: test.params, }) if err != nil { - t.Fatalf("string.filter failed to create processor: %s", err) + t.Fatalf("filter.regex failed to create processor: %s", err) } got, err := processorInstance.Process(t.Context(), test.payload) if err != nil { - t.Fatalf("string.filter processing failed: %s", err) + t.Fatalf("filter.regex processing failed: %s", err) } if test.expected == nil { if got != nil { - t.Fatalf("string.filter got %+v, expected nil", got) + t.Fatalf("filter.regex got %+v, expected nil", got) } return } gotString, ok := got.(string) if !ok { - t.Fatalf("string.filter returned a %T payload: %s", got, got) + t.Fatalf("filter.regex returned a %T payload: %s", got, got) } if !reflect.DeepEqual(gotString, test.expected) { - t.Fatalf("string.filter got %+v, expected %+v", gotString, test.expected) + t.Fatalf("filter.regex got %+v, expected %+v", gotString, test.expected) } }) } @@ -126,7 +126,7 @@ func TestBadStringFilter(t *testing.T) { name: "no pattern param", payload: "hello", params: map[string]any{}, - errorString: "string.filter pattern error: not found", + errorString: "filter.regex pattern error: not found", }, { name: "non-string input", @@ -134,7 +134,7 @@ func TestBadStringFilter(t *testing.T) { params: map[string]any{ "pattern": "hello", }, - errorString: "string.filter processor only accepts a string", + errorString: "filter.regex processor only accepts a string", }, { name: "non-string pattern param", @@ -142,7 +142,7 @@ func TestBadStringFilter(t *testing.T) { params: map[string]any{ "pattern": 123, }, - errorString: "string.filter pattern error: not a string", + errorString: "filter.regex pattern error: not a string", }, { name: "invalid regex pattern", @@ -156,19 +156,19 @@ func TestBadStringFilter(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - registration, ok := processor.ProcessorRegistry["string.filter"] + registration, ok := processor.ProcessorRegistry["filter.regex"] if !ok { - t.Fatalf("string.filter processor not registered") + t.Fatalf("filter.regex processor not registered") } processorInstance, err := registration.New(config.ProcessorConfig{ - Type: "string.filter", + Type: "filter.regex", Params: test.params, }) if err != nil { if test.errorString != err.Error() { - t.Fatalf("string.filter got error '%s', expected '%s'", err.Error(), test.errorString) + t.Fatalf("filter.regex got error '%s', expected '%s'", err.Error(), test.errorString) } return } @@ -176,11 +176,11 @@ func TestBadStringFilter(t *testing.T) { got, err := processorInstance.Process(t.Context(), test.payload) if err == nil { - t.Fatalf("string.filter expected to fail but got payload: %s", got) + t.Fatalf("filter.regex expected to fail but got payload: %s", got) } if err.Error() != test.errorString { - t.Fatalf("string.filter got error '%s', expected '%s'", err.Error(), test.errorString) + t.Fatalf("filter.regex got error '%s', expected '%s'", err.Error(), test.errorString) } }) } diff --git a/schema/processors.schema.json b/schema/processors.schema.json index 71baa65..f174c26 100644 --- a/schema/processors.schema.json +++ b/schema/processors.schema.json @@ -65,6 +65,29 @@ "required": ["type", "params"], "additionalProperties": false }, + { + "type": "object", + "title": "Filter by Regex", + "properties": { + "type": { + "type": "string", + "const": "filter.regex" + }, + "params": { + "type": "object", + "properties": { + "pattern": { + "title": "Pattern", + "type": "string" + } + }, + "required": ["pattern"], + "additionalProperties": false + } + }, + "required": ["type", "params"], + "additionalProperties": false + }, { "type": "object", "title": "Parse Float", @@ -774,29 +797,6 @@ "required": ["type"], "additionalProperties": false }, - { - "type": "object", - "title": "Filter String", - "properties": { - "type": { - "type": "string", - "const": "string.filter" - }, - "params": { - "type": "object", - "properties": { - "pattern": { - "title": "Pattern", - "type": "string" - } - }, - "required": ["pattern"], - "additionalProperties": false - } - }, - "required": ["type", "params"], - "additionalProperties": false - }, { "type": "object", "title": "Split String",