Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 48 additions & 14 deletions commanderbot/ext/mcdoc/mcdoc_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ def render(self, ctx):
return typeDef.render(ctx)
return super().render(ctx)


@dataclass
class IndexedType(McdocBaseType):
child: "McdocType"
parallelIndices: list[DynamicIndex | StaticIndex]

def suffix(self, ctx):
result = self.child.suffix(ctx)
result += f"[{','.join(i.render() for i in self.parallelIndices)}]"
return result


@dataclass
class StructTypePairField:
key: Union[str, "McdocType"]
Expand Down Expand Up @@ -758,8 +770,17 @@ def suffix(self, ctx):
return f"a tuple of length {len(self.items)}"


@dataclass
class UnknownType(McdocBaseType):
kind: str

def suffix(self, ctx):
return f"UNKNOWN (`{self.kind}`)"


McdocType = Union[
DispatcherType,
IndexedType,
EnumType,
ListType,
LiteralType,
Expand All @@ -782,6 +803,7 @@ def suffix(self, ctx):
UnionType,
TemplateType,
ConcreteType,
UnknownType,
]


Expand All @@ -794,6 +816,22 @@ def deserialize_attributes(data: dict) -> list[Attribute]:
return result


def deserialize_parallel_indices(data: dict) -> list[DynamicIndex | StaticIndex]:
result: list[DynamicIndex | StaticIndex] = []
for index in data.get("parallelIndices", []):
if index["kind"] == "dynamic":
accessor: list[str | KeywordIndex] = []
for part in index["accessor"]:
if isinstance(part, str):
accessor.append(part)
else:
accessor.append(KeywordIndex(part["keyword"]))
result.append(DynamicIndex(accessor=accessor))
elif index["kind"] == "static":
result.append(StaticIndex(value=index["value"]))
return result


def deserialize_numeric_range(data: Optional[dict]) -> Optional[NumericRange]:
if data is None:
return None
Expand All @@ -810,21 +848,15 @@ def deserialize_mcdoc(data: dict) -> McdocType:
kind = data.get("kind")

if kind == "dispatcher":
parallelIndices: list[DynamicIndex | StaticIndex] = []
for index in data.get("parallelIndices", []):
if index["kind"] == "dynamic":
accessor: list[str | KeywordIndex] = []
for part in index["accessor"]:
if isinstance(part, str):
accessor.append(part)
else:
accessor.append(KeywordIndex(part["keyword"]))
parallelIndices.append(DynamicIndex(accessor=accessor))
elif index["kind"] == "static":
parallelIndices.append(StaticIndex(value=index["value"]))
return DispatcherType(
registry=data.get("registry", ""),
parallelIndices=parallelIndices,
parallelIndices=deserialize_parallel_indices(data),
attributes=deserialize_attributes(data),
)
if kind == "indexed":
return IndexedType(
child=deserialize_mcdoc(data["child"]),
parallelIndices=deserialize_parallel_indices(data),
attributes=deserialize_attributes(data),
)
if kind == "struct":
Expand Down Expand Up @@ -960,4 +992,6 @@ def deserialize_mcdoc(data: dict) -> McdocType:
typeArgs=[deserialize_mcdoc(t) for t in data["typeArgs"]],
attributes=deserialize_attributes(data),
)
raise ValueError(f"Unknown kind: {kind}")
return UnknownType(
kind=f"{kind}"
)