diff --git a/fhirflat/flat2fhir.py b/fhirflat/flat2fhir.py index 6ce94e8..3cec5d4 100644 --- a/fhirflat/flat2fhir.py +++ b/fhirflat/flat2fhir.py @@ -193,6 +193,9 @@ def set_datatypes(k, v_dict, klass) -> dict: return {s.split(".", 1)[1]: v_dict[s] for s in v_dict} +# feels like there's a lot of duplication within the extension handling. Can we replace +# 'createExtension' with 'find_value_type'/ reuse some of the code? bit wierd how it +# jumps around so much. def find_value_type(k: str, v_dict, klass): prop = klass.schema()["properties"] value_type = [key for key in prop.keys() if key.startswith("value")] @@ -216,38 +219,27 @@ def find_value_type(k: str, v_dict, klass): + expanded_short_extensions, } - if len(value_type) == 1: - value_type = value_type[0] - data_type = prop[value_type]["type"] + for v_type in value_type: + data_type = prop[v_type]["type"] try: data_class = get_fhirtype(data_type) - return {"url": k, f"{value_type}": set_datatypes(k, v_dict, data_class)} - except AttributeError: - # datatype should be a primitive - return {"url": k, f"{value_type[0]}": v_dict[k]} - - else: - for v_type in value_type: - data_type = prop[v_type]["type"] + new_dict = v_dict[k] + new_dict = ( + expand_concepts(new_dict, data_class) + if isinstance(new_dict, dict) + else new_dict + ) try: - data_class = get_fhirtype(data_type) - new_dict = v_dict[k] - new_dict = ( - expand_concepts(new_dict, data_class) - if isinstance(new_dict, dict) - else new_dict - ) - try: - data_class.parse_obj(new_dict) - return {"url": k, f"{v_type}": new_dict} - except ValidationError: - continue - except AttributeError as e: - # should be a standard json type as a string - if isinstance(v_dict[k], json_type_matching(data_type)): - return {"url": k, f"{v_type}": v_dict[k]} - else: - raise e + data_class.parse_obj(new_dict) + return {"url": k, f"{v_type}": new_dict} + except ValidationError: + continue + except AttributeError as e: + # should be a standard json type as a string + if isinstance(v_dict[k], json_type_matching(data_type)): + return {"url": k, f"{v_type}": v_dict[k]} + else: + raise e def expand_concepts(data: dict[str, str], data_class: type[_DomainResource]) -> dict: diff --git a/fhirflat/util.py b/fhirflat/util.py index ee756c3..a2d65ca 100644 --- a/fhirflat/util.py +++ b/fhirflat/util.py @@ -110,10 +110,7 @@ def find_data_class_options( raise ValueError(f"Couldn't find a matching class for {k} in {data_class}") else: - try: - k_schema = data_class.schema()["properties"][k] - except KeyError: - k_schema = data_class.schema()["properties"]["extension"] + k_schema = data_class.schema()["properties"][k] base_class = ( k_schema.get("items").get("type") @@ -126,8 +123,6 @@ def find_data_class_options( base_class = [opt.get("type") for opt in k_schema["items"]["anyOf"]] - if k in base_class: - return get_fhirtype(k) return get_fhirtype(base_class)