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

feat: Add Condition Inspector #86

Merged
merged 1 commit into from
Mar 21, 2023
Merged

Conversation

jshlbrd
Copy link
Contributor

@jshlbrd jshlbrd commented Mar 17, 2023

Description

  • Adds a Condition inspector

Motivation and Context

We have some use cases for creating complex conditions that mimic this logic:

  • ( foo OR bar ) AND ( baz OR qux )
  • ( foo AND bar ) OR ( baz AND qux )

The simplest way to do this without a breaking change is to make a Condition inspector, which is a meta method similar to the ForEach inspector or the Pipeline processor. The Condition inspector is a condition (operator and inspectors) that can be referenced in other conditions to create the joined logic shown above. For example, the config below copies the value from foo to bar if the logic ( length(foo) > 0 AND foo starts_with "x" ) OR ( length(foo) < 4 AND foo equals "bar" ) is true:

local processors = [
  sub.interfaces.processor.copy(
    settings={ key: 'foo', set_key: 'bar', condition: sub.interfaces.operator.any([
        sub.interfaces.inspector.condition(
          options=sub.interfaces.operator.all([
            sub.patterns.inspector.length.gt_zero(key='foo'),
            sub.patterns.inspector.strings.starts_with(key='foo', expression='x'),
          ]),
        ),
        sub.interfaces.inspector.condition(
          options=sub.interfaces.operator.all([
            sub.interfaces.inspector.length(
              settings={ key: 'foo' },
              options={ type: 'less_than', value: 4 }
            ),
            sub.patterns.inspector.strings.equals(key='foo', expression='bar'),
          ]),
        ),
      ]) },
  )
];

The alternative to this is a breaking change in the conditions' pkg Config struct that takes nested conditions. I'd like to avoid breaking changes till v1.0, but it might be worth considering if the config for that package should change.

How Has This Been Tested?

Added new unit tests.

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.

@jshlbrd jshlbrd marked this pull request as ready for review March 17, 2023 15:37
@jshlbrd jshlbrd requested a review from a team as a code owner March 17, 2023 15:37
@jshlbrd jshlbrd merged commit e1fcee6 into main Mar 21, 2023
@jshlbrd jshlbrd deleted the jshlbrd/feat/condition-inspector branch March 21, 2023 18:24
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