From 8eaefc0c8b42bc12e5c4d031c63cf16871c15a7a Mon Sep 17 00:00:00 2001 From: Mallika Kaur Oberoi Date: Thu, 25 Jul 2024 15:43:49 -0700 Subject: [PATCH 1/2] feat(condition): Update conditions to support source and target key comparisons --- condition/number_equal_to.go | 16 ++++++++++++---- condition/number_equal_to_test.go | 27 +++++++++++++++++++++++++++ condition/number_greater_than.go | 16 ++++++++++++---- condition/number_greater_than_test.go | 27 +++++++++++++++++++++++++++ condition/number_less_than.go | 17 +++++++++++++---- condition/number_less_than_test.go | 27 +++++++++++++++++++++++++++ condition/string_equal_to.go | 12 ++++++++++-- condition/string_equal_to_test.go | 27 +++++++++++++++++++++++++++ condition/string_greater_than.go | 12 ++++++++++-- condition/string_greater_than_test.go | 27 +++++++++++++++++++++++++++ condition/string_less_than.go | 12 ++++++++++-- condition/string_less_than_test.go | 27 +++++++++++++++++++++++++++ 12 files changed, 229 insertions(+), 18 deletions(-) diff --git a/condition/number_equal_to.go b/condition/number_equal_to.go index ff133720..e6306628 100644 --- a/condition/number_equal_to.go +++ b/condition/number_equal_to.go @@ -28,6 +28,13 @@ func (insp *numberEqualTo) Inspect(ctx context.Context, msg *message.Message) (o if msg.IsControl() { return false, nil } + compare := insp.conf.Value + + target := msg.GetValue(insp.conf.Object.TargetKey) + + if target.Exists() { + compare = target.Float() + } if insp.conf.Object.SourceKey == "" { f, err := strconv.ParseFloat(string(msg.Data()), 64) @@ -35,14 +42,15 @@ 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 } + 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..b09e8e6a 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 + + target := msg.GetValue(insp.conf.Object.TargetKey) + + if target.Exists() { + compare = target.Float() + } + 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 } 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..976cd559 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 + + target := msg.GetValue(insp.conf.Object.TargetKey) + + if target.Exists() { + compare = target.Float() + } + 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 } + 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..113386a5 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 + + target := msg.GetValue(insp.conf.Object.TargetKey) + + if target.Exists() { + compare = target.Bytes() + } + if insp.conf.Object.SourceKey == "" { - return bytes.Equal(msg.Data(), insp.b), nil + return bytes.Equal(msg.Data(), compare), nil } 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..94eb5675 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 + + target := msg.GetValue(insp.conf.Object.TargetKey) + + if target.Exists() { + compare = target.Bytes() + } + if insp.conf.Object.SourceKey == "" { - return bytes.Compare(msg.Data(), insp.b) > 0, nil + return bytes.Compare(msg.Data(), compare) > 0, nil } 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..f021e9c8 100644 --- a/condition/string_less_than.go +++ b/condition/string_less_than.go @@ -34,12 +34,20 @@ func (insp *stringLessThan) Inspect(ctx context.Context, msg *message.Message) ( return false, nil } + compare := insp.b + + target := msg.GetValue(insp.conf.Object.TargetKey) + + if target.Exists() { + compare = target.Bytes() + } + if insp.conf.Object.SourceKey == "" { - return bytes.Compare(msg.Data(), insp.b) < 0, nil + return bytes.Compare(msg.Data(), compare) < 0, nil } 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) { From 95016ce855316ed62fe1fd934857119105fa129c Mon Sep 17 00:00:00 2001 From: Mallika Kaur Oberoi Date: Fri, 26 Jul 2024 14:24:33 -0700 Subject: [PATCH 2/2] refactor: update target_key fetch order --- condition/number_equal_to.go | 12 ++++++------ condition/number_greater_than.go | 12 ++++++------ condition/number_less_than.go | 12 ++++++------ condition/string_equal_to.go | 8 ++++---- condition/string_greater_than.go | 8 ++++---- condition/string_less_than.go | 7 +++---- 6 files changed, 29 insertions(+), 30 deletions(-) diff --git a/condition/number_equal_to.go b/condition/number_equal_to.go index e6306628..340b40e0 100644 --- a/condition/number_equal_to.go +++ b/condition/number_equal_to.go @@ -30,12 +30,6 @@ func (insp *numberEqualTo) Inspect(ctx context.Context, msg *message.Message) (o } compare := insp.conf.Value - target := msg.GetValue(insp.conf.Object.TargetKey) - - if target.Exists() { - compare = target.Float() - } - if insp.conf.Object.SourceKey == "" { f, err := strconv.ParseFloat(string(msg.Data()), 64) if err != nil { @@ -45,6 +39,12 @@ func (insp *numberEqualTo) Inspect(ctx context.Context, msg *message.Message) (o 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(), compare), nil } diff --git a/condition/number_greater_than.go b/condition/number_greater_than.go index b09e8e6a..27041c04 100644 --- a/condition/number_greater_than.go +++ b/condition/number_greater_than.go @@ -33,12 +33,6 @@ func (insp *numberGreaterThan) Inspect(ctx context.Context, msg *message.Message compare := insp.conf.Value - target := msg.GetValue(insp.conf.Object.TargetKey) - - if target.Exists() { - compare = target.Float() - } - if insp.conf.Object.SourceKey == "" { f, err := strconv.ParseFloat(string(msg.Data()), 64) if err != nil { @@ -48,6 +42,12 @@ func (insp *numberGreaterThan) Inspect(ctx context.Context, msg *message.Message 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(), compare), nil } diff --git a/condition/number_less_than.go b/condition/number_less_than.go index 976cd559..527739af 100644 --- a/condition/number_less_than.go +++ b/condition/number_less_than.go @@ -31,12 +31,6 @@ func (insp *numberLessThan) Inspect(ctx context.Context, msg *message.Message) ( compare := insp.conf.Value - target := msg.GetValue(insp.conf.Object.TargetKey) - - if target.Exists() { - compare = target.Float() - } - if insp.conf.Object.SourceKey == "" { f, err := strconv.ParseFloat(string(msg.Data()), 64) if err != nil { @@ -46,6 +40,12 @@ func (insp *numberLessThan) Inspect(ctx context.Context, msg *message.Message) ( 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(), compare), nil } diff --git a/condition/string_equal_to.go b/condition/string_equal_to.go index 113386a5..c7f9760f 100644 --- a/condition/string_equal_to.go +++ b/condition/string_equal_to.go @@ -36,16 +36,16 @@ func (insp *stringEqualTo) Inspect(ctx context.Context, msg *message.Message) (o compare := insp.b + if insp.conf.Object.SourceKey == "" { + return bytes.Equal(msg.Data(), compare), nil + } + target := msg.GetValue(insp.conf.Object.TargetKey) if target.Exists() { compare = target.Bytes() } - if insp.conf.Object.SourceKey == "" { - return bytes.Equal(msg.Data(), compare), nil - } - value := msg.GetValue(insp.conf.Object.SourceKey) return bytes.Equal(value.Bytes(), compare), nil } diff --git a/condition/string_greater_than.go b/condition/string_greater_than.go index 94eb5675..afbe9334 100644 --- a/condition/string_greater_than.go +++ b/condition/string_greater_than.go @@ -36,16 +36,16 @@ func (insp *stringGreaterThan) Inspect(ctx context.Context, msg *message.Message compare := insp.b + if insp.conf.Object.SourceKey == "" { + return bytes.Compare(msg.Data(), compare) > 0, nil + } + target := msg.GetValue(insp.conf.Object.TargetKey) if target.Exists() { compare = target.Bytes() } - if insp.conf.Object.SourceKey == "" { - return bytes.Compare(msg.Data(), compare) > 0, nil - } - value := msg.GetValue(insp.conf.Object.SourceKey) return bytes.Compare(value.Bytes(), compare) > 0, nil } diff --git a/condition/string_less_than.go b/condition/string_less_than.go index f021e9c8..48d87b1e 100644 --- a/condition/string_less_than.go +++ b/condition/string_less_than.go @@ -36,16 +36,15 @@ func (insp *stringLessThan) Inspect(ctx context.Context, msg *message.Message) ( compare := insp.b + if insp.conf.Object.SourceKey == "" { + return bytes.Compare(msg.Data(), compare) < 0, nil + } target := msg.GetValue(insp.conf.Object.TargetKey) if target.Exists() { compare = target.Bytes() } - if insp.conf.Object.SourceKey == "" { - return bytes.Compare(msg.Data(), compare) < 0, nil - } - value := msg.GetValue(insp.conf.Object.SourceKey) return bytes.Compare(value.Bytes(), compare) < 0, nil }