Skip to content

Commit 896a4de

Browse files
committed
Refactor _validate_and_convert_resource for readability by reorganizing type checks and exception handling
Ref: #63
1 parent dc0c0eb commit 896a4de

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

fhirpathpy/__init__.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,17 @@ def fn(resource: InputType, context: ContextType = None) -> OutputType:
159159

160160

161161
def _validate_and_convert_resource(resource: Any, input_type: type[InputType]) -> dict:
162-
if isinstance(resource, input_type):
163-
if isinstance(resource, dict):
164-
return resource
165-
elif hasattr(resource, "model_dump"):
166-
return resource.model_dump()
167-
else:
168-
raise Exception(f"Don't know how to work with type {type(resource).__name__}")
169-
else:
162+
if not isinstance(resource, input_type):
170163
raise Exception(f"Resource type is {type(resource).__name__}, expected {input_type.__name__}")
171164

165+
if isinstance(resource, dict):
166+
return resource
167+
168+
if hasattr(resource, "model_dump"):
169+
return resource.model_dump()
170+
171+
raise Exception(f"Don't know how to work with type {type(resource).__name__}")
172+
172173

173174
def _format_result(result: list, output_type: type[OutputType], is_first=False) -> Any:
174175
if not isinstance(result, list):

0 commit comments

Comments
 (0)