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

Added the default_value parameter to return it when unmatched. #20613

Merged
merged 5 commits into from Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions Packs/MapPattern/ReleaseNotes/1_0_6.md
@@ -0,0 +1,6 @@

#### Scripts
##### MapPattern
- Fixed an issue where the script returned a success message when a syntax error occurred.
- Added the **default_value** parameter to return it when all the patterns are not satisfied.
- Updated the Docker image to: *demisto/python3:3.10.5.31928*.
12 changes: 9 additions & 3 deletions Packs/MapPattern/Scripts/MapPattern/MapPattern.py
Expand Up @@ -400,6 +400,7 @@ def main():
context = args.get('context')
fields_comp_mode = argToBoolean(args.get('compare_fields') or 'false')
wildcards = argToList(args.get('wildcards'))
default_value = args.get('default_value')
regex_flags = re.IGNORECASE if argToBoolean(args.get('caseless') or 'true') else 0
for flag in argToList(args.get('flags', '')):
if flag in ('dotall', 's'):
Expand All @@ -424,22 +425,27 @@ def main():
fields_comp_mode=fields_comp_mode,
wildcards=wildcards,
regex_flags=regex_flags)

matched = False
if fields_comp_mode:
if isinstance(value, dict):
value, _ = tr.translate_fields(
value, matched = tr.translate_fields(
obj_value=value,
field_mapping=mappings,
priority=priority,
algorithm=algorithm)
else:
if not isinstance(value, (dict, list)):
value, _ = tr.translate(
value, matched = tr.translate(
source=value,
pattern_mapping=mappings,
priority=priority,
algorithm=algorithm)
if default_value and not matched:
value = default_value
except Exception as err:
return_error(err)
# Don't return an error by return_error() as this is transformer.
raise DemistoException(str(err))

return_results(value)

Expand Down
4 changes: 3 additions & 1 deletion Packs/MapPattern/Scripts/MapPattern/MapPattern.yml
Expand Up @@ -76,10 +76,12 @@ args:
- name: wildcards
description: The list of the special patterns which match to any values regardless
of algorithm.
- name: default_value
description: The value to return when all the patterns are not satisfied.
scripttarget: 0
subtype: python3
runonce: false
dockerimage: demisto/python3:3.9.8.24399
dockerimage: demisto/python3:3.10.5.31928
runas: DBotWeakRole
fromversion: 6.0.0
tests:
Expand Down
1 change: 1 addition & 0 deletions Packs/MapPattern/Scripts/MapPattern/MapPattern_test.py
Expand Up @@ -78,6 +78,7 @@ def test_main(mocker):
'compare_fields': t['compare_fields'],
'wildcards': t['wildcards'],
'mappings': t['mappings'],
'default_value': t.get('default_value'),
})
mocker.patch.object(demisto, 'results')
main()
Expand Down
1 change: 1 addition & 0 deletions Packs/MapPattern/Scripts/MapPattern/README.md
Expand Up @@ -41,6 +41,7 @@ When unmatched or the input value is structured (dict or list), it will simply r
| flags | The comma separated flags for pattern matching in regex. `dotall` (s), `multiline` (m), `ignorecase` (i) and `unicode` (u) are supported. This will apply to all the algorithms. |
| compare_fields | Set to true if you want pattern matching for each field, otherwise false. |
| wildcards | The list of the special patterns which match to any values regardless of algorithm. |
| default_value | The value to return when all the patterns are not satisfied. |

## Outputs
---
Expand Down
28 changes: 28 additions & 0 deletions Packs/MapPattern/Scripts/MapPattern/test_data/test-1.json
Expand Up @@ -26,6 +26,34 @@
}
]
},
{
"algorithm": "regmatch",
"caseless": "true",
"priority": "first_match",
"context": null,
"flags": "",
"compare_fields": "",
"wildcards": "",
"default_value": "default",
"mappings": {
"Unknown": 0,
"Informational|Info": 0.5,
"Low": 1,
"Medium": 2,
"High": 3,
"Critical": 4
},
"patterns": [
{
"value": "High",
"result": 3
},
{
"value": "Abc",
"result": "default"
}
]
},
{
"algorithm": "regmatch",
"caseless": "true",
Expand Down
2 changes: 1 addition & 1 deletion Packs/MapPattern/pack_metadata.json
Expand Up @@ -2,7 +2,7 @@
"name": "MapPattern",
"description": "This transformer will take in a value and transform it based on multiple condition expressions (wildcard, regex, etc) defined in a JSON dictionary structure.",
"support": "community",
"currentVersion": "1.0.5",
"currentVersion": "1.0.6",
"author": "Masahiko Inoue",
"url": "",
"email": "",
Expand Down