def intrinsic_property_resolver()

in samcli/lib/intrinsic_resolver/intrinsic_property_resolver.py [0:0]


    def intrinsic_property_resolver(self, intrinsic, ignore_errors, parent_function="template"):
        """
        This resolves the intrinsic of the format
        {
            intrinsic: dict
        } by calling the function with the relevant intrinsic function resolver.

        This also supports returning a string, list, boolean, int since they may be intermediate steps in the recursion
        process. No transformations are done on these.

        By default this will just return the item if non of the types match. This is because of the function
        resolve_all_attributes which will recreate the resources by processing every aspect of resource.

        This code resolves in a top down depth first fashion in order to create a functional style recursion that
        doesn't mutate any of the properties.

        Parameters
        ----------
        intrinsic : dict, str, list, bool, int
            This is an intrinsic property or an intermediate step
        ignore_errors : bool
            Whether to ignore errors
        parent_function : str
            In case there is a missing property, this is used to figure out where the property resolved is missing.
        Return
        ---------
        The simplified version of the intrinsic function. This could be a list,str,dict depending on the format required
        """
        if intrinsic is None:
            raise InvalidIntrinsicException("Missing Intrinsic property in {}".format(parent_function))
        if isinstance(intrinsic, list):
            return [self.intrinsic_property_resolver(item, ignore_errors) for item in intrinsic]
        if not isinstance(intrinsic, dict) or intrinsic == {}:
            return intrinsic

        # `intrinsic` is a dict at this point.

        keys = list(intrinsic.keys())
        key = keys[0]

        if key in self.intrinsic_key_function_map:
            intrinsic_value = intrinsic.get(key)
            return self.intrinsic_key_function_map.get(key)(intrinsic_value, ignore_errors)

        if key in self.conditional_key_function_map:
            intrinsic_value = intrinsic.get(key)
            return self.conditional_key_function_map.get(key)(intrinsic_value, ignore_errors)

        # In this case, it is a dictionary that doesn't directly contain an intrinsic resolver, we must recursively
        # resolve each of it's sub properties.
        sanitized_dict = {}
        for key, val in intrinsic.items():
            try:
                sanitized_key = self.intrinsic_property_resolver(key, ignore_errors, parent_function=parent_function)
                sanitized_val = self.intrinsic_property_resolver(val, ignore_errors, parent_function=parent_function)
                verify_intrinsic_type_str(
                    sanitized_key,
                    message="The keys of the dictionary {} in {} must all resolve to a string".format(
                        sanitized_key, parent_function
                    ),
                )
                sanitized_dict[sanitized_key] = sanitized_val
            # On any exception, leave the key:val of the orginal intact and continue on.
            # https://github.com/awslabs/aws-sam-cli/issues/1386
            except Exception:
                if ignore_errors:
                    LOG.debug("Unable to resolve property %s: %s. Leaving as is.", key, val)
                    sanitized_dict[key] = val
                else:
                    raise

        return sanitized_dict