@@ -104,6 +104,7 @@ def transform(self, cfn: Any) -> TransformResult:
104
104
"""Transform the template"""
105
105
return [], self ._walk (cfn .template , {}, cfn )
106
106
107
+ # pylint: disable=too-many-return-statements
107
108
def _walk (self , item : Any , params : MutableMapping [str , Any ], cfn : Any ):
108
109
obj = deepcopy (item )
109
110
if isinstance (obj , dict ):
@@ -134,6 +135,9 @@ def _walk(self, item: Any, params: MutableMapping[str, Any], cfn: Any):
134
135
f_k ,
135
136
)
136
137
del obj [k ]
138
+ elif k == "Fn::ToJsonString" :
139
+ # extra special handing for this as {} could be a valid value
140
+ return obj
137
141
elif k == "Fn::Sub" :
138
142
if isinstance (v , str ):
139
143
only_string , obj [k ] = self ._replace_string_params (v , params )
@@ -150,12 +154,15 @@ def _walk(self, item: Any, params: MutableMapping[str, Any], cfn: Any):
150
154
try :
151
155
mapping = _ForEachValueFnFindInMap (get_hash (v ), v )
152
156
map_value = mapping .value (cfn , params , True )
157
+ # if we get None this means its all strings but couldn't be resolved
158
+ # we will pass this forward
153
159
if map_value is None :
154
- del obj [k ]
155
160
continue
156
- if isinstance (map_value , str ):
161
+ # if we can resolve it we will return it
162
+ if isinstance (map_value , _SCALAR_TYPES ):
157
163
return map_value
158
164
except Exception as e : # pylint: disable=broad-exception-caught
165
+ # We couldn't resolve the FindInMap so we are going to leave it as it is
159
166
LOGGER .debug ("Transform and Fn::FindInMap error: %s" , {str (e )})
160
167
elif k == "Ref" :
161
168
if isinstance (v , str ):
@@ -169,7 +176,10 @@ def _walk(self, item: Any, params: MutableMapping[str, Any], cfn: Any):
169
176
obj [k ] = r
170
177
else :
171
178
sub_value = self ._walk (v , params , cfn )
172
- if sub_value is None :
179
+ # a sub object may be none or we have returned
180
+ # an empty object. We don't want to remove empty
181
+ # strings "" or 0 (zeros)
182
+ if sub_value is None or sub_value == {}:
173
183
del obj [k ]
174
184
else :
175
185
obj [k ] = self ._walk (v , params , cfn )
0 commit comments