MCPcopy Create free account
hub / github.com/apache/fory / _write_typing_union

Method _write_typing_union

python/pyfory/union.py:172–190  ·  view source on GitHub ↗
(self, write_context, value)

Source from the content-addressed store, hash-verified

170 return builder(case_id, value)
171
172 def _write_typing_union(self, write_context, value):
173 active_index = None
174 active_serializer = None
175 active_type = None
176
177 for i, (alt_type, serializer) in enumerate(self._alternative_serializers):
178 if isinstance(value, alt_type):
179 active_index = i
180 active_serializer = serializer
181 active_type = alt_type
182 break
183
184 if active_index is None:
185 raise TypeError(f"Value {value} of type {type(value)} doesn't match any alternative in Union{self._alternative_types}")
186
187 write_context.write_var_uint32(active_index)
188 typeinfo = self.type_resolver.get_type_info(active_type)
189 self.type_resolver.write_type_info(write_context, typeinfo)
190 active_serializer.write(write_context, value)
191
192 def _read_typing_union(self, read_context):
193 stored_index = read_context.read_var_uint32()

Callers 1

writeMethod · 0.95

Calls 4

writeMethod · 0.65
write_var_uint32Method · 0.45
get_type_infoMethod · 0.45
write_type_infoMethod · 0.45

Tested by

no test coverage detected