Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(condition): handling of missing source and/or target keys #277

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions condition/number_equal_to.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,21 @@ func (insp *numberEqualTo) Condition(ctx context.Context, msg *message.Message)

return insp.match(f, compare), nil
}
source_value := msg.GetValue(insp.conf.Object.SourceKey)
target_value := msg.GetValue(insp.conf.Object.TargetKey)

target := msg.GetValue(insp.conf.Object.TargetKey)
if target.Exists() {
compare = target.Float()
// for gjson's GetValue, if the path is empty string (indicating source key or target key is not present),
// the Result.Exists() will return false
// If source or target key is present but value cannot be found, always return false
if !source_value.Exists() || insp.conf.Object.TargetKey != "" && !target_value.Exists() {
return false, nil
}

if target_value.Exists() {
compare = target_value.Float()
}
jtan-brex marked this conversation as resolved.
Show resolved Hide resolved

v := msg.GetValue(insp.conf.Object.SourceKey)
return insp.match(v.Float(), compare), nil
return insp.match(source_value.Float(), compare), nil
}

func (c *numberEqualTo) match(f float64, t float64) bool {
Expand Down
64 changes: 64 additions & 0 deletions condition/number_equal_to_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,70 @@ var numberEqualToTests = []struct {
[]byte(`{"foo": 100, "bar": 200}`),
false,
},
{
"pass",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any tests that return false should be labeled as "fail" ("pass" is for tests that return true).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gotcha, thanks for pointing that out! fixed test cases to reflect expected 'fail' outcomes.

config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "baz",
},
"value": 0,
},
},
[]byte(`{"foo": 100, "bar": 200}`),
false,
},
{
"pass",
config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "baz",
"target_key": "bar",
},
},
},
[]byte(`{"foo": 100, "bar": 200}`),
false,
},
{
"pass",
config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"target_key": "abc",
},
},
},
[]byte(`100`),
false,
},
{
"pass",
config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "foo",
"target_key": "baz",
},
},
},
[]byte(`{"foo": 100, "bar": 200}`),
false,
},
{
"pass",
config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "baz",
"target_key": "abc",
},
},
},
[]byte(`{"foo": 100, "bar": 200}`),
false,
},
}

func TestNumberEqualTo(t *testing.T) {
Expand Down
17 changes: 12 additions & 5 deletions condition/string_equal_to.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,21 @@ func (insp *stringEqualTo) Condition(ctx context.Context, msg *message.Message)
return bytes.Equal(msg.Data(), compare), nil
}

target := msg.GetValue(insp.conf.Object.TargetKey)
source_value := msg.GetValue(insp.conf.Object.SourceKey)
target_value := msg.GetValue(insp.conf.Object.TargetKey)

if target.Exists() {
compare = target.Bytes()
// for gjson's GetValue, if the path is empty string (indicating source key or target key is not present),
// the Result.Exists() will return false
// If source or target key is present but value cannot be found, always return false
if !source_value.Exists() || insp.conf.Object.TargetKey != "" && !target_value.Exists() {
return false, nil
}

if target_value.Exists() {
compare = target_value.Bytes()
}

value := msg.GetValue(insp.conf.Object.SourceKey)
return bytes.Equal(value.Bytes(), compare), nil
return bytes.Equal(source_value.Bytes(), compare), nil
}

func (c *stringEqualTo) String() string {
Expand Down
64 changes: 64 additions & 0 deletions condition/string_equal_to_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,70 @@ var stringEqualToTests = []struct {
[]byte(`{"foo":"abc", "bar":"def"}`),
false,
},
{
"pass",
config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "foo",
},
"value": "abc",
},
},
[]byte(`{"bar": "abc", "baz": "0"}`),
false,
},
{
"pass",
config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "foo",
},
"value": "",
},
},
[]byte(`{"bar": "abc", "baz": "0"}`),
false,
},
{
"pass",
config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "foo",
"target_key": "baz",
},
},
},
[]byte(`{"bar": "abc", "baz": "0"}`),
false,
},
{
"pass",
config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"target_key": "foo",
},
},
},
[]byte(`{"bar": "abc", "baz": "0"}`),
false,
},
{
"pass",
config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "bar",
"target_key": "foo",
},
},
},
[]byte(`{"bar": "abc", "baz": "0"}`),
false,
},
}

func TestStringEqualTo(t *testing.T) {
Expand Down