Skip to content

Commit 405a0c9

Browse files
authored
Create some performance benefits (aws-cloudformation#1816)
1 parent b04bd09 commit 405a0c9

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

src/cfnlint/helpers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ def __contains__(self, item):
216216
return True
217217
return False
218218

219+
def get(self, key, default=None):
220+
try:
221+
return self[key]
222+
except KeyError:
223+
return default
224+
219225
def get_metadata_filename(url):
220226
"""Returns the filename for a metadata file associated with a remote resource"""
221227
caching_dir = os.path.join(os.path.dirname(__file__), 'data', 'DownloadsMetadata')

src/cfnlint/rules/conditions/Equals.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Equals(CloudFormationLintRule):
2020
'Fn::Sub', 'Fn::Join', 'Fn::Select', 'Fn::Split']
2121
function = 'Fn::Equals'
2222

23-
def _check_equal_values(self, cfn, element, path):
23+
def _check_equal_values(self, element, path, valid_refs):
2424
matches = []
2525

2626
if len(element) == 1:
@@ -33,10 +33,10 @@ def _check_equal_values(self, cfn, element, path):
3333
message.format(', '.join(self.allowed_functions))
3434
))
3535
elif element_key == 'Ref':
36-
valid_refs = cfn.get_valid_refs()
37-
if element_value in valid_refs:
38-
if valid_refs.get(element_value).get('From') == 'Parameters':
39-
if valid_refs.get(element_value).get('Type') in VALID_PARAMETER_TYPES_LIST:
36+
valid_ref = valid_refs.get(element_value)
37+
if valid_ref:
38+
if valid_ref.get('From') == 'Parameters':
39+
if valid_ref.get('Type') in VALID_PARAMETER_TYPES_LIST:
4040
message = 'Every Fn::Equals object requires a list of 2 string parameters'
4141
matches.append(RuleMatch(
4242
path, message))
@@ -53,6 +53,7 @@ def match(self, cfn):
5353
# Build the list of functions
5454
trees = cfn.search_deep_keys(self.function)
5555

56+
valid_refs = cfn.get_valid_refs()
5657
for tree in trees:
5758
# Test when in Conditions
5859
if tree[0] == 'Conditions':
@@ -73,7 +74,7 @@ def match(self, cfn):
7374
for index, element in enumerate(value):
7475
if isinstance(element, dict):
7576
matches.extend(self._check_equal_values(
76-
cfn, element, tree[:-1] + [index]))
77+
element, tree[:-1] + [index], valid_refs))
7778
elif not isinstance(element, (six.string_types, bool, six.integer_types, float)):
7879
message = self.function + \
7980
' element must be a String, Boolean, Number, or supported function ({0})'

test/unit/module/helpers/test_regex_dict.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,24 @@
88
class TestRegexDict(BaseTestCase):
99
"""Test Regex Dict """
1010

11-
def test_success_run(self):
11+
def test_getitem(self):
1212
"""Test success run"""
1313

1414
obj = RegexDict()
1515
obj['^Value$'] = True
1616

1717
with self.assertRaises(KeyError):
1818
obj['NotExist']
19+
20+
def test_get(self):
21+
obj = RegexDict()
22+
obj['^Value$'] = True
23+
24+
self.assertEqual(obj.get('NotExist', 'Default'), 'Default')
25+
26+
def test_return_longest(self):
27+
obj = RegexDict()
28+
obj['^Test'] = False
29+
obj['^TestLonger'] = True
30+
31+
self.assertTrue(obj['TestLongerObject'])

0 commit comments

Comments
 (0)