|
| 1 | +""" |
| 2 | +MIT License |
| 3 | +
|
| 4 | +Copyright (c) 2021 Jedy Matt Tabasco |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +of this software and associated documentation files (the "Software"), to deal |
| 8 | +in the Software without restriction, including without limitation the rights |
| 9 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +copies of the Software, and to permit persons to whom the Software is |
| 11 | +furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in all |
| 14 | +copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +SOFTWARE. |
| 23 | +""" |
| 24 | + |
1 | 25 | from typing import NamedTuple |
2 | 26 |
|
3 | 27 | import sqlalchemy |
@@ -118,17 +142,21 @@ def _seed(self, entity, parent: Entity = None): |
118 | 142 |
|
119 | 143 | kwargs = entity[self.__data_key] |
120 | 144 |
|
121 | | - if isinstance(kwargs, dict): |
122 | | - # instantiate object |
123 | | - instance = self._setup_instance(class_, kwargs, parent) |
124 | | - for attr_name, value in iter_ref_attr(kwargs, self.ref_prefix): |
125 | | - self._pre_seed(entity=value, parent=Entity(instance, attr_name)) |
126 | | - |
127 | | - else: # source_data is list |
| 145 | + # kwargs is list |
| 146 | + if isinstance(kwargs, list): |
128 | 147 | for kwargs_ in kwargs: |
129 | 148 | instance = self._setup_instance(class_, kwargs_, parent) |
130 | | - for attr_name, value in iter_ref_attr(kwargs_, self.ref_prefix): |
131 | | - self._pre_seed(value, parent=Entity(instance, attr_name)) |
| 149 | + self._seed_children(instance, kwargs_) |
| 150 | + return |
| 151 | + |
| 152 | + # kwargs is dict |
| 153 | + # instantiate object |
| 154 | + instance = self._setup_instance(class_, kwargs, parent) |
| 155 | + self._seed_children(instance, kwargs) |
| 156 | + |
| 157 | + def _seed_children(self, instance, kwargs): |
| 158 | + for attr_name, value in iter_ref_attr(kwargs, self.ref_prefix): |
| 159 | + self._pre_seed(entity=value, parent=Entity(instance, attr_name)) |
132 | 160 |
|
133 | 161 | def _setup_instance(self, class_, kwargs: dict, parent: Entity): |
134 | 162 | instance = class_(**filter_kwargs(kwargs, class_, self.ref_prefix)) |
|
0 commit comments