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 all commits
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
13 changes: 11 additions & 2 deletions condition/number_equal_to.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,22 @@ func (insp *numberEqualTo) Condition(ctx context.Context, msg *message.Message)
return insp.match(f, compare), nil
}


val := msg.GetValue(insp.conf.Object.SourceKey)

// 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 !val.Exists() {
return false, 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
return insp.match(val.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,
},
{
"fail",
config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "baz",
},
"value": 0,
},
},
[]byte(`{"foo": 100, "bar": 200}`),
false,
},
{
"fail",
config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "baz",
"target_key": "bar",
},
},
},
[]byte(`{"foo": 100, "bar": 200}`),
false,
},
{
"fail",
config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"target_key": "abc",
},
},
},
[]byte(`100`),
false,
},
{
"fail",
config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "foo",
"target_key": "baz",
},
},
},
[]byte(`{"foo": 100, "bar": 200}`),
false,
},
{
"fail",
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
13 changes: 10 additions & 3 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)
val := msg.GetValue(insp.conf.Object.SourceKey)

// 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 !val.Exists() {
return false, 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(), compare), nil
return bytes.Equal(val.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,
},
{
"fail",
config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "foo",
},
"value": "abc",
},
},
[]byte(`{"bar": "abc", "baz": "0"}`),
false,
},
{
"fail",
config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "foo",
},
"value": "",
},
},
[]byte(`{"bar": "abc", "baz": "0"}`),
false,
},
{
"fail",
config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"source_key": "foo",
"target_key": "baz",
},
},
},
[]byte(`{"bar": "abc", "baz": "0"}`),
false,
},
{
"fail",
config.Config{
Settings: map[string]interface{}{
"object": map[string]interface{}{
"target_key": "foo",
},
},
},
[]byte(`{"bar": "abc", "baz": "0"}`),
false,
},
{
"fail",
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