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

filterx: set_fields() #397

Merged
merged 8 commits into from
Nov 29, 2024
Merged

filterx: set_fields() #397

merged 8 commits into from
Nov 29, 2024

Conversation

alltilla
Copy link
Member

@alltilla alltilla commented Nov 28, 2024

A recurring pattern in FilterX is to take a dict and set multiple fields in it with overrides or defaults.

set_fields() takes a dict as the first argument and overrides and defaults as optional parameters.

overrides and defaults are also dicts, where the key is the field's name, and the value is either an expression, or
a list of expressions. If a list is provided, each expression will be evaluated, and the first successful, non-null one will
be used to set the respective field's value. This is similar to chaining null-coalescing (??) operators, but is more performant.

overrides are always processed for each field. The defaults for a field are only processed, if the field does not already have a value set.

Example usage:

set_fields(
  my_dict,
  overrides={
    "foo": [invalid_expr, "foo_override"],
    "baz": "baz_override",
    "almafa": [invalid_expr_1, null],  # No effect
  },
  defaults={
    "foo": [invalid_expr, "foo_default"],
    "bar": "bar_default",
    "almafa": "almafa_default",
    "kortefa": [invalid_expr_1, null],  # No effect
    }
);

alltilla added a commit to alltilla/axosyslog that referenced this pull request Nov 28, 2024
Signed-off-by: Attila Szakacs <[email protected]>
alltilla added a commit to alltilla/axosyslog that referenced this pull request Nov 28, 2024
Signed-off-by: Attila Szakacs <[email protected]>
alltilla added a commit to alltilla/axosyslog that referenced this pull request Nov 28, 2024
Signed-off-by: Attila Szakacs <[email protected]>
alltilla added a commit to alltilla/axosyslog that referenced this pull request Nov 28, 2024
Signed-off-by: Attila Szakacs <[email protected]>
The inner generator part of the code is a workaround
for the grammar, and is not very clean to have.

We would like to iterate through it in some expr
ctors, like it was a normal literal generator.

Also the len() function could be useful somewhere.

Removing the literal inner generators would be a
bigger task, so I just hacked the literal generator
API a bit to support literal inner generators.

Signed-off-by: Attila Szakacs <[email protected]>
We store overrides and defaults for the modified
dict's fields.

Later on we will iterate on these and set the
field's value with the first valid expression.

Signed-off-by: Attila Szakacs <[email protected]>
alltilla added a commit to alltilla/axosyslog that referenced this pull request Nov 29, 2024
Signed-off-by: Attila Szakacs <[email protected]>
If there are overrides defined for the field,
we first iterate through them, and if there is
an expression that successfully evaluated, we
set it.

If overrides were not effective, and the field
already has a value, we are done with this field,
we leave it as it is.

If, however it did not have a value, we iterate through
the defaults, like we did with the overrides, and set
the field accordingly.

Signed-off-by: Attila Szakacs <[email protected]>
Signed-off-by: Attila Szakacs <[email protected]>
@MrAnno MrAnno merged commit bbb2176 into axoflow:main Nov 29, 2024
22 checks passed
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