pydantic-avro 0.9.0__py3-none-any.whl → 0.9.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -132,7 +132,7 @@ def generate_field_string(field: dict) -> str:
132
132
 
133
133
  def get_pydantic_type(t: Union[str, dict]) -> str:
134
134
  """Get the Pydantic type for a given Avro type"""
135
- if isinstance(t, str):
135
+ if isinstance(t, str) or isinstance(t, list):
136
136
  t = {"type": t}
137
137
 
138
138
  if isinstance(t.get("type"), str):
@@ -137,11 +137,14 @@ class AvroTypeConverter:
137
137
  r = field_props["allOf"][0]["$ref"]
138
138
  if ("prefixItems" in field_props or ("minItems" in field_props and "maxItems" in field_props)) and t == "array":
139
139
  t = "tuple"
140
-
141
140
  u = field_props.get("anyOf")
141
+ o = field_props.get("oneOf")
142
+ discriminator = field_props.get("discriminator")
142
143
 
143
144
  if u is not None:
144
- return self._union_to_avro(u, avro_type_dict)
145
+ return self._union_to_avro(u, avro_type_dict, discriminator)
146
+ elif o is not None:
147
+ return self._union_to_avro(o, avro_type_dict, discriminator)
145
148
  elif r is not None:
146
149
  return self._handle_references(r, avro_type_dict)
147
150
  elif t is None:
@@ -229,8 +232,13 @@ class AvroTypeConverter:
229
232
  value_type = self._get_avro_type_dict(a)["type"]
230
233
  return {"type": "map", "values": value_type}
231
234
 
232
- def _union_to_avro(self, field_props: list, avro_type_dict: dict) -> dict:
233
- """Returns a type of a union field"""
235
+ def _union_to_avro(self, field_props: list, avro_type_dict: dict, discriminator: Optional[dict] = None) -> dict:
236
+ """Returns a type of a union field, including discriminated unions"""
237
+ # Handle discriminated unions
238
+ if discriminator is not None:
239
+ return self._discriminated_union_to_avro(field_props, avro_type_dict, discriminator)
240
+
241
+ # Standard union handling (unchanged)
234
242
  avro_type_dict["type"] = []
235
243
  for union_element in field_props:
236
244
  t = self._get_avro_type_dict(union_element)
@@ -277,3 +285,39 @@ class AvroTypeConverter:
277
285
  tn = tn["type"]
278
286
  avro_type_dict["type"] = {"type": "array", "items": tn}
279
287
  return avro_type_dict
288
+
289
+ def _discriminated_union_to_avro(self, variants: list, avro_type_dict: dict, discriminator: dict) -> dict:
290
+ """Handles discriminated unions with a discriminator field"""
291
+ discriminator_property = discriminator.get("propertyName", "type")
292
+ variant_types = []
293
+
294
+ # Process each variant in the union
295
+ for variant in variants:
296
+ # Get the discriminator value for this variant
297
+ disc_value = None
298
+ if "properties" in variant and discriminator_property in variant.get("properties", {}):
299
+ prop = variant["properties"][discriminator_property]
300
+ if "const" in prop:
301
+ disc_value = prop["const"]
302
+ elif "enum" in prop and len(prop["enum"]) == 1:
303
+ disc_value = prop["enum"][0]
304
+
305
+ # If we can't determine the discriminator value, generate a regular record
306
+ if disc_value is None:
307
+ variant_type = self._get_avro_type_dict(variant)
308
+ else:
309
+ # Create a named record for this variant
310
+ variant_name = f"{disc_value}Variant"
311
+ if "$ref" in variant:
312
+ ref_schema = get_definition(variant["$ref"], self.root_schema)
313
+ variant_fields = self.fields_to_avro_dicts(ref_schema)
314
+ else:
315
+ variant_fields = self.fields_to_avro_dicts(variant)
316
+
317
+ variant_type = {"type": {"type": "record", "name": variant_name, "fields": variant_fields}}
318
+
319
+ variant_types.append(variant_type["type"])
320
+
321
+ # Set the union of all variant types
322
+ avro_type_dict["type"] = variant_types
323
+ return avro_type_dict
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pydantic-avro
3
- Version: 0.9.0
3
+ Version: 0.9.2
4
4
  Summary: Converting pydantic classes to avro schemas
