rename string.filter to filter.regex

This commit is contained in:
Joel Wetzell
2026-03-08 17:19:49 -05:00
parent 6e53cbe8fd
commit cb71b5c5b8
3 changed files with 58 additions and 58 deletions

View File

@@ -9,38 +9,38 @@ import (
"github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/config"
) )
type StringFilter struct { type FilterRegex struct {
config config.ProcessorConfig config config.ProcessorConfig
Pattern *regexp.Regexp 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) payloadString, ok := GetAnyAs[string](payload)
if !ok { 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 nil, nil
} }
return payloadString, nil return payloadString, nil
} }
func (sf *StringFilter) Type() string { func (fr *FilterRegex) Type() string {
return sf.config.Type return fr.config.Type
} }
func init() { func init() {
RegisterProcessor(ProcessorRegistration{ RegisterProcessor(ProcessorRegistration{
Type: "string.filter", Type: "filter.regex",
New: func(config config.ProcessorConfig) (Processor, error) { New: func(config config.ProcessorConfig) (Processor, error) {
params := config.Params params := config.Params
patternString, err := params.GetString("pattern") patternString, err := params.GetString("pattern")
if err != nil { 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) patternRegexp, err := regexp.Compile(patternString)
@@ -49,7 +49,7 @@ func init() {
return nil, err return nil, err
} }
return &StringFilter{config: config, Pattern: patternRegexp}, nil return &FilterRegex{config: config, Pattern: patternRegexp}, nil
}, },
}) })
} }

View File

@@ -9,23 +9,23 @@ import (
) )
func TestStringFilterFromRegistry(t *testing.T) { func TestStringFilterFromRegistry(t *testing.T) {
registration, ok := processor.ProcessorRegistry["string.filter"] registration, ok := processor.ProcessorRegistry["filter.regex"]
if !ok { if !ok {
t.Fatalf("string.filter processor not registered") t.Fatalf("filter.regex processor not registered")
} }
processorInstance, err := registration.New(config.ProcessorConfig{ processorInstance, err := registration.New(config.ProcessorConfig{
Type: "string.filter", Type: "filter.regex",
Params: map[string]any{ Params: map[string]any{
"pattern": "hello", "pattern": "hello",
}, },
}) })
if err != nil { 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" { if processorInstance.Type() != "filter.regex" {
t.Fatalf("string.filter processor has wrong type: %s", processorInstance.Type()) t.Fatalf("filter.regex processor has wrong type: %s", processorInstance.Type())
} }
payload := "hello" payload := "hello"
@@ -33,17 +33,17 @@ func TestStringFilterFromRegistry(t *testing.T) {
got, err := processorInstance.Process(t.Context(), payload) got, err := processorInstance.Process(t.Context(), payload)
if err != nil { if err != nil {
t.Fatalf("string.filter processing failed: %s", err) t.Fatalf("filter.regex processing failed: %s", err)
} }
gotString, ok := got.(string) gotString, ok := got.(string)
if !ok { if !ok {
t.Fatalf("string.filter should return byte slice") t.Fatalf("filter.regex should return byte slice")
} }
if gotString != expected { 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 { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
registration, ok := processor.ProcessorRegistry["string.filter"] registration, ok := processor.ProcessorRegistry["filter.regex"]
if !ok { if !ok {
t.Fatalf("string.filter processor not registered") t.Fatalf("filter.regex processor not registered")
} }
processorInstance, err := registration.New(config.ProcessorConfig{ processorInstance, err := registration.New(config.ProcessorConfig{
Type: "string.filter", Type: "filter.regex",
Params: test.params, Params: test.params,
}) })
if err != nil { 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) got, err := processorInstance.Process(t.Context(), test.payload)
if err != nil { if err != nil {
t.Fatalf("string.filter processing failed: %s", err) t.Fatalf("filter.regex processing failed: %s", err)
} }
if test.expected == nil { if test.expected == nil {
if got != nil { if got != nil {
t.Fatalf("string.filter got %+v, expected nil", got) t.Fatalf("filter.regex got %+v, expected nil", got)
} }
return return
} }
gotString, ok := got.(string) gotString, ok := got.(string)
if !ok { 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) { 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", name: "no pattern param",
payload: "hello", payload: "hello",
params: map[string]any{}, params: map[string]any{},
errorString: "string.filter pattern error: not found", errorString: "filter.regex pattern error: not found",
}, },
{ {
name: "non-string input", name: "non-string input",
@@ -134,7 +134,7 @@ func TestBadStringFilter(t *testing.T) {
params: map[string]any{ params: map[string]any{
"pattern": "hello", "pattern": "hello",
}, },
errorString: "string.filter processor only accepts a string", errorString: "filter.regex processor only accepts a string",
}, },
{ {
name: "non-string pattern param", name: "non-string pattern param",
@@ -142,7 +142,7 @@ func TestBadStringFilter(t *testing.T) {
params: map[string]any{ params: map[string]any{
"pattern": 123, "pattern": 123,
}, },
errorString: "string.filter pattern error: not a string", errorString: "filter.regex pattern error: not a string",
}, },
{ {
name: "invalid regex pattern", name: "invalid regex pattern",
@@ -156,19 +156,19 @@ func TestBadStringFilter(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
registration, ok := processor.ProcessorRegistry["string.filter"] registration, ok := processor.ProcessorRegistry["filter.regex"]
if !ok { if !ok {
t.Fatalf("string.filter processor not registered") t.Fatalf("filter.regex processor not registered")
} }
processorInstance, err := registration.New(config.ProcessorConfig{ processorInstance, err := registration.New(config.ProcessorConfig{
Type: "string.filter", Type: "filter.regex",
Params: test.params, Params: test.params,
}) })
if err != nil { if err != nil {
if test.errorString != err.Error() { 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 return
} }
@@ -176,11 +176,11 @@ func TestBadStringFilter(t *testing.T) {
got, err := processorInstance.Process(t.Context(), test.payload) got, err := processorInstance.Process(t.Context(), test.payload)
if err == nil { 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 { 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)
} }
}) })
} }

View File

@@ -65,6 +65,29 @@
"required": ["type", "params"], "required": ["type", "params"],
"additionalProperties": false "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", "type": "object",
"title": "Parse Float", "title": "Parse Float",
@@ -774,29 +797,6 @@
"required": ["type"], "required": ["type"],
"additionalProperties": false "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", "type": "object",
"title": "Split String", "title": "Split String",