Developed and maintained by the nice folks at Verily.
This package contains a Go implementation of the FHIRPath specification, implemented directly with the google/fhir proto definitions.
This package aims to be compliant with both:
- the N1 Normative Release specification, and
- the R4 specifications.
import "github.com/fhir-fli/fhirpath-go/fhirpath"
A FHIRPath must be compiled before running it against a resource using the Compile
method like so:
expression, err := fhirpath.Compile("Patient.name.given")
if err != nil {
panic("error while compiling FHIRPath")
}
The compilation result can then be run against a resource:
inputResources := []fhir.Resource{somePatient, someMedication}
result, err := expression.Evaluate(inputResources)
if err != nil {
panic("error while running FHIRPath against resource")
}
As defined in the FHIRPath specification, the output of evaluation is a Collection. So, the
result of Evaluate is of type []any
. As such, the result must be unpacked and cast to the desired
type for further processing.
Options are provided for optional modification of compilation and evaluation. There is currently support for:
- adding custom functions during Compile time
- adding custom external constant variables
The constraints on the custom function are as follows:
- First argument must be
system.Collection
- Arguments that follow must be either a fhir proto type or primitive system type
customFn := func (input system.Collection, args ...any) (system.Collection error) {
fmt.Print("called custom fn")
return input, nil
}
expression, err := fhirpath.Compile("print()", compopts.AddFunction("print", customFn))
The constraints on external constants are as follows:
- Must be a fhir proto type, primitive system type, or
system.Collection
- If you pass in a collection, contained elements must be fhir proto or system type.
customVar := system.String("custom variable")
result, err := expression.Evaluate([]fhir.Resource{someResource}, evalopts.EnvVariable("var", customVar))
The FHIRPath spec defines the following custom System types:
- Boolean
- String
- Integer
- Decimal
- Quantity
- Date
- Time
- DateTime
FHIR Protos get implicitly converted to the above types according to this chart, when used in some FHIRPath expressions.
FHIRPath is not the most intuitive language, and there are some quirks. See gotchas.