Skip to content

Commit

Permalink
Add ignorecase filter function (#125)
Browse files Browse the repository at this point in the history
* add ignorecase function

* Update CHANGELOG.md
  • Loading branch information
oscarbc96 committed Sep 16, 2020
1 parent 2b51cfb commit 14e56b6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog
All notable changes to this project will be documented in this file.

## [0.19.2] - 2020-09-16
### Improvements
- Add `regex:ignorecase` filter function

## [0.19.1] - 2020-09-01
### Improvements
- Add support for this new S3 url format: `https://bucket.s3.aws-region.amazonaws.com/path1/path2`
Expand Down
2 changes: 1 addition & 1 deletion cfripper/__version__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
VERSION = (0, 19, 1)
VERSION = (0, 19, 2)

__version__ = ".".join(map(str, VERSION))
1 change: 1 addition & 0 deletions cfripper/config/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def wrap(*args, **kwargs):
"and": lambda *args, **kwargs: all(arg(kwargs) for arg in args),
"in": param_resolver(lambda a, b, **kwargs: a in b),
"regex": param_resolver(lambda *args, **kwargs: bool(re.match(*args))),
"regex:ignorecase": param_resolver(lambda *args, **kwargs: bool(re.match(*args, re.IGNORECASE))),
"exists": param_resolver(lambda a, **kwargs: a is not None),
"empty": param_resolver(lambda *args, **kwargs: len(args) == 0),
"ref": param_resolver(lambda param_name, **kwargs: get(kwargs, param_name)),
Expand Down
33 changes: 17 additions & 16 deletions docs/rule_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,23 @@ Rule Standard -> Rule Config -> Filter #1 -> ... -> Filter #N


### Implemented filter functions
| Function | Description | Example |
|:----------:|:---------------------------------------------------------------------------:|:---------------------------------------:|
| `eq` | Same as a == b | `{"eq": ["string", "string"]}` |
| `ne` | Same as a != b | `{"ne": ["string", "not_that_string"]}` |
| `lt` | Same as a < b | `{"lt": [0, 1]}` |
| `gt` | Same as a > b | `{"gt": [1, 0]}` |
| `le` | Same as a <= b | `{"le": [1, 1]}` |
| `ge` | Same as a >= b | `{"ge": [1, 1]}` |
| `not` | Same as not a | `{"not": True}` |
| `or` | True if any arg is True | `{"or": [False, True]}` |
| `and` | True if all args are True | `{"and": [True, True]}` |
| `in` | Same as a in b | `{"in": ["b", ["a", "b"]]}` |
| `regex` | True if b match pattern a | `{"regex": [r"^\d+$", "5"]}` |
| `exists` | True if a is not None | `{"exists": None}` |
| `empty` | True if len(a) equals 0 | `{"empty": []}` |
| `ref` | Get the value at any depth of the context based on the path described by a. | `{"ref": "param_a.param_b"}` |
| Function | Description | Example |
|:-------------------:|:---------------------------------------------------------------------------:|:---------------------------------------:|
| `eq` | Same as a == b | `{"eq": ["string", "string"]}` |
| `ne` | Same as a != b | `{"ne": ["string", "not_that_string"]}` |
| `lt` | Same as a < b | `{"lt": [0, 1]}` |
| `gt` | Same as a > b | `{"gt": [1, 0]}` |
| `le` | Same as a <= b | `{"le": [1, 1]}` |
| `ge` | Same as a >= b | `{"ge": [1, 1]}` |
| `not` | Same as not a | `{"not": True}` |
| `or` | True if any arg is True | `{"or": [False, True]}` |
| `and` | True if all args are True | `{"and": [True, True]}` |
| `in` | Same as a in b | `{"in": ["b", ["a", "b"]]}` |
| `regex` | True if b match pattern a (case sensitive) | `{"regex": [r"^\d+$", "5"]}` |
| `regex:ignorecase` | True if b match pattern a (case insensitive) | `{"regex:ignorecase": [r"^AA$", "aa"]}` |
| `exists` | True if a is not None | `{"exists": None}` |
| `empty` | True if len(a) equals 0 | `{"empty": []}` |
| `ref` | Get the value at any depth of the context based on the path described by a. | `{"ref": "param_a.param_b"}` |

### Examples

Expand Down
17 changes: 17 additions & 0 deletions tests/config/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,23 @@ def template_cross_account_role_with_name():
(Filter(eval={"regex": [r"\w*\d\s*\d\s*\d\w*", "xx123xx"]}), {}, True),
(Filter(eval={"regex": [r"^b\w+", "foobar"]}), {}, False),
(Filter(eval={"regex": [r"\w*b\w+", "foobar"]}), {}, True),
(Filter(eval={"regex": [r"III", "III"]}), {}, True),
(Filter(eval={"regex": [r"iii", "iii"]}), {}, True),
(Filter(eval={"regex": [r"iIi", "iii"]}), {}, False),
(Filter(eval={"regex": [r"iIi", "III"]}), {}, False),
(Filter(eval={"regex:ignorecase": [r"^\d+$", "5"]}), {}, True),
(Filter(eval={"regex:ignorecase": [r"pi+", "piiig"]}), {}, True),
(Filter(eval={"regex:ignorecase": [r"iii", "piiig"]}), {}, False),
(Filter(eval={"regex:ignorecase": [r"igs", "piiig"]}), {}, False),
(Filter(eval={"regex:ignorecase": [r"\w*\d\s*\d\s*\d\w*", "xx1 2 3xx"]}), {}, True),
(Filter(eval={"regex:ignorecase": [r"\w*\d\s*\d\s*\d\w*", "xx12 3xx"]}), {}, True),
(Filter(eval={"regex:ignorecase": [r"\w*\d\s*\d\s*\d\w*", "xx123xx"]}), {}, True),
(Filter(eval={"regex:ignorecase": [r"^b\w+", "foobar"]}), {}, False),
(Filter(eval={"regex:ignorecase": [r"\w*b\w+", "foobar"]}), {}, True),
(Filter(eval={"regex:ignorecase": [r"III", "III"]}), {}, True),
(Filter(eval={"regex:ignorecase": [r"iii", "iii"]}), {}, True),
(Filter(eval={"regex:ignorecase": [r"iIi", "iii"]}), {}, True),
(Filter(eval={"regex:ignorecase": [r"iIi", "III"]}), {}, True),
(Filter(eval={"exists": None}), {}, False),
(Filter(eval={"exists": "string"}), {}, True),
(Filter(eval={"exists": 1}), {}, True),
Expand Down

0 comments on commit 14e56b6

Please sign in to comment.