|
| 1 | +"""Spider TDL types.""" |
| 2 | + |
| 3 | +from abc import ABC, abstractmethod |
| 4 | + |
| 5 | +from typing_extensions import override |
| 6 | + |
| 7 | + |
| 8 | +class TdlType(ABC): |
| 9 | + """Abstract base class for all TDL types.""" |
| 10 | + |
| 11 | + @abstractmethod |
| 12 | + def type_str(self) -> str: |
| 13 | + """:return: String representation of the TDL type.""" |
| 14 | + |
| 15 | + |
| 16 | +class DoubleType(TdlType): |
| 17 | + """TDL double type.""" |
| 18 | + |
| 19 | + @override |
| 20 | + def type_str(self) -> str: |
| 21 | + return "double" |
| 22 | + |
| 23 | + |
| 24 | +class FloatType(TdlType): |
| 25 | + """TDL float type.""" |
| 26 | + |
| 27 | + @override |
| 28 | + def type_str(self) -> str: |
| 29 | + return "float" |
| 30 | + |
| 31 | + |
| 32 | +class Int8Type(TdlType): |
| 33 | + """TDL int8 type.""" |
| 34 | + |
| 35 | + @override |
| 36 | + def type_str(self) -> str: |
| 37 | + return "int8" |
| 38 | + |
| 39 | + |
| 40 | +class Int16Type(TdlType): |
| 41 | + """TDL int16 type.""" |
| 42 | + |
| 43 | + @override |
| 44 | + def type_str(self) -> str: |
| 45 | + return "int16" |
| 46 | + |
| 47 | + |
| 48 | +class Int32Type(TdlType): |
| 49 | + """TDL int32 type.""" |
| 50 | + |
| 51 | + @override |
| 52 | + def type_str(self) -> str: |
| 53 | + return "int32" |
| 54 | + |
| 55 | + |
| 56 | +class Int64Type(TdlType): |
| 57 | + """TDL int64 type.""" |
| 58 | + |
| 59 | + @override |
| 60 | + def type_str(self) -> str: |
| 61 | + return "int64" |
| 62 | + |
| 63 | + |
| 64 | +class BoolType(TdlType): |
| 65 | + """TDL bool type.""" |
| 66 | + |
| 67 | + @override |
| 68 | + def type_str(self) -> str: |
| 69 | + return "bool" |
| 70 | + |
| 71 | + |
| 72 | +class ClassType(TdlType): |
| 73 | + """TDL Custom class type.""" |
| 74 | + |
| 75 | + def __init__(self, name: str) -> None: |
| 76 | + """ |
| 77 | + Creates a TDL custom class type. |
| 78 | + :param name: The name of the class. |
| 79 | + """ |
| 80 | + self._name = name |
| 81 | + |
| 82 | + @override |
| 83 | + def type_str(self) -> str: |
| 84 | + return self._name |
| 85 | + |
| 86 | + |
| 87 | +class ListType(TdlType): |
| 88 | + """TDL List type.""" |
| 89 | + |
| 90 | + def __init__(self, element_type: TdlType) -> None: |
| 91 | + """ |
| 92 | + Creates a TDL list type. |
| 93 | + :param element_type: |
| 94 | + """ |
| 95 | + self.element_type = element_type |
| 96 | + |
| 97 | + @override |
| 98 | + def type_str(self) -> str: |
| 99 | + return f"List<{self.element_type.type_str()}>" |
| 100 | + |
| 101 | + |
| 102 | +def is_integral(tdl_type: TdlType) -> bool: |
| 103 | + """ |
| 104 | + :param tdl_type: |
| 105 | + :return: Whether `tdl_type` is a TDL integral type. |
| 106 | + """ |
| 107 | + return isinstance(tdl_type, (Int8Type, Int16Type, Int32Type, Int64Type)) |
| 108 | + |
| 109 | + |
| 110 | +def is_string(tdl_type: TdlType) -> bool: |
| 111 | + """ |
| 112 | + :param tdl_type: |
| 113 | + :return: Whether `tdl_type` is a TDL string type, i.e. `List<int8>`. |
| 114 | + """ |
| 115 | + return isinstance(tdl_type, ListType) and isinstance(tdl_type.element_type, Int8Type) |
| 116 | + |
| 117 | + |
| 118 | +def is_map_key(tdl_type: TdlType) -> bool: |
| 119 | + """ |
| 120 | + :param tdl_type: |
| 121 | + :return: Whether `tdl_type` is a supported key type of a map. |
| 122 | + """ |
| 123 | + return is_integral(tdl_type) or is_string(tdl_type) |
| 124 | + |
| 125 | + |
| 126 | +class MapType(TdlType): |
| 127 | + """TDL Map type.""" |
| 128 | + |
| 129 | + def __init__(self, key_type: TdlType, value_type: TdlType) -> None: |
| 130 | + """ |
| 131 | + Creates a TDL map type. |
| 132 | + :param key_type: |
| 133 | + :param value_type: |
| 134 | + :raises TypeError: If key is not a supported type. |
| 135 | + """ |
| 136 | + if not is_map_key(key_type): |
| 137 | + msg = f"{key_type} is not a supported type for map key." |
| 138 | + raise TypeError(msg) |
| 139 | + self.key_type = key_type |
| 140 | + self.value_type = value_type |
| 141 | + |
| 142 | + @override |
| 143 | + def type_str(self) -> str: |
| 144 | + return f"Map<{self.key_type.type_str()},{self.value_type.type_str()}>" |
0 commit comments