Skip to content

Typed Evaluation

The typed evaluation functions wrap EvaluateCached and convert the result to a specific Go type. They simplify common patterns like checking existence, counting results, or extracting a single string or boolean value.

All of these functions use the DefaultCache internally, so repeated calls with the same expression benefit from automatic caching.

EvaluateToBoolean

Evaluates a FHIRPath expression and returns the result as a Go bool. Returns false if the result is empty. Returns an error if the result contains more than one value or if the single value is not a Boolean.

func EvaluateToBoolean(resource []byte, expr string) (bool, error)

Parameters:

NameTypeDescription
resource[]byteRaw JSON bytes of a FHIR® resource
exprstringA FHIRPath expression that should yield a single Boolean

Returns:

TypeDescription
boolThe boolean result, or false if the result is empty
errorNon-nil on compilation/evaluation errors, multiple results, or non-Boolean result

Example:

patient := []byte(`{
    "resourceType": "Patient",
    "active": true,
    "name": [{"family": "Smith"}]
}`)

// Check a boolean field
active, err := fhirpath.EvaluateToBoolean(patient, "Patient.active")
if err != nil {
    log.Fatal(err)
}
fmt.Println(active) // true

// Boolean expressions also work
hasName, err := fhirpath.EvaluateToBoolean(patient, "Patient.name.exists()")
if err != nil {
    log.Fatal(err)
}
fmt.Println(hasName) // true

EvaluateToString

Evaluates a FHIRPath expression and returns the result as a Go string. Returns an empty string if the result is empty. If the single result is a types.String, its raw value is returned; otherwise, the value’s String() representation is used. Returns an error if the result contains more than one value.

func EvaluateToString(resource []byte, expr string) (string, error)

Parameters:

NameTypeDescription
resource[]byteRaw JSON bytes of a FHIR® resource
exprstringA FHIRPath expression that should yield a single value

Returns:

TypeDescription
stringThe string result, or "" if the result is empty
errorNon-nil on compilation/evaluation errors, or if the result has more than one value

Example:

patient := []byte(`{
    "resourceType": "Patient",
    "name": [{"family": "Johnson", "given": ["Alice"]}],
    "birthDate": "1985-03-22"
}`)

family, err := fhirpath.EvaluateToString(patient, "Patient.name.first().family")
if err != nil {
    log.Fatal(err)
}
fmt.Println(family) // Johnson

birthDate, err := fhirpath.EvaluateToString(patient, "Patient.birthDate")
if err != nil {
    log.Fatal(err)
}
fmt.Println(birthDate) // 1985-03-22

EvaluateToStrings

Evaluates a FHIRPath expression and returns all results as a []string. Each element is converted to its string representation. Unlike EvaluateToString, this function handles collections of any size.

func EvaluateToStrings(resource []byte, expr string) ([]string, error)

Parameters:

NameTypeDescription
resource[]byteRaw JSON bytes of a FHIR® resource
exprstringA FHIRPath expression

Returns:

TypeDescription
[]stringAll result values as strings
errorNon-nil on compilation or evaluation errors

Example:

patient := []byte(`{
    "resourceType": "Patient",
    "name": [
        {"family": "Williams", "given": ["Robert", "James"]},
        {"family": "Bill", "given": ["Bob"]}
    ]
}`)

// Get all given names across all name entries
names, err := fhirpath.EvaluateToStrings(patient, "Patient.name.given")
if err != nil {
    log.Fatal(err)
}
fmt.Println(names) // [Robert James Bob]

Exists

Evaluates a FHIRPath expression and returns true if the result collection is non-empty. This is equivalent to calling Evaluate and checking !result.Empty(), but more concise.

func Exists(resource []byte, expr string) (bool, error)

Parameters:

NameTypeDescription
resource[]byteRaw JSON bytes of a FHIR® resource
exprstringA FHIRPath expression

Returns:

TypeDescription
booltrue if at least one result exists
errorNon-nil on compilation or evaluation errors

Example:

patient := []byte(`{
    "resourceType": "Patient",
    "telecom": [
        {"system": "phone", "value": "555-0100"}
    ]
}`)

hasPhone, err := fhirpath.Exists(patient, "Patient.telecom.where(system = 'phone')")
if err != nil {
    log.Fatal(err)
}
fmt.Println(hasPhone) // true

hasEmail, err := fhirpath.Exists(patient, "Patient.telecom.where(system = 'email')")
if err != nil {
    log.Fatal(err)
}
fmt.Println(hasEmail) // false

Count

Evaluates a FHIRPath expression and returns the number of values in the result collection.

func Count(resource []byte, expr string) (int, error)

Parameters:

NameTypeDescription
resource[]byteRaw JSON bytes of a FHIR® resource
exprstringA FHIRPath expression

Returns:

TypeDescription
intThe number of result values
errorNon-nil on compilation or evaluation errors

Example:

patient := []byte(`{
    "resourceType": "Patient",
    "name": [
        {"family": "Smith", "given": ["John", "Jacob"]},
        {"family": "Doe"}
    ],
    "address": [
        {"city": "Springfield"},
        {"city": "Shelbyville"},
        {"city": "Capital City"}
    ]
}`)

nameCount, err := fhirpath.Count(patient, "Patient.name")
if err != nil {
    log.Fatal(err)
}
fmt.Println(nameCount) // 2

addressCount, err := fhirpath.Count(patient, "Patient.address")
if err != nil {
    log.Fatal(err)
}
fmt.Println(addressCount) // 3

Summary

FunctionReturn TypeEmpty ResultMultiple ResultsCaching
EvaluateToBooleanboolfalseErrorYes
EvaluateToStringstring""ErrorYes
EvaluateToStrings[]string[]string{}All convertedYes
ExistsboolfalsetrueYes
Countint0Returns countYes

All functions use EvaluateCached internally, so the first call for a given expression incurs compilation cost, and all subsequent calls are served from the DefaultCache.

Practical Patterns

Validation with EvaluateToBoolean

func validatePatient(resource []byte) error {
    // Check required fields
    hasName, err := fhirpath.EvaluateToBoolean(resource, "Patient.name.exists()")
    if err != nil {
        return fmt.Errorf("validation error: %w", err)
    }
    if !hasName {
        return fmt.Errorf("Patient must have at least one name")
    }
    return nil
}

Extracting Lists with EvaluateToStrings

func getAllIdentifiers(resource []byte) ([]string, error) {
    return fhirpath.EvaluateToStrings(resource, "Patient.identifier.value")
}

Conditional Logic with Exists

func isDeceased(resource []byte) (bool, error) {
    return fhirpath.Exists(resource, "Patient.deceased.where($this = true)")
}
Last updated on