pydantic-avro 0.8.0__py3-none-any.whl → 0.9.0__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.
@@ -61,6 +61,8 @@ AVRO_TYPE_MAPPING = {
61
61
  },
62
62
  }
63
63
 
64
+ PRIMITVE_TYPES = ["int", "long", "float", "double", "boolean", "null"]
65
+
64
66
 
65
67
  def get_definition(ref: str, schema: dict):
66
68
  """Reading definition of base schema for nested structs"""
@@ -133,6 +135,9 @@ class AvroTypeConverter:
133
135
  at = field_props.get("avro_type")
134
136
  if "allOf" in field_props and len(field_props["allOf"]) == 1:
135
137
  r = field_props["allOf"][0]["$ref"]
138
+ if ("prefixItems" in field_props or ("minItems" in field_props and "maxItems" in field_props)) and t == "array":
139
+ t = "tuple"
140
+
136
141
  u = field_props.get("anyOf")
137
142
 
138
143
  if u is not None:
@@ -162,6 +167,8 @@ class AvroTypeConverter:
162
167
  avro_type_dict["type"] = "boolean"
163
168
  elif t == "null":
164
169
  avro_type_dict["type"] = "null"
170
+ elif t == "tuple":
171
+ return self._tuple_to_avro(field_props, avro_type_dict)
165
172
  else:
166
173
  raise NotImplementedError(
167
174
  f"Type '{t}' not support yet, "
@@ -216,7 +223,10 @@ class AvroTypeConverter:
216
223
  def _object_to_avro(self, field_props: dict) -> dict:
217
224
  """Returns a type of an object field"""
218
225
  a = field_props.get("additionalProperties")
219
- value_type = "string" if a is None else self._get_avro_type_dict(a)["type"]
226
+ if not a or isinstance(a, bool):
227
+ value_type = "string"
228
+ else:
229
+ value_type = self._get_avro_type_dict(a)["type"]
220
230
  return {"type": "map", "values": value_type}
221
231
 
222
232
  def _union_to_avro(self, field_props: list, avro_type_dict: dict) -> dict:
@@ -227,23 +237,43 @@ class AvroTypeConverter:
227
237
  avro_type_dict["type"].append(t["type"])
228
238
  return avro_type_dict
229
239
 
240
+ def _tuple_to_avro(self, field_props: dict, avro_type_dict: dict) -> dict:
241
+ """Returns a type of a tuple field"""
242
+ prefix_items = field_props.get("prefixItems")
243
+ if not prefix_items:
244
+ # Pydantic v1 there is no prefixItems, but minItems and maxItems and items is a list.
245
+ prefix_items = field_props.get("items")
246
+ if not prefix_items:
247
+ raise ValueError(f"Tuple Field '{field_props}' does not have any items .")
248
+ possible_types = []
249
+ for prefix_item in prefix_items:
250
+ item_type = self._get_avro_type(prefix_item, avro_type_dict).get("type")
251
+ if not item_type:
252
+ raise ValueError(f"Field '{avro_type_dict}' does not have a defined type.")
253
+ if isinstance(item_type, list):
254
+ possible_types.extend([x for x in item_type if x not in possible_types])
255
+ elif item_type not in possible_types:
256
+ possible_types.append(item_type)
257
+ avro_type_dict["type"] = {"type": "array", "items": possible_types}
258
+ return avro_type_dict
259
+
230
260
  def _array_to_avro(self, field_props: dict, avro_type_dict: dict) -> dict:
231
261
  """Returns a type of an array field"""
232
- items = field_props["items"]
262
+ items = field_props.get("items")
263
+ # In pydantic v1. items is a list, we need to handle this case first
264
+ # E.g. [{'type': 'number'}, {'type': 'number'}]}
265
+ if isinstance(items, list):
266
+ if not len(items):
267
+ raise ValueError(f"Field '{field_props}' does not have valid items.")
268
+ items = items[0]
269
+ if not isinstance(items, dict):
270
+ raise ValueError(f"Field '{field_props}' does not have valid items.")
233
271
  tn = self._get_avro_type_dict(items)
234
272
  # If items in array are an object:
235
273
  if "$ref" in items:
236
274
  tn = tn["type"]
237
275
  # If items in array are a logicalType
238
- if (
239
- isinstance(tn, dict)
240
- and isinstance(tn.get("type", {}), dict)
241
- and tn.get("type", {}).get("logicalType") is not None
242
- ):
276
+ if isinstance(tn, dict) and isinstance(tn.get("type", None), (dict, list)):
243
277
  tn = tn["type"]
244
- # If items in array are an array, the structure must be corrected
245
- if isinstance(tn, dict) and isinstance(tn.get("type", {}), dict) and tn.get("type", {}).get("type") == "array":
246
- items = tn["type"]["items"]
247
- tn = {"type": "array", "items": items}
248
278
  avro_type_dict["type"] = {"type": "array", "items": tn}
249
279
  return avro_type_dict
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pydantic-avro
3
- Version: 0.8.0
3
+ Version: 0.9.0
4
4
  Summary: Converting pydantic classes to avro schemas
5
5
  Home-page: https://github.com/godatadriven/pydantic-avro
6
6
  License: MIT
@@ -1,4 +1,3 @@
1
- LICENSE,sha256=gBlYCG1yxb0vGlsmek0lMPVOK5YDxQope4F54jzeqoY,1069
2
1
  pydantic_avro/__init__.py,sha256=P4vaozEL8Rea7xWdB6ENj3DNF2RPPOWascfwbtQS7gE,48
3
2
  pydantic_avro/__main__.py,sha256=-AL9FNYsAdFmilm-96MbbxymTW6QminXPCBZJ1nkqBE,695
4
3
  pydantic_avro/base.py,sha256=i1wco_T6FXFdtlxzbzXt3Jv93TmTzTfJ493KCfZh1dc,78
@@ -10,9 +9,9 @@ pydantic_avro/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
9
  pydantic_avro/to_avro/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
11
10
  pydantic_avro/to_avro/base.py,sha256=Y8QOGTpXO3BwJ6arrRK-k2JniK5kLTvlDpQ0PyZojmg,1409
12
11
  pydantic_avro/to_avro/config.py,sha256=R9HLkWiXs7YZ02te8CQt5Kf2rbsE8Fx-hjqv3tBSFJU,152
13
- pydantic_avro/to_avro/types.py,sha256=9bGoTDxejaHUsKRvQxwqF3-k7NGHU-vjTnhzR3o8m8o,8980
14
- pydantic_avro-0.8.0.dist-info/entry_points.txt,sha256=gwHiQfbGLO8Np2sa1bZ_bpxU7sEufx6IachViBE_Fnw,66
15
- pydantic_avro-0.8.0.dist-info/LICENSE,sha256=gBlYCG1yxb0vGlsmek0lMPVOK5YDxQope4F54jzeqoY,1069
16
- pydantic_avro-0.8.0.dist-info/WHEEL,sha256=vxFmldFsRN_Hx10GDvsdv1wroKq8r5Lzvjp6GZ4OO8c,88
17
- pydantic_avro-0.8.0.dist-info/METADATA,sha256=eqI3PQnW1t_AXqyV5gFFlV62w1czrR5m1IaTzY4DLzk,2945
18
- pydantic_avro-0.8.0.dist-info/RECORD,,
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,,
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2021 GoDataDriven
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
File without changes