Decorators¶
decorators
¶
Decorators for message classes.
Provides @message_dataclass (and its aliases @event_dataclass,
@command_dataclass, @query_dataclass) to reduce boilerplate when
defining message types. The decorated class is a frozen dataclass whose
fields are automatically exposed via get_payload_fields() and are used
by _from_payload_fields() for deserialisation.
Example::
from forging_blocks.foundation.messages.decorators import event_dataclass
from forging_blocks.foundation.messages.event import Event
@event_dataclass
class OrderCreated(Event[dict[str, object]]):
order_id: str
customer_id: str
total: float
event = OrderCreated(order_id="ORD-001", customer_id="CUST-42", total=99.95)
event.to_dict() # includes both "payload" and "data" keys
event_dataclass = message_dataclass
module-attribute
¶
Alias for @message_dataclass intended for domain events.
command_dataclass = message_dataclass
module-attribute
¶
Alias for @message_dataclass intended for commands.
query_dataclass = message_dataclass
module-attribute
¶
Alias for @message_dataclass intended for queries.
message_dataclass(cls: type[_M] | None = None, *, frozen: bool = True) -> type[_M] | Callable[[type[_M]], type[_M]]
¶
Decorate a class as a message dataclass.
The decorator applies @dataclass(frozen=frozen) and then patches
get_payload_fields and _from_payload_fields onto the class so
that payload data is automatically derived from its fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type[_M] | None
|
The class to decorate (when used without arguments). |
None
|
frozen
|
bool
|
Whether the dataclass should be frozen (default |
True
|
Returns:
| Type | Description |
|---|---|
type[_M] | Callable[[type[_M]], type[_M]]
|
The decorated class (or a wrapper when called with keyword arguments). |
Source code in src/forging_blocks/foundation/messages/decorators.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |