File size: 2,859 Bytes
b40c49c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""

from __future__ import annotations

import logging
from dataclasses import dataclass, field
from typing import Any, Iterator

LOGGER = logging.getLogger(__name__)


@dataclass(frozen=True)
class Mappings:
    """
    This class holds a mapping
    """

    maps: dict[str, Map] = field(init=True, default_factory=dict)
    is_transform: bool = field(init=True, default=False)

    @classmethod
    def create_from_dict(cls, instance: Any) -> Mappings:

        if not isinstance(instance, dict):
            return cls({})
        try:
            result = {}
            is_transform = False
            for k, v in instance.items():
                if k == "Fn::Transform":
                    is_transform = True
                elif isinstance(k, str):
                    result[k] = Map.create_from_dict(v)
            return cls(result, is_transform)
        except (ValueError, AttributeError) as e:
            LOGGER.debug(e, exc_info=True)
            return cls({})


@dataclass(frozen=True)
class _MappingSecondaryKey:
    """
    This class holds a mapping value
    """

    keys: dict[str, list[Any] | str | int | float] = field(
        init=True, default_factory=dict
    )
    is_transform: bool = field(init=True, default=False)

    def value(self, secondary_key: str):
        if secondary_key not in self.keys:
            raise KeyError(secondary_key)
        return self.keys[secondary_key]

    @classmethod
    def create_from_dict(cls, instance: Any) -> _MappingSecondaryKey:
        if not isinstance(instance, dict):
            return cls({})
        is_transform = False
        keys = {}
        for k, v in instance.items():
            if k == "Fn::Transform":
                is_transform = True
            elif isinstance(k, str) and isinstance(v, (str, list, int, float)):
                keys[k] = v
        return cls(keys, is_transform)


@dataclass(frozen=True)
class Map:
    """
    This class holds a mapping
    """

    keys: dict[str, _MappingSecondaryKey] = field(init=True, default_factory=dict)
    is_transform: bool = field(init=True, default=False)

    def find_in_map(self, top_key: str, secondary_key: str) -> Iterator[Any]:
        if top_key not in self.keys:
            raise KeyError(top_key)
        yield self.keys[top_key].value(secondary_key)

    @classmethod
    def create_from_dict(cls, instance: Any) -> Map:
        if not isinstance(instance, dict):
            return cls({})
        is_transform = False
        keys = {}
        for k, v in instance.items():
            if k == "Fn::Transform":
                is_transform = True
            elif isinstance(k, str):
                keys[k] = _MappingSecondaryKey.create_from_dict(v)
        return cls(keys, is_transform)