Skip to content

Commit

Permalink
DSL POC
Browse files Browse the repository at this point in the history
Ref #1
  • Loading branch information
ir4y committed Mar 24, 2023
1 parent 3805a4a commit 9dccda4
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 69 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ pytest-cov = "==3.0.0"
freezegun = "==1.2.2"
pyyaml = "==5.4"
pre-commit = "~=2.21.0"
black = "*"
201 changes: 132 additions & 69 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions fhirpathpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from fhirpathpy.engine import do_eval
from fhirpathpy.engine.util import arraify, get_data, set_paths
from fhirpathpy.engine.nodes import FP_Type
from fhirpathpy.dsl_impl import DSL

__title__ = "fhirpathpy"
__version__ = "0.1.2"
Expand Down Expand Up @@ -82,3 +83,6 @@ def compile(path, model=None):
For example, you could pass in the result of require("fhirpath/fhir-context/r4")
"""
return set_paths(apply_parsed_path, parsedPath=parse(path), model=model)


dsl = DSL()
43 changes: 43 additions & 0 deletions fhirpathpy/dsl_impl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class DSL:
path: str

def __init__(self, prefix=None):
self.path = prefix

def __getattr__(self, attr):
if self.path is None and attr == "empty":
return DSL("{}")
if self.path is None and attr == "env":
return DSL("%")
elif self.path == "%":
return DSL(f"{self.path}{attr}")
elif self.path is not None:
return DSL(f"{self.path}.{attr}")
else:
return DSL(attr)

def __getitem__(self, attr):
return DSL(f"{self.path}[{attr}]")

def __eq__(self, other):
return DSL(f"{self.path} ~ {format_value(other)}")

def __ne__(self, other):
return DSL(f"{self.path} !~ {format_value(other)}")

def __call__(self, **kwargs):
args = build_args(kwargs)
return DSL(f"{self.path}({args})")

def __str__(self):
return self.path


def format_value(value):
if isinstance(value, str):
return f"'{value}'"
return str(value)


def build_args(args):
return ",".join(f"{k}={format_value(v)}" for k, v in args.items())
16 changes: 16 additions & 0 deletions tests/test_dsl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from fhirpathpy import dsl


def dsl_test():
assert str(dsl.Patient.id) == "Patient.id"
assert (
str(dsl.Patient.name.where(use="usual").given.first())
== "Patient.name.where(use='usual').given.first()"
)
assert (
str(
dsl.env.Source.entry[0].resource.expansion.contains.where(code=dsl.env.Coding.code)
!= dsl.empty
)
== "%Source.entry[0].resource.expansion.contains.where(code=%Coding.code) !~ {}"
)

0 comments on commit 9dccda4

Please sign in to comment.