File size: 1,855 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
"""
Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""

from __future__ import annotations

from typing import Any, Dict

import jsonpatch

from cfnlint.schema._getatts import AttributeDict, GetAtts
from cfnlint.schema.resolver import RefResolver

# Can't use a dataclass because its hard to parse in json
# with optional fields without addtional help


class Schema:
    def __init__(self, schema: dict[str, Any], is_cached: bool = False) -> None:
        self.is_cached: bool = is_cached
        self._schema: dict[str, Any] = schema
        self._type_name: str = schema["typeName"]
        self.resolver: RefResolver = RefResolver.from_schema(schema)
        self._getatts: GetAtts = GetAtts(self)

    @property
    def type_name(self) -> str:
        """Return the type name of the schema"""
        return self._type_name

    @property
    def schema(self) -> Dict:
        """Return the JSON Schema version of the CFN Schema

        Args:
        Returns:
            Dict: the JSON schema representation of the CFN schema
        """
        return self._schema

    def patch(self, patches: list[Dict]) -> None:
        """Patches the schema file

        Args:
            patches: A list of JSON Patches
        Returns:
            None: Returns when the patches have been applied
        """
        jsonpatch.JsonPatch(patches).apply(self.schema, in_place=True)

    @property
    def get_atts(self) -> AttributeDict:
        """Get the valid GetAtts for this schema. Schemas are defined in property
            readOnlyProperties and we need to build definitions of those properties

        Args:
        Returns:
            dict[str, GetAtt]: Dict of keys with valid strings and the json schema
            object for the property
        """
        return self._getatts.attrs