5
5
  Home-page: https://github.com/godatadriven/pydantic-avro
6
6
  License: MIT
@@ -4,14 +4,14 @@ pydantic_avro/base.py,sha256=i1wco_T6FXFdtlxzbzXt3Jv93TmTzTfJ493KCfZh1dc,78
4
4
  pydantic_avro/from_avro/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
5
  pydantic_avro/from_avro/avro_to_pydantic.py,sha256=FOEd_6TqoHSlLALgyveb456B3f7TEPGw2ByRgRcigqc,1356
6
6
  pydantic_avro/from_avro/class_registery.py,sha256=n_8yELp-Eux9bAs4CJOV78bQ061VV_-2FuvowOQpWCg,864
7
- pydantic_avro/from_avro/types.py,sha256=lt-L0mVAflSrpFxQKtjxx1lUCWICdSaXlwGfeJkfXgI,5167
7
+ pydantic_avro/from_avro/types.py,sha256=--aWUMKtR5DUaYXXJ1RrDonLZ7AADcdGGAARopLZexA,5190
8
8
  pydantic_avro/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
9
  pydantic_avro/to_avro/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
10
  pydantic_avro/to_avro/base.py,sha256=Y8QOGTpXO3BwJ6arrRK-k2JniK5kLTvlDpQ0PyZojmg,1409
11
11
  pydantic_avro/to_avro/config.py,sha256=R9HLkWiXs7YZ02te8CQt5Kf2rbsE8Fx-hjqv3tBSFJU,152
12
- pydantic_avro/to_avro/types.py,sha256=pnDRg8ar9a5f57AeEsQr-wyeFJzR2_9fmrRcNkjWT4c,10521
13
- pydantic_avro-0.9.0.dist-info/entry_points.txt,sha256=gwHiQfbGLO8Np2sa1bZ_bpxU7sEufx6IachViBE_Fnw,66
14
- pydantic_avro-0.9.0.dist-info/LICENSE,sha256=gBlYCG1yxb0vGlsmek0lMPVOK5YDxQope4F54jzeqoY,1069
15
- pydantic_avro-0.9.0.dist-info/WHEEL,sha256=vxFmldFsRN_Hx10GDvsdv1wroKq8r5Lzvjp6GZ4OO8c,88
16
- pydantic_avro-0.9.0.dist-info/METADATA,sha256=ugz_U6wYoXrHR5DitsZNo15iWixeTPEbTOyaNvlL_ko,2945
17
- pydantic_avro-0.9.0.dist-info/RECORD,,
12
+ pydantic_avro/to_avro/types.py,sha256=-qemQC_vs-xy7VJDTQ1y1k7K1J6Qv3pTLILqp17MO2A,12731
13
+ pydantic_avro-0.9.2.dist-info/entry_points.txt,sha256=gwHiQfbGLO8Np2sa1bZ_bpxU7sEufx6IachViBE_Fnw,66
14
+ pydantic_avro-0.9.2.dist-info/LICENSE,sha256=gBlYCG1yxb0vGlsmek0lMPVOK5YDxQope4F54jzeqoY,1069
15
+ pydantic_avro-0.9.2.dist-info/WHEEL,sha256=vxFmldFsRN_Hx10GDvsdv1wroKq8r5Lzvjp6GZ4OO8c,88
16
+ pydantic_avro-0.9.2.dist-info/METADATA,sha256=cSYqoXQORSErCOuZMd_T30e-bHrbRRN0bgHMLM5TXq8,2945
17
+ pydantic_avro-0.9.2.dist-info/RECORD,,