Skip to content

Commit

Permalink
Source Shopify #4841 - reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaliizazmic committed Aug 18, 2021
1 parent fe8e8b3 commit 45c10a7
Showing 1 changed file with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#

from decimal import Decimal
from typing import List, MutableMapping, Any, Iterable, Mapping
from typing import Any, Iterable, List, Mapping, MutableMapping


class Transformer:

@staticmethod
def _get_json_types(value_type) -> List[str]:
json_types = {
Expand All @@ -36,7 +36,9 @@ def _get_json_types(value_type) -> List[str]:
dict: ["object"],
list: ["array"],
bool: ["boolean"],
type(None): ["null", ]
type(None): [
"null",
],
}
return json_types.get(value_type)

Expand All @@ -60,7 +62,6 @@ def _transform_array(self, array: List[Any], item_properties: MutableMapping[str
for item in array:
if schema_type == "object":
self._transform_object(item, nested_properties)
yield item

def _transform_object(self, transform_object: Mapping[str, Any], properties: MutableMapping[str, Any]):
# compare schema types and convert if necessary.
Expand All @@ -71,7 +72,9 @@ def _transform_object(self, transform_object: Mapping[str, Any], properties: Mut
object_properties = properties.get(object_property)
schema_types = object_properties.get("type", [])
if not isinstance(schema_types, list):
schema_types = [schema_types, ]
schema_types = [
schema_types,
]
if not schema_types:
continue
value_json_types = self._get_json_types(type(value))
Expand All @@ -89,5 +92,7 @@ def _transform_object(self, transform_object: Mapping[str, Any], properties: Mut
def transform(self, records: List[MutableMapping[str, Any]], schema: Mapping[str, Any]) -> Iterable[MutableMapping]:
# Shopify API returns array of objects
# It's need to compare records values with schemas
stream_properties = {"type": ["null", "object"], "properties": schema.get("properties", {})}
yield from self._transform_array(records, stream_properties)
properties = schema.get("properties", {})
for record in records:
self._transform_object(record, properties)
yield record

0 comments on commit 45c10a7

Please sign in to comment.