8
8
from abc import ABC , abstractmethod
9
9
from collections import deque
10
10
from dataclasses import InitVar , dataclass , field , fields
11
- from typing import Any , Deque , Dict , Iterator , List , Mapping , Sequence , Set , Tuple
11
+ from typing import Any , Deque , Dict , Iterator , List , Sequence , Set , Tuple
12
12
13
+ from cfnlint .context ._conditions import Conditions
13
14
from cfnlint .helpers import (
14
15
BOOLEAN_STRINGS_TRUE ,
15
16
FUNCTIONS ,
@@ -136,7 +137,7 @@ class Context:
136
137
# cfn-lint Template class
137
138
parameters : Dict [str , "Parameter" ] = field (init = True , default_factory = dict )
138
139
resources : Dict [str , "Resource" ] = field (init = True , default_factory = dict )
139
- conditions : Dict [ str , "Condition" ] = field (init = True , default_factory = dict )
140
+ conditions : Conditions = field (init = True , default_factory = Conditions )
140
141
mappings : Dict [str , "Map" ] = field (init = True , default_factory = dict )
141
142
142
143
strict_types : bool = field (init = True , default = True )
@@ -149,9 +150,6 @@ class Context:
149
150
# and adds in any Refs available from things like Fn::Sub
150
151
ref_values : Dict [str , Any ] = field (init = True , default_factory = dict )
151
152
152
- # Resolved conditions for reference
153
- resolved_conditions : Mapping [str , bool ] = field (init = True , default_factory = dict )
154
-
155
153
transforms : Transforms = field (init = True , default_factory = lambda : Transforms ([]))
156
154
157
155
def evolve (self , ** kwargs ) -> "Context" :
@@ -187,6 +185,10 @@ def ref_value(self, instance: str) -> Iterator[Tuple[str | List[str], "Context"]
187
185
return
188
186
if instance in self .parameters :
189
187
for v , path in self .parameters [instance ].ref (self ):
188
+
189
+ # validate that ref is possible with path
190
+ # need to evaluate if Fn::If would be not true if value is
191
+ # what it is
190
192
yield v , self .evolve (
191
193
path = self .path .evolve (
192
194
value_path = deque (["Parameters" , instance ]) + path
@@ -266,11 +268,6 @@ def ref(self, context: Context) -> Iterator[Any]:
266
268
pass
267
269
268
270
269
- @dataclass
270
- class Condition :
271
- instance : Any = field (init = True )
272
-
273
-
274
271
@dataclass
275
272
class Parameter (_Ref ):
276
273
"""
@@ -448,19 +445,6 @@ def _init_transforms(transforms: Any) -> Transforms:
448
445
return Transforms ([])
449
446
450
447
451
- def _init_conditions (conditions : Any ) -> dict [str , Condition ]:
452
- obj = {}
453
- if not isinstance (conditions , dict ):
454
- raise ValueError ("Conditions must be a object" )
455
- for k , v in conditions .items ():
456
- try :
457
- obj [k ] = Condition (v )
458
- except ValueError :
459
- pass
460
-
461
- return obj
462
-
463
-
464
448
def _init_mappings (mappings : Any ) -> dict [str , Map ]:
465
449
obj = {}
466
450
if not isinstance (mappings , dict ):
@@ -489,9 +473,9 @@ def create_context_for_template(cfn):
489
473
490
474
transforms = _init_transforms (cfn .template .get ("Transform" , []))
491
475
492
- conditions = {}
476
+ conditions = Conditions ({})
493
477
try :
494
- conditions = _init_conditions (cfn .template .get ("Conditions" , {}))
478
+ conditions = Conditions . create_from_instance (cfn .template .get ("Conditions" , {}))
495
479
except (ValueError , AttributeError ):
496
480
pass
497
481
0 commit comments