-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(condition): Add MetaErr Inspector (#217)
- Loading branch information
Showing
3 changed files
with
257 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package condition | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"regexp" | ||
|
||
"github.com/brexhq/substation/config" | ||
"github.com/brexhq/substation/message" | ||
|
||
iconfig "github.com/brexhq/substation/internal/config" | ||
"github.com/brexhq/substation/internal/errors" | ||
) | ||
|
||
type metaErrConfig struct { | ||
// Inspector used to inspect the message. If the inspector | ||
// throws an error, this inspector will return false. | ||
Inspector config.Config `json:"inspector"` | ||
// ErrorMessages are regular expressions that match error messages and determine | ||
// if the error should be caught. | ||
// | ||
// This is optional and defaults to an empty list (all errors are caught). | ||
ErrorMessages []string `json:"error_messages"` | ||
} | ||
|
||
func (c *metaErrConfig) Decode(in interface{}) error { | ||
return iconfig.Decode(in, c) | ||
} | ||
|
||
func (c *metaErrConfig) Validate() error { | ||
if c.Inspector.Type == "" { | ||
return fmt.Errorf("inspector: %v", errors.ErrMissingRequiredOption) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func newMetaErr(ctx context.Context, cfg config.Config) (*metaErr, error) { | ||
conf := metaErrConfig{} | ||
if err := conf.Decode(cfg.Settings); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := conf.Validate(); err != nil { | ||
return nil, err | ||
} | ||
|
||
i, err := newInspector(ctx, conf.Inspector) | ||
if err != nil { | ||
return nil, fmt.Errorf("condition: meta_err: %v", err) | ||
} | ||
|
||
meta := metaErr{ | ||
conf: conf, | ||
insp: i, | ||
} | ||
|
||
meta.errorMessages = make([]*regexp.Regexp, len(conf.ErrorMessages)) | ||
for i, em := range conf.ErrorMessages { | ||
re, err := regexp.Compile(em) | ||
if err != nil { | ||
return nil, fmt.Errorf("condition: meta_err: %v", err) | ||
} | ||
|
||
meta.errorMessages[i] = re | ||
} | ||
|
||
return &meta, nil | ||
} | ||
|
||
type metaErr struct { | ||
conf metaErrConfig | ||
|
||
insp inspector | ||
errorMessages []*regexp.Regexp | ||
} | ||
|
||
func (c *metaErr) Inspect(ctx context.Context, msg *message.Message) (bool, error) { | ||
if msg.IsControl() { | ||
return false, nil | ||
} | ||
|
||
match, err := c.insp.Inspect(ctx, msg) | ||
if err != nil { | ||
// Catch all errors. | ||
if len(c.errorMessages) == 0 { | ||
return false, nil | ||
} | ||
|
||
// Catch specific errors. | ||
for _, re := range c.errorMessages { | ||
if re.MatchString(err.Error()) { | ||
return false, nil | ||
} | ||
} | ||
|
||
return false, fmt.Errorf("condition: meta_err: %v", err) | ||
} | ||
|
||
return match, nil | ||
} | ||
|
||
func (c *metaErr) String() string { | ||
b, _ := json.Marshal(c.conf) | ||
return string(b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package condition | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/brexhq/substation/config" | ||
"github.com/brexhq/substation/message" | ||
) | ||
|
||
var _ inspector = &metaErr{} | ||
|
||
var metaErrTests = []struct { | ||
name string | ||
cfg config.Config | ||
data []byte | ||
expected bool | ||
}{ | ||
{ | ||
"catch_all", | ||
config.Config{ | ||
Settings: map[string]interface{}{ | ||
"inspector": map[string]interface{}{ | ||
"settings": map[string]interface{}{ | ||
"object": map[string]interface{}{ | ||
"source_key": "a", | ||
}, | ||
"inspector": map[string]interface{}{ | ||
"type": "string_starts_with", | ||
"settings": map[string]interface{}{ | ||
"string": "c", | ||
}, | ||
}, | ||
"type": "any", | ||
}, | ||
"type": "meta_for_each", | ||
}, | ||
}, | ||
}, | ||
[]byte(`{"a":"bcd"}`), | ||
false, | ||
}, | ||
{ | ||
"catch_one", | ||
config.Config{ | ||
Settings: map[string]interface{}{ | ||
"error_messages": []string{"input must be an array"}, | ||
"inspector": map[string]interface{}{ | ||
"settings": map[string]interface{}{ | ||
"object": map[string]interface{}{ | ||
"source_key": "a", | ||
}, | ||
"inspector": map[string]interface{}{ | ||
"type": "string_starts_with", | ||
"settings": map[string]interface{}{ | ||
"string": "c", | ||
}, | ||
}, | ||
"type": "any", | ||
}, | ||
"type": "meta_for_each", | ||
}, | ||
}, | ||
}, | ||
[]byte(`{"a":"bcd"}`), | ||
false, | ||
}, | ||
{ | ||
"no_error", | ||
config.Config{ | ||
Settings: map[string]interface{}{ | ||
"inspector": map[string]interface{}{ | ||
"settings": map[string]interface{}{ | ||
"object": map[string]interface{}{ | ||
"source_key": "a", | ||
}, | ||
"inspector": map[string]interface{}{ | ||
"type": "string_starts_with", | ||
"settings": map[string]interface{}{ | ||
"string": "c", | ||
}, | ||
}, | ||
"type": "any", | ||
}, | ||
"type": "meta_for_each", | ||
}, | ||
}, | ||
}, | ||
[]byte(`{"a":["bcd"]}`), | ||
true, | ||
}, | ||
} | ||
|
||
func TestMetaErr(t *testing.T) { | ||
ctx := context.TODO() | ||
|
||
for _, test := range metaErrTests { | ||
t.Run(test.name, func(t *testing.T) { | ||
message := message.New().SetData(test.data) | ||
|
||
insp, err := newMetaErr(ctx, test.cfg) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
check, err := insp.Inspect(ctx, message) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if test.expected != check { | ||
t.Errorf("expected %v, got %v, %v", test.expected, check, string(test.data)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func benchmarkMetaErr(b *testing.B, insp *metaErr, message *message.Message) { | ||
ctx := context.TODO() | ||
for i := 0; i < b.N; i++ { | ||
_, _ = insp.Inspect(ctx, message) | ||
} | ||
} | ||
|
||
func BenchmarkMetaErr(b *testing.B) { | ||
for _, test := range metaErrTests { | ||
insp, err := newMetaErr(context.TODO(), test.cfg) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
|
||
b.Run(test.name, | ||
func(b *testing.B) { | ||
message := message.New().SetData(test.data) | ||
benchmarkMetaErr(b, insp, message) | ||
}, | ||
) | ||
} | ||
} |