Skip to content

feat(condition): Update conditions to support source and target key comparisons#214

Merged
Mallika05 merged 2 commits intomainfrom
07-25-feat_condition_update_conditions_to_support_source_and_target_key_comparisons
Jul 26, 2024
Merged

feat(condition): Update conditions to support source and target key comparisons#214
Mallika05 merged 2 commits intomainfrom
07-25-feat_condition_update_conditions_to_support_source_and_target_key_comparisons

Conversation

@Mallika05
Copy link
Contributor

@Mallika05 Mallika05 commented Jul 25, 2024

Description

Updated the following conditions to support comparisons between source_key and target_key values.

  • number_equal_to
  • number_greater_than
  • number_less_than
  • string_equal_to
  • string_greater_than
  • string_less_than

Example:

sub.condition.number.less_than( settings={object: {source_key: 'a', target_key: 'z'}} )
sub.condition.string.less_than( settings={object: {source_key: 'a', target_key: 'z'}} )

Motivation and Context

This adds flexibility to accept both static values or target_key whichever is provided in the config. Static value is ignored if target_key is present in the condition. This solves the issue discussed here: #64.

How Has This Been Tested?

Added unit tests in respective files.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

Copy link
Contributor Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @Mallika05 and the rest of your teammates on Graphite Graphite

@Mallika05 Mallika05 marked this pull request as ready for review July 25, 2024 22:56
@Mallika05 Mallika05 requested a review from a team as a code owner July 25, 2024 22:56
@jshlbrd jshlbrd self-assigned this Jul 25, 2024
@jshlbrd
Copy link
Contributor

jshlbrd commented Jul 25, 2024

The additions look good but I have an ask for changes to each function, it's slightly better to do this:

	compare := insp.conf.Value

	if insp.conf.Object.SourceKey == "" {
		f, err := strconv.ParseFloat(string(msg.Data()), 64)
		if err != nil {
			return false, err
		}

		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

Instead of this:

	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, compare), nil
	}

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

If conf.Object.SourceKey is empty, then the condition is evaluating the message's content directly, so it can't do a comparison between two different values (e.g. {"foo":1.0} shouldn't be compared to 1.0 because it will always be false). We'll get a little more performance by accessing the TargetKey after that statement than before it.

If someone were to configure these functions like that, then that would be a user error (identical to this one).

@Mallika05 Mallika05 requested a review from jshlbrd July 26, 2024 21:29
@Mallika05 Mallika05 merged commit dfbc678 into main Jul 26, 2024
@Mallika05 Mallika05 deleted the 07-25-feat_condition_update_conditions_to_support_source_and_target_key_comparisons branch July 26, 2024 21:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants