/fhirpath-go

Go implementation of the FHIRPath specification, implemented directly with the google/fhir proto definitions.

Primary LanguageGoBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

FHIRPath

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:

Import

import "github.com/verily-src/fhirpath-go/fhirpath"

Usage

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.

CompileOptions and EvaluateOptions

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

To add a custom function

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))

To add external constants

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))

System Types

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.

Things to be aware of

FHIRPath is not the most intuitive language, and there are some quirks. See gotchas.