@@ -115,61 +115,59 @@ def nest_value_in_output(value: Any) -> dict:
115115 )
116116 return nest_value_in_output (vip_variable .model_dump (by_alias = True , exclude_none = True ))
117117
118+ if value is not None and not self .children :
119+ output ["vipType" ] = vip_type or FeatureTemplateOptionType .CONSTANT .value
120+ output ["vipValue" ] = json_dumped_value
121+ elif value is not None and self .children :
122+ output ["vipType" ] = vip_type or FeatureTemplateOptionType .CONSTANT .value
123+ children_output = []
124+ for obj in value :
125+ obj_json_dump = obj .model_dump (mode = "json" )
126+ child_payload : dict = {}
127+ for child in self .children : # Child in schema
128+ obj : FeatureTemplate # type: ignore
129+
130+ # We are searching for the model field that
131+ # corresponds to the child element (field from vManage schema)
132+ # If this line fails, it means that the schema from vManage has more fields than the model
133+ # and the model needs to be updated (add missing fields)
134+ model_tuple = next (
135+ filter (
136+ lambda f : (
137+ f [1 ].alias == child .key
138+ or get_extra_field (f [1 ], "vmanage_key" ) == child .key
139+ or f [0 ] == child .key
140+ ),
141+ obj .model_fields .items (),
142+ )
143+ )
144+ # We get the pydantic model field and the value from the object
145+ model_field = model_tuple [1 ]
146+ obj_value = getattr (obj , model_tuple [0 ])
147+ obj_json_value = obj_json_dump .get (model_tuple [0 ])
148+ po = get_extra_field (model_field , "priority_order" )
149+ vip_type = get_extra_field (model_field , "vip_type" )
150+ # After we get the data, we create the payload by populating the schema
151+ # Then we merge the payload to the output
152+ merge (
153+ child_payload ,
154+ child .payload_scheme (
155+ obj_value ,
156+ json_dumped_value = obj_json_value ,
157+ priority_order = po ,
158+ vip_type = vip_type ,
159+ ),
160+ )
161+ if priority_order :
162+ child_payload .update ({"priority-order" : priority_order })
163+ children_output .append (child_payload )
164+ output ["vipValue" ] = children_output
118165 else :
119- if value is not None :
120- output ["vipType" ] = vip_type or FeatureTemplateOptionType .CONSTANT .value
121- if self .children :
122- children_output = []
123- for obj in value : # obj is User, atomic value. Loop every child
124- obj_json_dump = obj .model_dump (mode = "json" )
125- child_payload : dict = {}
126- for child in self .children : # Child in schema
127- obj : FeatureTemplate # type: ignore
128- model_tuple = next (
129- filter (
130- lambda f : get_extra_field (f [1 ], "data_path" , []) == child .dataPath
131- and (
132- f [1 ].alias == child .key
133- or get_extra_field (f [1 ], "vmanage_key" ) == child .key
134- or f [0 ] == child .key
135- ),
136- obj .model_fields .items (),
137- )
138- )
139- model_field = model_tuple [1 ]
140- obj_value = getattr (obj , model_tuple [0 ])
141- obj_json_value = obj_json_dump .get (model_tuple [0 ])
142- po = get_extra_field (model_field , "priority_order" )
143- vip_type = get_extra_field (model_field , "vip_type" )
144- merge (
145- child_payload ,
146- child .payload_scheme (
147- obj_value ,
148- json_dumped_value = obj_json_value ,
149- priority_order = po ,
150- vip_type = vip_type ,
151- ),
152- )
153- if priority_order :
154- child_payload .update ({"priority-order" : priority_order })
155- children_output .append (child_payload )
156- output ["vipValue" ] = children_output
157- else :
158- output ["vipValue" ] = json_dumped_value
166+ if value is None or "default" in self .dataType :
167+ return {}
159168 else :
160- if value is None :
161- return {}
162- if "default" in self .dataType :
163- return {}
164- # output["vipValue"] = self.dataType["default"] if value is None else value
165- # output["vipType"] = self.defaultOption.value
166- else :
167- output ["vipValue" ] = []
168- output ["vipType" ] = FeatureTemplateOptionType .IGNORE .value
169-
170- # TODO
171- # DataType to dataclass Model
172- # No default values for everything
169+ output ["vipValue" ] = []
170+ output ["vipType" ] = FeatureTemplateOptionType .IGNORE .value
173171
174172 if self .primaryKeys :
175173 output ["vipPrimaryKey" ] = self .primaryKeys
0 commit comments