Skip to content

Commit

Permalink
fix(condition): handling of missing source and/or target keys (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtan-brex authored Nov 26, 2024
1 parent 0b5934b commit 090981b
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 5 deletions.
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

0 comments on commit 090981b

Please sign in to comment.