walker: support dataclasses
authorDmitry Selyutin <ghostmansd@gmail.com>
Sun, 18 Jun 2023 18:45:57 +0000 (21:45 +0300)
committerDmitry Selyutin <ghostmansd@gmail.com>
Sun, 18 Jun 2023 18:46:23 +0000 (21:46 +0300)
src/mdis/walker.py

index 5de9ff0e6055487802fe2b464ad020461d52b0c4..a5762fadd096a8a57f55af2f00b63366ef830634 100644 (file)
@@ -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 ()