Source code for codegrade.models.parse_error_leaf
"""The module that defines the ``ParseErrorLeaf`` model.
SPDX-License-Identifier: AGPL-3.0-only OR BSD-3-Clause-Clear
"""
from __future__ import annotations
import typing as t
from dataclasses import dataclass, field
import cg_request_args as rqa
from cg_maybe import Maybe, Nothing
from cg_maybe.utils import maybe_from_nullable
from .. import parsers
from ..utils import to_dict
from .parse_error_found import ParseErrorFound
[docs]
@dataclass
class ParseErrorLeaf:
"""JSON shape of `Leaf`."""
#: Discriminator tag.
kind: t.Literal["leaf"]
#: Description of what the parser expected.
expected: str
#: Information about the value that was actually received.
found: ParseErrorFound
#: Optional extra context attached by the parser. Omitted when absent.
extra: Maybe[t.Mapping[str, str]] = Nothing
raw_data: t.Optional[t.Dict[str, t.Any]] = field(init=False, repr=False)
data_parser: t.ClassVar[t.Any] = rqa.Lazy(
lambda: rqa.FixedMapping(
rqa.RequiredArgument(
"kind",
rqa.StringEnum("leaf"),
doc="Discriminator tag.",
),
rqa.RequiredArgument(
"expected",
rqa.SimpleValue.str,
doc="Description of what the parser expected.",
),
rqa.RequiredArgument(
"found",
parsers.ParserFor.make(ParseErrorFound),
doc="Information about the value that was actually received.",
),
rqa.OptionalArgument(
"extra",
rqa.LookupMapping(rqa.SimpleValue.str),
doc="Optional extra context attached by the parser. Omitted when absent.",
),
)
)
def __post_init__(self) -> None:
getattr(super(), "__post_init__", lambda: None)()
self.extra = maybe_from_nullable(self.extra)
def to_dict(self) -> t.Dict[str, t.Any]:
res: t.Dict[str, t.Any] = {
"kind": to_dict(self.kind),
"expected": to_dict(self.expected),
"found": to_dict(self.found),
}
if self.extra.is_just:
res["extra"] = to_dict(self.extra.value)
return res
@classmethod
def from_dict(
cls: t.Type[ParseErrorLeaf], d: t.Dict[str, t.Any]
) -> ParseErrorLeaf:
parsed = cls.data_parser.try_parse(d)
res = cls(
kind=parsed.kind,
expected=parsed.expected,
found=parsed.found,
extra=parsed.extra,
)
res.raw_data = d
return res