Skip to content

Commit

Permalink
Add extensions for primitive types #6
Browse files Browse the repository at this point in the history
  • Loading branch information
ruscoder committed Aug 28, 2024
1 parent 20625d8 commit 8b460fc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 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()
29 changes: 22 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,24 @@ 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 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 +148,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 +160,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 list(zip_identifier_type(definition, identifier))
)


Expand Down

0 comments on commit 8b460fc

Please sign in to comment.