diff --git a/condition/number_equal_to.go b/condition/number_equal_to.go index ff133720..340b40e0 100644 --- a/condition/number_equal_to.go +++ b/condition/number_equal_to.go @@ -28,6 +28,7 @@ func (insp *numberEqualTo) Inspect(ctx context.Context, msg *message.Message) (o if msg.IsControl() { return false, nil } + compare := insp.conf.Value if insp.conf.Object.SourceKey == "" { f, err := strconv.ParseFloat(string(msg.Data()), 64) @@ -35,14 +36,21 @@ func (insp *numberEqualTo) Inspect(ctx context.Context, msg *message.Message) (o return false, err } - return insp.match(f), nil + return insp.match(f, compare), nil } + + target := msg.GetValue(insp.conf.Object.TargetKey) + + if target.Exists() { + compare = target.Float() + } + v := msg.GetValue(insp.conf.Object.SourceKey) - return insp.match(v.Float()), nil + return insp.match(v.Float(), compare), nil } -func (c *numberEqualTo) match(f float64) bool { - return f == c.conf.Value +func (c *numberEqualTo) match(f float64, t float64) bool { + return f == t } func (c *numberEqualTo) String() string { diff --git a/condition/number_equal_to_test.go b/condition/number_equal_to_test.go index 6275ac98..cafc1c92 100644 --- a/condition/number_equal_to_test.go +++ b/condition/number_equal_to_test.go @@ -107,6 +107,33 @@ var numberEqualToTests = []struct { []byte(`1.4`), true, }, + { + "pass", + config.Config{ + Settings: map[string]interface{}{ + "object": map[string]interface{}{ + "source_key": "foo", + "target_key": "bar", + }, + }, + }, + []byte(`{"foo": 10, "bar": 10}`), + true, + }, + { + "fail", + config.Config{ + Settings: map[string]interface{}{ + "object": map[string]interface{}{ + "source_key": "foo", + "target_key": "bar", + }, + "value": 100, + }, + }, + []byte(`{"foo": 100, "bar": 200}`), + false, + }, } func TestNumberEqualTo(t *testing.T) { diff --git a/condition/number_greater_than.go b/condition/number_greater_than.go index fe95f8a0..27041c04 100644 --- a/condition/number_greater_than.go +++ b/condition/number_greater_than.go @@ -31,21 +31,29 @@ func (insp *numberGreaterThan) Inspect(ctx context.Context, msg *message.Message return false, nil } + compare := insp.conf.Value + if insp.conf.Object.SourceKey == "" { f, err := strconv.ParseFloat(string(msg.Data()), 64) if err != nil { return false, err } - return insp.match(f), nil + return insp.match(f, compare), nil + } + + target := msg.GetValue(insp.conf.Object.TargetKey) + + if target.Exists() { + compare = target.Float() } v := msg.GetValue(insp.conf.Object.SourceKey) - return insp.match(v.Float()), nil + return insp.match(v.Float(), compare), nil } -func (c *numberGreaterThan) match(f float64) bool { - return f > c.conf.Value +func (c *numberGreaterThan) match(f float64, t float64) bool { + return f > t } func (c *numberGreaterThan) String() string { diff --git a/condition/number_greater_than_test.go b/condition/number_greater_than_test.go index 0b2b0179..f12c27e5 100644 --- a/condition/number_greater_than_test.go +++ b/condition/number_greater_than_test.go @@ -107,6 +107,33 @@ var numberGreaterThanTests = []struct { []byte(`1`), false, }, + { + "pass", + config.Config{ + Settings: map[string]interface{}{ + "object": map[string]interface{}{ + "source_key": "foo", + "target_key": "bar", + }, + }, + }, + []byte(`{"foo": 100, "bar": 10}`), + true, + }, + { + "fail", + config.Config{ + Settings: map[string]interface{}{ + "object": map[string]interface{}{ + "source_key": "foo", + "target_key": "bar", + }, + "value": 10, + }, + }, + []byte(`{"foo": 100, "bar": 2000}`), + false, + }, } func TestNumberGreaterThan(t *testing.T) { diff --git a/condition/number_less_than.go b/condition/number_less_than.go index 602a0f6a..527739af 100644 --- a/condition/number_less_than.go +++ b/condition/number_less_than.go @@ -29,20 +29,29 @@ func (insp *numberLessThan) Inspect(ctx context.Context, msg *message.Message) ( return false, nil } + compare := insp.conf.Value + if insp.conf.Object.SourceKey == "" { f, err := strconv.ParseFloat(string(msg.Data()), 64) if err != nil { return false, err } - return insp.match(f), nil + return insp.match(f, compare), nil } + + target := msg.GetValue(insp.conf.Object.TargetKey) + + if target.Exists() { + compare = target.Float() + } + v := msg.GetValue(insp.conf.Object.SourceKey) - return insp.match(v.Float()), nil + return insp.match(v.Float(), compare), nil } -func (c *numberLessThan) match(f float64) bool { - return f < c.conf.Value +func (c *numberLessThan) match(f float64, t float64) bool { + return f < t } func (c *numberLessThan) String() string { diff --git a/condition/number_less_than_test.go b/condition/number_less_than_test.go index ec584511..860b7a9e 100644 --- a/condition/number_less_than_test.go +++ b/condition/number_less_than_test.go @@ -107,6 +107,33 @@ var numberLessThanTests = []struct { []byte(`1`), true, }, + { + "pass", + config.Config{ + Settings: map[string]interface{}{ + "object": map[string]interface{}{ + "source_key": "foo", + "target_key": "bar", + }, + }, + }, + []byte(`{"foo": 10, "bar": 100}`), + true, + }, + { + "fail", + config.Config{ + Settings: map[string]interface{}{ + "object": map[string]interface{}{ + "source_key": "foo", + "target_key": "bar", + }, + "value": 10, + }, + }, + []byte(`{"foo": 2, "bar": 1}`), + false, + }, } func TestNumberLessThan(t *testing.T) { diff --git a/condition/string_equal_to.go b/condition/string_equal_to.go index 976c05b9..c7f9760f 100644 --- a/condition/string_equal_to.go +++ b/condition/string_equal_to.go @@ -34,12 +34,20 @@ func (insp *stringEqualTo) Inspect(ctx context.Context, msg *message.Message) (o return false, nil } + compare := insp.b + if insp.conf.Object.SourceKey == "" { - return bytes.Equal(msg.Data(), insp.b), nil + return bytes.Equal(msg.Data(), compare), nil + } + + target := msg.GetValue(insp.conf.Object.TargetKey) + + if target.Exists() { + compare = target.Bytes() } value := msg.GetValue(insp.conf.Object.SourceKey) - return bytes.Equal(value.Bytes(), insp.b), nil + return bytes.Equal(value.Bytes(), compare), nil } func (c *stringEqualTo) String() string { diff --git a/condition/string_equal_to_test.go b/condition/string_equal_to_test.go index b237eccf..5e37956b 100644 --- a/condition/string_equal_to_test.go +++ b/condition/string_equal_to_test.go @@ -46,6 +46,33 @@ var stringEqualToTests = []struct { []byte("\"\""), true, }, + { + "pass", + config.Config{ + Settings: map[string]interface{}{ + "object": map[string]interface{}{ + "source_key": "foo", + "target_key": "bar", + }, + }, + }, + []byte(`{"foo":"abc", "bar":"abc"}`), + true, + }, + { + "fail", + config.Config{ + Settings: map[string]interface{}{ + "object": map[string]interface{}{ + "source_key": "foo", + "target_key": "bar", + }, + "value": "abc", + }, + }, + []byte(`{"foo":"abc", "bar":"def"}`), + false, + }, } func TestStringEqualTo(t *testing.T) { diff --git a/condition/string_greater_than.go b/condition/string_greater_than.go index 72a11186..afbe9334 100644 --- a/condition/string_greater_than.go +++ b/condition/string_greater_than.go @@ -34,12 +34,20 @@ func (insp *stringGreaterThan) Inspect(ctx context.Context, msg *message.Message return false, nil } + compare := insp.b + if insp.conf.Object.SourceKey == "" { - return bytes.Compare(msg.Data(), insp.b) > 0, nil + return bytes.Compare(msg.Data(), compare) > 0, nil + } + + target := msg.GetValue(insp.conf.Object.TargetKey) + + if target.Exists() { + compare = target.Bytes() } value := msg.GetValue(insp.conf.Object.SourceKey) - return bytes.Compare(value.Bytes(), insp.b) > 0, nil + return bytes.Compare(value.Bytes(), compare) > 0, nil } func (c *stringGreaterThan) String() string { diff --git a/condition/string_greater_than_test.go b/condition/string_greater_than_test.go index ed2cee54..52f4117c 100644 --- a/condition/string_greater_than_test.go +++ b/condition/string_greater_than_test.go @@ -36,6 +36,33 @@ var stringGreaterThanTests = []struct { []byte(`2023-01-01T00:00:00Z`), true, }, + { + "pass", + config.Config{ + Settings: map[string]interface{}{ + "object": map[string]interface{}{ + "source_key": "foo", + "target_key": "bar", + }, + }, + }, + []byte(`{"foo":"2023-01-01T00:00:00Z", "bar":"2022-01-01T00:00:00Z"}`), + true, + }, + { + "fail", + config.Config{ + Settings: map[string]interface{}{ + "object": map[string]interface{}{ + "source_key": "foo", + "target_key": "bar", + }, + "value": "greetings", + }, + }, + []byte(`{"foo":"hello", "bar":"world"}`), + false, + }, } func TestStringGreaterThan(t *testing.T) { diff --git a/condition/string_less_than.go b/condition/string_less_than.go index 4bba1f4a..48d87b1e 100644 --- a/condition/string_less_than.go +++ b/condition/string_less_than.go @@ -34,12 +34,19 @@ func (insp *stringLessThan) Inspect(ctx context.Context, msg *message.Message) ( return false, nil } + compare := insp.b + if insp.conf.Object.SourceKey == "" { - return bytes.Compare(msg.Data(), insp.b) < 0, nil + return bytes.Compare(msg.Data(), compare) < 0, nil + } + target := msg.GetValue(insp.conf.Object.TargetKey) + + if target.Exists() { + compare = target.Bytes() } value := msg.GetValue(insp.conf.Object.SourceKey) - return bytes.Compare(value.Bytes(), insp.b) < 0, nil + return bytes.Compare(value.Bytes(), compare) < 0, nil } func (c *stringLessThan) String() string { diff --git a/condition/string_less_than_test.go b/condition/string_less_than_test.go index 4cd5745f..b77008ce 100644 --- a/condition/string_less_than_test.go +++ b/condition/string_less_than_test.go @@ -36,6 +36,33 @@ var stringLessThanTests = []struct { []byte(`2023-01-01T00:00:00Z`), true, }, + { + "pass", + config.Config{ + Settings: map[string]interface{}{ + "object": map[string]interface{}{ + "source_key": "foo", + "target_key": "bar", + }, + }, + }, + []byte(`{"foo":"2022-01-01T00:00:00Z", "bar":"2023-01-01T00:00:00Z"}`), + true, + }, + { + "fail", + config.Config{ + Settings: map[string]interface{}{ + "object": map[string]interface{}{ + "source_key": "foo", + "target_key": "bar", + }, + "value": "2025-01-01", + }, + }, + []byte(`{"foo":"2024-01-01T00:00:00Z", "bar":"2023-01-01"}`), + false, + }, } func TestStringLessThan(t *testing.T) {