Skip to content

Commit

Permalink
fix(condition): simplify value retrieval in number and string equalit…
Browse files Browse the repository at this point in the history
…y checks
  • Loading branch information
jtan-brex committed Nov 20, 2024
1 parent 7329eeb commit 070e33c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
14 changes: 8 additions & 6 deletions condition/number_equal_to.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,23 @@ 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)


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 !source_value.Exists() || insp.conf.Object.TargetKey != "" && !target_value.Exists() {
if !val.Exists() {
return false, nil
}

if target_value.Exists() {
compare = target_value.Float()
target := msg.GetValue(insp.conf.Object.TargetKey)
if target.Exists() {
compare = target.Float()
}

return insp.match(source_value.Float(), compare), nil
return insp.match(val.Float(), compare), nil
}

func (c *numberEqualTo) match(f float64, t float64) bool {
Expand Down
12 changes: 6 additions & 6 deletions condition/string_equal_to.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@ func (insp *stringEqualTo) Condition(ctx context.Context, msg *message.Message)
return bytes.Equal(msg.Data(), compare), nil
}

source_value := msg.GetValue(insp.conf.Object.SourceKey)
target_value := 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 !source_value.Exists() || insp.conf.Object.TargetKey != "" && !target_value.Exists() {
if !val.Exists() {
return false, nil
}

if target_value.Exists() {
compare = target_value.Bytes()
target := msg.GetValue(insp.conf.Object.TargetKey)
if target.Exists() {
compare = target.Bytes()
}

return bytes.Equal(source_value.Bytes(), compare), nil
return bytes.Equal(val.Bytes(), compare), nil
}

func (c *stringEqualTo) String() string {
Expand Down

0 comments on commit 070e33c

Please sign in to comment.