From c5612b8992c3cc1bc4f119096a03f8cc5b2ac6ed Mon Sep 17 00:00:00 2001 From: Dmitry Selyutin Date: Sun, 18 Jun 2023 21:45:57 +0300 Subject: [PATCH] walker: support dataclasses --- src/mdis/walker.py | 10 ++++++++++ 1 file changed, 10 insertions(+) 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 () -- 2.30.2