Skip to content

Commit

Permalink
Merge pull request #12 from beda-software/issue-6
Browse files Browse the repository at this point in the history
Add extensions for primitive types #6
  • Loading branch information
m0rl authored Aug 28, 2024
2 parents 7dfe431 + 5374bc1 commit 6eabea6
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 14 deletions.
5 changes: 5 additions & 0 deletions fhir_py_types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@ class StructureDefinition:

def is_polymorphic(definition: StructureDefinition) -> bool:
return len(definition.type) > 1


def is_primitive_type(property_type: StructurePropertyType) -> bool:
# All primitive types starts with lowercased letters
return property_type.code[0].islower()
32 changes: 25 additions & 7 deletions fhir_py_types/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
StructureDefinitionKind,
StructurePropertyType,
is_polymorphic,
is_primitive_type,
)

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -111,10 +112,27 @@ def remap_type(
def zip_identifier_type(
definition: StructureDefinition, identifier: str
) -> Iterable[tuple[str, StructurePropertyType]]:
return (
(format_identifier(definition, identifier, t), t)
for t in [remap_type(definition, t) for t in definition.type]
)
result = []

for t in [remap_type(definition, t) for t in definition.type]:
result.append((format_identifier(definition, identifier, t), t))
if (
definition.kind != StructureDefinitionKind.PRIMITIVE
and is_primitive_type(t)
):
result.append(
(
f"_{format_identifier(definition, identifier, t)}",
StructurePropertyType(
code="Element",
target_profile=[],
required=False,
isarray=definition.type[0].isarray,
),
)
)

return result


def make_assignment_statement(
Expand All @@ -133,7 +151,7 @@ def make_assignment_statement(


def type_annotate(
defintion: StructureDefinition,
definition: StructureDefinition,
identifier: str,
form: Literal[AnnotationForm.Property, AnnotationForm.TypeAlias],
) -> Iterable[ast.stmt]:
Expand All @@ -145,9 +163,9 @@ def type_annotate(
form,
default=make_default_initializer(identifier_, type_),
),
ast.Expr(value=ast.Constant(defintion.docstring)),
ast.Expr(value=ast.Constant(definition.docstring)),
]
for (identifier_, type_) in zip_identifier_type(defintion, identifier)
for (identifier_, type_) in zip_identifier_type(definition, identifier)
)


Expand Down
76 changes: 69 additions & 7 deletions tests/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ def test_generates_class_for_flat_definition() -> None:
simple=1,
),
ast.Expr(value=ast.Constant(value="test resource property 1")),
ast.AnnAssign(
target=ast.Name(id="_property1"),
annotation=ast.Subscript(
value=ast.Name(id="Optional_"),
slice=ast.Constant("Element"),
),
value=ast.Constant(None),
simple=1,
),
ast.Expr(value=ast.Constant(value="test resource property 1")),
],
decorator_list=[],
type_params=[],
Expand All @@ -92,21 +102,21 @@ def test_generates_class_for_flat_definition() -> None:
(
[
StructureDefinition(
id="TestResource",
docstring="test resource description",
id="date",
docstring="date description",
type=[
StructurePropertyType(code="str", required=True, isarray=False)
StructurePropertyType(code="date", required=True, isarray=False)
],
elements={},
kind=StructureDefinitionKind.PRIMITIVE,
)
],
[
ast.Assign(
targets=[ast.Name("TestResource")],
value=ast.Name("str"),
targets=[ast.Name("date")],
value=ast.Name("date"),
),
ast.Expr(value=ast.Constant("test resource description")),
ast.Expr(value=ast.Constant("date description")),
],
),
],
Expand Down Expand Up @@ -173,6 +183,18 @@ def test_generates_multiple_classes_for_compound_definition() -> None:
ast.Expr(
value=ast.Constant(value="nested test resource property 1")
),
ast.AnnAssign(
target=ast.Name(id="_property1"),
annotation=ast.Subscript(
value=ast.Name(id="Optional_"),
slice=ast.Constant("Element"),
),
simple=1,
value=ast.Constant(None),
),
ast.Expr(
value=ast.Constant(value="nested test resource property 1")
),
],
decorator_list=[],
type_params=[],
Expand Down Expand Up @@ -314,6 +336,24 @@ def test_generates_annotations_according_to_structure_type(
else None,
),
ast.Expr(value=ast.Constant(value="test resource property 1")),
ast.AnnAssign(
target=ast.Name(id="_property1"),
annotation=ast.Subscript(
value=ast.Name(id="Optional_"),
slice=ast.Subscript(
value=ast.Name(id="List_"),
slice=ast.Constant("Element"),
),
)
if isarray
else ast.Subscript(
value=ast.Name(id="Optional_"),
slice=ast.Constant("Element"),
),
simple=1,
value=ast.Constant(None),
),
ast.Expr(value=ast.Constant(value="test resource property 1")),
],
decorator_list=[],
type_params=[],
Expand All @@ -329,7 +369,7 @@ def test_generates_annotations_according_to_structure_type(
)


def test_unrolls_required_polymorphic_into_class_uion() -> None:
def test_unrolls_required_polymorphic_into_class_union() -> None:
assert_eq(
[
StructureDefinition(
Expand Down Expand Up @@ -385,6 +425,16 @@ def test_unrolls_required_polymorphic_into_class_uion() -> None:
value=ast.Constant(None),
),
ast.Expr(value=ast.Constant(value="monotype property definition")),
ast.AnnAssign(
target=ast.Name(id="_monotype"),
annotation=ast.Subscript(
value=ast.Name(id="Optional_"),
slice=ast.Constant("Element"),
),
simple=1,
value=ast.Constant(None),
),
ast.Expr(value=ast.Constant(value="monotype property definition")),
ast.AnnAssign(
target=ast.Name(id="valueBoolean"),
annotation=ast.Subscript(
Expand All @@ -397,6 +447,18 @@ def test_unrolls_required_polymorphic_into_class_uion() -> None:
ast.Expr(
value=ast.Constant(value="polymorphic property definition")
),
ast.AnnAssign(
target=ast.Name(id="_valueBoolean"),
annotation=ast.Subscript(
value=ast.Name(id="Optional_"),
slice=ast.Constant("Element"),
),
simple=1,
value=ast.Constant(None),
),
ast.Expr(
value=ast.Constant(value="polymorphic property definition")
),
ast.AnnAssign(
target=ast.Name(id="valueQuantity"),
annotation=ast.Subscript(
Expand Down

0 comments on commit 6eabea6

Please sign in to comment.