Skip to content

Commit

Permalink
✨ freeze action
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed May 11, 2023
1 parent d10d7d1 commit 48b3822
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
8 changes: 1 addition & 7 deletions src/arclet/alconna/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,12 @@ class ActType(IntEnum):
"""


@dataclass(eq=True)
@dataclass(eq=True, frozen=True)
class Action:
"""节点触发的动作"""
type: ActType
value: Any

def __post_init__(self):
if self.type == ActType.APPEND and not isinstance(self.value, list):
self.value = [self.value]
elif self.type == ActType.COUNT and not isinstance(self.value, int):
self.value = 1


store = Action(ActType.STORE, Ellipsis)
"""默认的存储动作"""
Expand Down
5 changes: 5 additions & 0 deletions src/arclet/alconna/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

from functools import reduce
from dataclasses import replace
from typing import Any, Iterable, Sequence, overload

from tarina import lang
Expand All @@ -17,6 +18,10 @@ def _handle_default(node: CommandNode):
if node.default is None:
return
act = node.action
if act.type == 1 and not isinstance(act.value, list):
act = node.action = replace(act, value=[act.value])
elif act.type == 2 and not isinstance(act.value, int):
act = node.action = replace(act, value=1)
if isinstance(node.default, (OptionResult, SubcommandResult)):
if act.type == 0 and act.value is ...:
node.action = Action(act.type, node.default.value)
Expand Down

0 comments on commit 48b3822

Please sign in to comment.