Skip to content

Commit

Permalink
Source Shopify #4841 - improving extracting schemas type
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaliizazmic committed Aug 18, 2021
1 parent 8adaeb0 commit 7c18eb9
Showing 1 changed file with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ def _get_json_types(value_type) -> List[str]:
}
return json_types.get(value_type)

@staticmethod
def _extract_schema_type(properties: Mapping[str, Any]) -> str:
schema_types = properties.get("type", [])
if not isinstance(schema_types, list):
schema_types = [
schema_types,
]
return schema_types

@staticmethod
def _find_schema_type(schema_types: List[str]) -> str:
not_null_types = schema_types.copy()
Expand All @@ -53,28 +62,27 @@ def _find_schema_type(schema_types: List[str]) -> str:
def _transform_number(value: Any):
return Decimal(value)

def _transform_array(self, array: List[Any], item_properties: MutableMapping[str, Any]):
def _transform_array(self, array: List[Any], item_properties: Mapping[str, Any]):
# iterate over items in array, compare schema types and convert if necessary.
item_types = item_properties.get("type", [])
item_types = self._extract_schema_type(item_properties)
if item_types:
schema_type = self._find_schema_type(item_types)
nested_properties = item_properties.get("properties", {})
item_properties = item_properties.get("items", {})
for item in array:
if schema_type == "object":
self._transform_object(item, nested_properties)
if schema_type == "array":
self._transform_array(item, item_properties)

def _transform_object(self, transform_object: Mapping[str, Any], properties: MutableMapping[str, Any]):
def _transform_object(self, transform_object: MutableMapping[str, Any], properties: Mapping[str, Any]):
# compare schema types and convert if necessary.
for object_property, value in transform_object.items():
if value is None:
continue
if object_property in properties:
object_properties = properties.get(object_property)
schema_types = object_properties.get("type", [])
if not isinstance(schema_types, list):
schema_types = [
schema_types,
]
schema_types = self._extract_schema_type(object_properties)
if not schema_types:
continue
value_json_types = self._get_json_types(type(value))
Expand Down

0 comments on commit 7c18eb9

Please sign in to comment.