The members of enum, encoded as their values (Effect's Enum,
whose values are the members). Decoding takes a value (or a member) and
gives the member; encoding gives the member's value. Values must be
strings or finite numbers.
side says what the node does: "decode" (value to member),
"encode" (member to value), "type" (members) or "encoded"
(values). Checks apply to members.
Attributes#
| Attribute | Type | Description |
|---|---|---|
enum | type[enum.Enum] | |
side | EnumSide = 'decode' | |
tag | = 'Enum' | |
members | tuple[enum.Enum, ...] | |
parse | = parse_by_interpreting |
Functions#
__init__#
__init__(enum, side='decode', *, annotations=None, checks=None, key_annotations=None, encoding=None, encoding_checks=None, optional_key=False, constructor_default=None) -> None| Parameter | Type | Description |
|---|---|---|
enum | type[enum.Enum] | |
side | EnumSide = 'decode' | |
annotations | Annotations | None = None | |
checks | Checks | None = None | |
key_annotations | Annotations | None = None | |
encoding | Encoding | None = None | |
encoding_checks | Checks | None = None | |
optional_key | bool = False | |
constructor_default | Effect[object, Issue] | None = None |
get_expected#
get_expected() -> strReturns str
def get_expected(self) -> str:
if self.side in ("type", "encode"):
return self.enum.__name__
return " | ".join(format_literal(m.value) for m in self.enum)interpret#
interpret(input, options) -> ParseResult| Parameter | Type | Description |
|---|---|---|
input | object | |
options | ParseOptions |
Returns ParseResult
def interpret(self, input: object, options: ParseOptions) -> ParseResult:
if self.encoding is not None:
return AST.interpret(self, input, options)
return self.parse_local(input, options)parse_local#
parse_local(input, options) -> ParseResult| Parameter | Type | Description |
|---|---|---|
input | object | |
options | ParseOptions |
Returns ParseResult
def parse_local(self, input: object, options: ParseOptions) -> ParseResult:
if self.side != "encode":
return AST.parse_local(self, input, options)
# Checks describe members: run them on the member, then encode it.
return map_value(
replace(self, side="type").parse_local(input, options),
lambda member: cast("enum.Enum", member).value,
)is_opaque#
is_opaque() -> boolReturns bool
def is_opaque(self) -> bool:
return self.side in ("decode", "encode")owner#
owner() -> ASTThe node whose body this one shares (Effect's getContextOwner): a copy with
another key context, or a flip's encoded side, which has its own encoding. Its decoded
side and its flip are the owner's.
Returns AST
def owner(self) -> AST:
"""The node whose body this one shares (Effect's ``getContextOwner``): a copy with
another key context, or a flip's encoded side, which has its own encoding. Its decoded
side and its flip are the owner's."""
return self.context_owner or selfkey_context_owner#
key_context_owner() -> ASTThe node this one is with only its key context changed, encoding included, else
itself: copies that are one schema to derivations and JSON Schema definitions, each
in its own key context (Effect's replaceContext test of getContextOwner).
Returns AST
def key_context_owner(self) -> AST:
"""The node this one is with only its key context changed, encoding included, else
itself: copies that are one schema to derivations and JSON Schema definitions, each
in its own key context (Effect's ``replaceContext`` test of ``getContextOwner``)."""
owner = self.owner()
return owner if owner.encoding is self.encoding else selfresolve#
resolve() -> Annotations | NoneThe annotations that describe the node: its last check's when it
has checks, else its own (Effect's resolveAnnotations).
Returns Annotations | None
def resolve(self) -> Annotations | None:
"""The annotations that describe the node: its last check's when it
has checks, else its own (Effect's ``resolveAnnotations``)."""
return self.checks[-1].annotations if self.checks else self.annotationsexpected#
expected() -> strThe text "Expected <...>" messages use: the identifier
annotation, else get_expected() (Effect's getExpected).
Returns str
def expected(self) -> str:
"""The text ``"Expected <...>"`` messages use: the ``identifier``
annotation, else ``get_expected()`` (Effect's ``getExpected``)."""
identifier = (self.resolve() or {}).get("identifier")
return identifier if isinstance(identifier, str) else self.get_expected()flip#
flip() -> ASTThe node that encodes what this one decodes. A node without an encoding flips its parts; a leaf is its own flip.
Returns AST
def flip(self) -> AST:
"""The node that encodes what this one decodes. A node without an
encoding flips its parts; a leaf is its own flip."""
if not self._flip_side:
encoding = self.encoding
owner = self.owner()
if encoding is not None:
flipped = flip_encoding(self, encoding)
elif owner is not self:
# A contextual copy's flip is its owner's, in the copy's
# context: one schema to derivations.
flipped_owner = owner.flip()
flipped = self if flipped_owner is owner else _with_context(flipped_owner, self)
else:
flipped = self._flip()
self._flip_side.append(flipped)
return self._flip_side[0]to_type#
to_type() -> ASTThe node's decoded side: no encoding, its parts' decoded sides. A contextual copy's is its owner's, with the copy's key context.
Returns AST
def to_type(self) -> AST:
"""The node's decoded side: no encoding, its parts' decoded sides. A
contextual copy's is its owner's, with the copy's key context."""
owner = self.owner()
if owner is not self:
typed = owner.to_type()
if typed is owner and self.encoding is None:
return self
return _with_context(typed, self)
if not self._type_side:
base = replace_encoding(self, None)
typed = base._to_type()
encoding_checks = typed.encoding_checks
if encoding_checks is not None:
# Checks of the encoded side hold for the decoded one when no
# part transforms; else only those of the shape (a size).
if _preserves_encoding_checks(base):
promoted = encoding_checks
elif typed._keeps_structural_checks():
promoted = structural_checks(encoding_checks)
else:
promoted = None
typed = copy(
typed, encoding_checks=None, checks=join_checks(typed.checks, promoted)
)
self._type_side.append(typed)
if typed is not self and not typed._type_side: # pragma: no mutate, memo only
typed._type_side.append(typed)
return self._type_side[0]children#
children() -> Iterable[AST]The nodes this one parses its parts with (Effect's recur); a
Suspend's target is not made for this.
Returns Iterable[AST]
def children(self) -> Iterable[AST]:
"""The nodes this one parses its parts with (Effect's ``recur``); a
``Suspend``'s target is not made for this."""
return ()