From 633e2b7e7f8b6387d27d31be50cef9df2ee822b3 Mon Sep 17 00:00:00 2001 From: Attila Szakacs Date: Thu, 28 Nov 2024 11:36:39 +0100 Subject: [PATCH] news: add entry for #397 Signed-off-by: Attila Szakacs --- news/fx-feature-397.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 news/fx-feature-397.md diff --git a/news/fx-feature-397.md b/news/fx-feature-397.md new file mode 100644 index 000000000..2c89624fe --- /dev/null +++ b/news/fx-feature-397.md @@ -0,0 +1,36 @@ +`set_fields()`: Added new function to set a dict's fields with overrides and defaults. + +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 + } +); +```