From: Dmitry Selyutin Date: Sun, 18 Jun 2023 18:45:57 +0000 (+0300) Subject: walker: support dataclasses X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=c5612b8992c3cc1bc4f119096a03f8cc5b2ac6ed;p=mdis.git walker: support dataclasses --- diff --git a/src/mdis/walker.py b/src/mdis/walker.py index 5de9ff0..a5762fa 100644 --- a/src/mdis/walker.py +++ b/src/mdis/walker.py @@ -3,6 +3,8 @@ __all__ = [ "WalkerMeta", ] +import dataclasses + from . import dispatcher @@ -23,6 +25,14 @@ class Walker(dispatcher.Dispatcher, metaclass=WalkerMeta): yield (key, value) yield from self((key, value)) + @dispatcher.Hook(dataclasses.is_dataclass) + def dispatch_dataclass(self, instance): + for field in dataclasses.fields(instance): + key = field.name + value = getattr(instance, key) + yield (key, value) + yield from self((key, value)) + @dispatcher.Hook(object) def dispatch_object(self, instance): yield from ()