-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Attila Szakacs <[email protected]>
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
); | ||
``` |