Skip to content

Commit

Permalink
fix mapping input content when associated with json mixin (i.e. FileD…
Browse files Browse the repository at this point in the history
…ata)
  • Loading branch information
rbiseck3 committed Jul 17, 2024
1 parent fcd8fef commit f06a9bf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
10 changes: 9 additions & 1 deletion test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from enum import Enum

from pydantic import BaseModel
from unstructured.ingest.v2.interfaces import FileData
from uvicorn.importer import import_from_string

from unstructured_platform_plugins.etl_uvicorn import utils
Expand Down Expand Up @@ -101,14 +102,20 @@ class MyEnum(Enum):


def test_map_inputs():
def fn(a: A, b: B, c: MyEnum, d: list) -> None:
def fn(a: A, b: B, c: MyEnum, d: list, e: FileData) -> None:
pass

file_data = FileData(
identifier="custom_file_data",
connector_type="mock_connector",
additional_metadata={"additional": "metadata"},
)
inputs = {
"a": {"b": 4, "c": 5.6},
"b": {"d": True, "e": {"key": "value"}},
"c": MyEnum.VALUE.value,
"d": [1, 2, 3],
"e": file_data.to_dict(),
}

mapped_inputs = utils.map_inputs(func=fn, raw_inputs=inputs)
Expand All @@ -117,5 +124,6 @@ def fn(a: A, b: B, c: MyEnum, d: list) -> None:
"b": B(d=True, e={"key": "value"}),
"c": MyEnum.VALUE.value,
"d": [1, 2, 3],
"e": file_data,
}
assert mapped_inputs == expected
10 changes: 8 additions & 2 deletions unstructured_platform_plugins/etl_uvicorn/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from types import GenericAlias, NoneType
from typing import Any, Callable, Optional

from dataclasses_json import DataClassJsonMixin
from pydantic import BaseModel

from unstructured_platform_plugins.schema.json_schema import (
Expand Down Expand Up @@ -83,11 +84,16 @@ def map_inputs(func: Callable, raw_inputs: dict[str, Any]) -> dict[str, Any]:
type_info = get_type_hints(func)
type_info.pop("return")
for field_name, type_data in type_info.items():
if field_name not in raw_inputs:
continue

if (
is_dataclass(type_data)
and field_name in raw_inputs
inspect.isclass(type_data)
and issubclass(type_data, DataClassJsonMixin)
and isinstance(raw_inputs[field_name], dict)
):
raw_inputs[field_name] = type_data.from_dict(raw_inputs[field_name])
elif is_dataclass(type_data) and isinstance(raw_inputs[field_name], dict):
raw_inputs[field_name] = type_data(**raw_inputs[field_name])
elif isinstance(type_data, EnumMeta):
raw_inputs[field_name] = raw_inputs[field_name]
Expand Down

0 comments on commit f06a9bf

Please sign in to comment.