fhirpath-go

module
v0.0.0-...-5b88717 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2024 License: BSD-3-Clause

README

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/fhir-fli/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.

Directories

Path Synopsis
Package fhir provides a library for working with R4 Google FHIR protos: https://github.com/google/fhir.
Package fhir provides a library for working with R4 Google FHIR protos: https://github.com/google/fhir.
Package fhirpath implements the FHIRPath specification.
Package fhirpath implements the FHIRPath specification.
compopts
Package compopts provides CompileOption values for FHIRPath.
Package compopts provides CompileOption values for FHIRPath.
evalopts
Package evalopts provides EvaluateOption values for FHIRPath.
Package evalopts provides EvaluateOption values for FHIRPath.
fhirpathtest
Package fhirpathtest provides an easy way to generate test-doubles within FHIRPath.
Package fhirpathtest provides an easy way to generate test-doubles within FHIRPath.
internal/compile
Package compile provides helpers for compiling FHIRPath strings into FHIRPath expressions.
Package compile provides helpers for compiling FHIRPath strings into FHIRPath expressions.
internal/expr
Package expr contains all the expression types and related logic for FHIRPath expressions.
Package expr contains all the expression types and related logic for FHIRPath expressions.
internal/expr/exprtest
Package exprtest provides some useful test dummies to make it easier to mock expressions to return a desired result.
Package exprtest provides some useful test dummies to make it easier to mock expressions to return a desired result.
internal/funcs
Package funcs provides the implementations for all base FHIRPath functions.
Package funcs provides the implementations for all base FHIRPath functions.
internal/opts
Package opts is an internal package that exists for setting configuration settings for FHIRPath.
Package opts is an internal package that exists for setting configuration settings for FHIRPath.
internal/parser
Package parser provides the logic for traversing the ANTLR generated parse tree.
Package parser provides the logic for traversing the ANTLR generated parse tree.
internal/reflection
Package reflection provides types and utility functions to enable FHIRPath type reflection.
Package reflection provides types and utility functions to enable FHIRPath type reflection.
patch
Package patch implements the FHIRPath Patch specification.
Package patch implements the FHIRPath Patch specification.
system
Package system provides types and related utility functions for all the valid FHIRPath System types to be used for literals.
Package system provides types and related utility functions for all the valid FHIRPath System types to be used for literals.
internal
bundle
Package bundle provides utilities for working with FHIR R4 Bundle proto definitions.
Package bundle provides utilities for working with FHIR R4 Bundle proto definitions.
element
Package element defines subpackages focused on FHIR R4 elements.
Package element defines subpackages focused on FHIR R4 elements.
element/identifier
Package identifier provides utilities for constructing and working with FHIR R4 Identifier elements.
Package identifier provides utilities for constructing and working with FHIR R4 Identifier elements.
fhirconv
Package fhirconv provides conversion utilities to Go-native types from FHIR R4 Elements.
Package fhirconv provides conversion utilities to Go-native types from FHIR R4 Elements.
fhirtest
Package fhirtest provides resource test dummies and useful utilities to enable better testing of the R4 FHIR Protos.
Package fhirtest provides resource test dummies and useful utilities to enable better testing of the R4 FHIR Protos.
narrow
Package narrow provides conversion functionality for narrowing integer types in a safe and generic manner.
Package narrow provides conversion functionality for narrowing integer types in a safe and generic manner.
resource
Package resource contains utilities for working with abstract FHIR Resource objects.
Package resource contains utilities for working with abstract FHIR Resource objects.
resourceopt
Package resourceopt is an internal package that provides helper utilities for forming resource-options in resource packages.
Package resourceopt is an internal package that provides helper utilities for forming resource-options in resource packages.
slices
Package slices provides helpful functions for searching, sorting and manipulating slices.
Package slices provides helpful functions for searching, sorting and manipulating slices.
stablerand
Package stablerand is a small helper utility that encapsulates its random engine and always uses the same seed value for its randomness.
Package stablerand is a small helper utility that encapsulates its random engine and always uses the same seed value for its randomness.
units
Package units provides basic unit constants that are used for various FHIR Quantity types.
Package units provides basic unit constants that are used for various FHIR Quantity types.
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL