types

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: MIT Imports: 14 Imported by: 5

Documentation

Overview

Package types defines the FHIRPath type system.

Index

Constants

View Source
const (
	TypeNameBoolean  = "Boolean"
	TypeNameString   = "String"
	TypeNameInteger  = "Integer"
	TypeNameDecimal  = "Decimal"
	TypeNameDate     = "Date"
	TypeNameDateTime = "DateTime"
	TypeNameTime     = "Time"
	TypeNameQuantity = "Quantity"
)

The names of the System types, as FHIRPath spells them.

These are the values Type() returns and the names is, as and ofType() match against, so they are shared rather than written out at each site: the engine compares them across three packages, and a disagreement about one of them would surface as a type test that silently never matches.

FHIR primitives are deliberately not here. FHIR.boolean is a distinct type from System.Boolean and is spelled in lower camel case for exactly that reason; the mapping between the two lives in the evaluator, which is where the distinction is decided.

View Source
const DefaultQuantityUnit = "1"

DefaultQuantityUnit is the unit a quantity carries when its source gave none.

The specification calls it "the UCUM default unit" and writes it as '1': 42.toQuantity() is 42 '1'. It matters that the unit is present rather than blank, because '1' is dimensionless and so not convertible to a meter — which is why 45.toQuantity('m') is empty rather than 45 'm'.

View Source
const UCUMSystem = "http://unitsofmeasure.org"

UCUMSystem identifies UCUM as the code system of a FHIR Quantity.

Variables

View Source
var EmptyCollection = Collection{}

EmptyCollection is a shared empty collection to avoid allocations.

View Source
var ErrCalendarConversionRequired = errors.New("UCUM year and month durations require explicit conversion to calendar units")

ErrCalendarConversionRequired reports that a UCUM year or month was used to shift a date or time.

A UCUM year is a fixed span of 365.25 days and a UCUM month a fixed 30.44, while adding a calendar year or month moves to the same date in another year or month. The FHIRPath specification keeps the two systems apart above seconds and requires an explicit conversion to cross between them, so @1973-12-25 + 1 'a' does not silently pick one meaning.

View Source
var ErrDateComponentOnTime = errors.New("a date component cannot shift a time")

ErrDateComponentOnTime reports an attempt to shift a time of day by a unit that measures the calendar.

A time carries no date, so there is nothing for a day or a month to move. "If there is more than one item, an item of an incompatible type, or an unsupported unit for the type, the evaluation of the expression will end and signal an error ... This includes attempting to add date components to a Time."

View Source
var ErrIncompatibleUnits = errors.New("incompatible units")

ErrIncompatibleUnits reports that two quantities are not commensurable, so no conversion between their units exists. Per the FHIRPath spec, "attempting to operate on quantities with invalid units will result in empty ({})", so callers translate this sentinel into an empty collection instead of failing the whole expression.

View Source
var ErrPrecisionMismatch = errors.New("temporal values are specified to different precisions")

ErrPrecisionMismatch reports that two temporal values agree on every precision they have in common, but one is specified more precisely than the other, so the outcome cannot be determined.

The FHIRPath specification requires the operation to yield empty in this case: "If one value is specified to a different level of precision than the other, the result is empty ({ }) to indicate that the result of the comparison is unknown." Callers translate this sentinel into an empty collection.

Order matters: the comparison walks precisions from the most significant down and stops at the first difference, so this is only reached when everything shared matches. now() > today() is empty, while now() > @1974-12-25 is true, decided at the year.

View Source
var ErrUnsupportedPrecision = errors.New("precision is not valid for this temporal type")

ErrUnsupportedPrecision reports that the precision named in a difference() or duration() call is not one the input's type admits.

The specification fixes the permitted set per type: a date takes year, month, week or day; a datetime adds hour, minute, second and millisecond; a time takes only the four time-of-day precisions. Asking a date for the number of hours crossed is not an unknown result — it is a nonsensical request, so it is an error rather than empty.

View Source
var FalseCollection = Collection{falseBoolean}

FalseCollection is a cached collection containing false.

View Source
var TrueCollection = Collection{trueBoolean}

TrueCollection is a cached collection containing true.

Functions

func CalendarUnitForUCUMCode added in v1.5.0

func CalendarUnitForUCUMCode(code string) (string, bool)

CalendarUnitForUCUMCode returns the calendar duration keyword FHIR maps a time-valued UCUM code onto, and whether the code is one it maps.

This is what lets a Quantity read from FHIR data take part in date arithmetic at all: a Quantity of 1 'a' would be refused, since a UCUM year is a definite 365.25 days and cannot be added to a calendar, while the 1 year it maps to is exactly what the calendar can add.

func EqualTemporal added in v1.5.0

func EqualTemporal(left, right Value) (bool, error)

EqualTemporal reports whether two temporal values are equal, under the same precision rule as ordering: the comparison proceeds precision by precision and stops at the first difference, so equality is unknown — and the operator yields empty — only when the values match on everything they share while one is specified more precisely.

@2012-01 = @2012           // empty, different precision
@2012-01 = @2012-02        // false, decided at the month
@T10:30:00 = @T10:30:00.0  // true, seconds and milliseconds are one precision

Returns ErrPrecisionMismatch for the unknown case, and an error when the values are not comparable temporals at all.

func IsSystemTypeName added in v1.5.0

func IsSystemTypeName(name string) bool

IsSystemTypeName reports whether the name is one of the System types FHIRPath declares in its Literals section.

This is the type system of the language rather than a FHIR version-specific list, so it does not go stale: any other type name on a primitive value came from the FHIR model (code, uri, id, markdown, ...).

func IsTemporal added in v1.5.0

func IsTemporal(v Value) bool

IsTemporal reports whether a value is a Date, DateTime or Time.

func PutCollection

func PutCollection(c *Collection)

PutCollection returns a Collection to the pool for reuse. The collection is reset to length 0.

func SecondUnitMilliseconds added in v1.5.0

func SecondUnitMilliseconds(value decimal.Decimal, unit string) (int, bool)

SecondUnitMilliseconds converts a quantity given in seconds to whole milliseconds, reporting false when the unit is not a second.

This is the one place a duration's fractional part survives: "The decimal portion of the time-valued quantity is only applied for second or millisecond precisions; for all other precisions, the decimal portion is ignored, since date/time arithmetic is performed with calendar duration semantics." So 0.1 's' shifts by 100 milliseconds, while 7.9 days shifts by seven.

func TemporalDifference added in v1.5.0

func TemporalDifference(from, to Value, precision string) (int64, error)

TemporalDifference returns the number of boundaries of the given precision crossed between two temporal values, negative when the input is the later of the two.

A boundary is a point at which the named component changes: the difference in weeks between two dates is the number of Sundays that fall after the first and on or before the second, which is why @2025-01-02.difference(@2025-01-07, 'week') is 1 even though only five days separate them.

Reports ErrPrecisionMismatch when either value is specified less precisely than the request, which callers translate to empty, and ErrUnsupportedPrecision when the precision does not apply to the type.

func TemporalDuration added in v1.5.0

func TemporalDuration(from, to Value, precision string) (int64, error)

TemporalDuration returns the number of whole periods of the given precision between two temporal values, negative when the input is the later of the two.

This is the "how long since" reading, distinct from the boundary count: @2025-01-01.duration(@2025-09-01, 'year') is 0 because the year has not elapsed, where difference() would report 0 as well but @2024-12-31.difference(@2025-01-01, 'year') is 1 and its duration is 0.

Reports the same errors as TemporalDifference.

Types

type Boolean

type Boolean struct {
	// contains filtered or unexported fields
}

Boolean represents a FHIRPath boolean value.

func GetBoolean

func GetBoolean(b bool) Boolean

GetBoolean returns a cached Boolean value.

func NewBoolean

func NewBoolean(v bool) Boolean

NewBoolean creates a new Boolean value.

func (Boolean) Bool

func (b Boolean) Bool() bool

Bool returns the underlying boolean value.

func (Boolean) Element added in v1.5.0

func (p Boolean) Element() *ObjectValue

Element returns the FHIR element a primitive was read with, or nil when the primitive stood alone in the JSON.

func (Boolean) Equal

func (b Boolean) Equal(other Value) bool

Equal returns true if other is a Boolean with the same value.

func (Boolean) Equivalent

func (b Boolean) Equivalent(other Value) bool

Equivalent is the same as Equal for booleans.

func (Boolean) HasElement added in v1.5.0

func (p Boolean) HasElement() bool

HasElement reports whether any element accompanied the value.

func (Boolean) IsEmpty

func (b Boolean) IsEmpty() bool

IsEmpty returns false for boolean values.

func (Boolean) Not

func (b Boolean) Not() Boolean

Not returns the logical negation.

func (Boolean) String

func (b Boolean) String() string

String returns "true" or "false".

func (Boolean) Type

func (b Boolean) Type() string

Type returns "Boolean".

func (Boolean) WithElement added in v1.5.0

func (b Boolean) WithElement(element *ObjectValue) Boolean

WithElement returns a copy carrying the FHIR element that accompanied the value in the JSON, which is where its extensions and id live.

func (Boolean) WithFHIRType added in v1.5.0

func (b Boolean) WithFHIRType(fhirType string) Boolean

WithFHIRType returns a copy that reports the FHIR type it was declared with. FHIR primitives are types in their own right — a FHIR.boolean is not a System.Boolean — so a value keeps the name the model gave it.

type Collection

type Collection []Value

Collection is an ordered sequence of FHIRPath values. It is the fundamental return type for all FHIRPath expressions.

func GetCollection

func GetCollection() *Collection

GetCollection returns a Collection from the pool. The returned collection has length 0 but may have capacity > 0.

func JSONToCollection

func JSONToCollection(data []byte) (Collection, error)

JSONToCollection converts JSON bytes to a Collection.

func NewCollectionWithCap

func NewCollectionWithCap(capacity int) Collection

NewCollectionWithCap creates a new Collection with the specified capacity. Use this when you know the expected size to avoid reallocations.

func SingletonCollection

func SingletonCollection(v Value) Collection

SingletonCollection creates a collection with a single value. This is a common operation that benefits from optimization.

func (Collection) AllFalse

func (c Collection) AllFalse() bool

AllFalse returns true if all items are boolean false.

func (Collection) AllTrue

func (c Collection) AllTrue() bool

AllTrue returns true if all items are boolean true.

func (Collection) AnyFalse

func (c Collection) AnyFalse() bool

AnyFalse returns true if any item is boolean false.

func (Collection) AnyTrue

func (c Collection) AnyTrue() bool

AnyTrue returns true if any item is boolean true.

func (Collection) Combine

func (c Collection) Combine(other Collection) Collection

Combine returns a new collection that combines c and other. Unlike Union, duplicates are preserved.

func (Collection) Contains

func (c Collection) Contains(v Value) bool

Contains returns true if the collection contains a value equal to v.

func (Collection) Count

func (c Collection) Count() int

Count returns the number of elements in the collection.

func (Collection) Distinct

func (c Collection) Distinct() Collection

Distinct returns a new collection with duplicate values removed. Preserves the order of first occurrence.

func (Collection) Empty

func (c Collection) Empty() bool

Empty returns true if the collection has no elements.

func (Collection) Exclude

func (c Collection) Exclude(other Collection) Collection

Exclude returns elements in c that are not in other.

func (Collection) First

func (c Collection) First() (Value, bool)

First returns the first element and true, or nil and false if empty.

func (Collection) Intersect

func (c Collection) Intersect(other Collection) Collection

Intersect returns elements that are in both collections.

func (Collection) IsDistinct

func (c Collection) IsDistinct() bool

IsDistinct returns true if all elements in the collection are unique.

func (Collection) Last

func (c Collection) Last() (Value, bool)

Last returns the last element and true, or nil and false if empty.

func (Collection) Single

func (c Collection) Single() (Value, error)

Single returns the single element if the collection has exactly one element. Returns an error if empty or has more than one element.

func (Collection) SingletonBoolean added in v1.5.0

func (c Collection) SingletonBoolean() (value, ok bool)

SingletonBoolean applies the FHIRPath "Singleton Evaluation of Collections" rule for operations that expect a Boolean input: a single Boolean node evaluates to its value, and a single node of any other type evaluates to true. FHIR invariants rely on this — age-1 opens with "(code or value.empty())", where code is a string.

ok is false when the collection is empty or holds more than one item; callers propagate empty in that case.

func (Collection) Skip

func (c Collection) Skip(n int) Collection

Skip returns a collection with the first n elements removed.

func (Collection) String

func (c Collection) String() string

String returns a string representation of the collection.

func (Collection) Tail

func (c Collection) Tail() Collection

Tail returns all elements except the first.

func (Collection) Take

func (c Collection) Take(n int) Collection

Take returns a collection with only the first n elements.

func (Collection) ToBoolean

func (c Collection) ToBoolean() (bool, error)

ToBoolean converts singleton collection to boolean. Returns error if not a singleton or not a boolean value.

func (Collection) Union

func (c Collection) Union(other Collection) Collection

Union returns a new collection that is the union of c and other. Duplicates are removed.

type Comparable

type Comparable interface {
	Value
	// Compare returns -1 if less than, 0 if equal, 1 if greater than.
	// Returns error if types are incompatible.
	Compare(other Value) (int, error)
}

Comparable is implemented by types that support ordering.

type Date

type Date struct {
	// contains filtered or unexported fields
}

Date represents a FHIRPath date value. Supports partial dates: year, year-month, year-month-day.

func NewDate

func NewDate(s string) (Date, error)

NewDate creates a Date from a string.

func NewDateFromTime

func NewDateFromTime(t time.Time) Date

NewDateFromTime creates a Date from a time.Time.

func (Date) AddDuration

func (d Date) AddDuration(value int, unit string) (Date, error)

AddDuration adds a duration (as Quantity with temporal unit) to the date. Supported units: year(s), month(s), week(s), day(s)

func (Date) Compare

func (d Date) Compare(other Value) (int, error)

Compare compares two dates. Returns -1, 0, or 1. Implements the Comparable interface. Returns empty (error) if precisions differ and comparison is ambiguous.

func (Date) Day

func (d Date) Day() int

Day returns the day component (0 if not specified).

func (Date) Element added in v1.5.0

func (p Date) Element() *ObjectValue

Element returns the FHIR element a primitive was read with, or nil when the primitive stood alone in the JSON.

func (Date) Equal

func (d Date) Equal(other Value) bool

Equal checks equality with another value.

func (Date) Equivalent

func (d Date) Equivalent(other Value) bool

Equivalent checks equivalence with another value.

func (Date) HasElement added in v1.5.0

func (p Date) HasElement() bool

HasElement reports whether any element accompanied the value.

func (Date) IsEmpty

func (d Date) IsEmpty() bool

IsEmpty returns false for Date.

func (Date) Month

func (d Date) Month() int

Month returns the month component (0 if not specified).

func (Date) Precision

func (d Date) Precision() DatePrecision

Precision returns the date precision.

func (Date) String

func (d Date) String() string

String returns the string representation.

func (Date) SubtractDuration

func (d Date) SubtractDuration(value int, unit string) (Date, error)

SubtractDuration subtracts a duration from the date.

func (Date) ToTime

func (d Date) ToTime() time.Time

ToTime converts to time.Time (uses defaults for missing components).

func (Date) Type

func (d Date) Type() string

Type returns the type name.

func (Date) WithElement added in v1.5.0

func (d Date) WithElement(element *ObjectValue) Date

WithElement returns a copy carrying the FHIR element that accompanied the value in the JSON, which is where its extensions and id live.

func (Date) WithFHIRType added in v1.5.0

func (d Date) WithFHIRType(fhirType string) Date

WithFHIRType returns a copy that reports the FHIR type it was declared with. FHIR primitives are types in their own right — a FHIR.boolean is not a System.Boolean — so a value keeps the name the model gave it.

func (Date) Year

func (d Date) Year() int

Year returns the year component.

type DatePrecision

type DatePrecision int

DatePrecision indicates the precision of a date.

const (
	YearPrecision DatePrecision = iota
	MonthPrecision
	DayPrecision
)

type DateTime

type DateTime struct {
	// contains filtered or unexported fields
}

DateTime represents a FHIRPath datetime value.

func NewDateTime

func NewDateTime(s string) (DateTime, error)

NewDateTime creates a DateTime from a string.

func NewDateTimeFromTime

func NewDateTimeFromTime(t time.Time) DateTime

NewDateTimeFromTime creates a DateTime from time.Time.

func (DateTime) AddDuration

func (dt DateTime) AddDuration(value int, unit string) (DateTime, error)

AddDuration adds a duration (as Quantity with temporal unit) to the datetime. Supported units: year(s), month(s), week(s), day(s), hour(s), minute(s), second(s), millisecond(s)

func (DateTime) Compare

func (dt DateTime) Compare(other Value) (int, error)

Compare compares two datetimes. Returns -1, 0, or 1. Implements the Comparable interface. Returns error if precisions differ and comparison is ambiguous.

func (DateTime) Day

func (dt DateTime) Day() int

func (DateTime) Element added in v1.5.0

func (p DateTime) Element() *ObjectValue

Element returns the FHIR element a primitive was read with, or nil when the primitive stood alone in the JSON.

func (DateTime) Equal

func (dt DateTime) Equal(other Value) bool

Equal checks equality with another value.

func (DateTime) Equivalent

func (dt DateTime) Equivalent(other Value) bool

Equivalent checks equivalence with another value.

func (DateTime) HasElement added in v1.5.0

func (p DateTime) HasElement() bool

HasElement reports whether any element accompanied the value.

func (DateTime) HasTZ added in v1.3.0

func (dt DateTime) HasTZ() bool

HasTZ returns whether the datetime has an explicit timezone.

func (DateTime) Hour

func (dt DateTime) Hour() int

func (DateTime) IsEmpty

func (dt DateTime) IsEmpty() bool

IsEmpty returns false for DateTime.

func (DateTime) Millisecond

func (dt DateTime) Millisecond() int

func (DateTime) Minute

func (dt DateTime) Minute() int

func (DateTime) Month

func (dt DateTime) Month() int

func (DateTime) Precision added in v1.3.0

func (dt DateTime) Precision() DateTimePrecision

Precision returns the datetime precision.

func (DateTime) Second

func (dt DateTime) Second() int

func (DateTime) String

func (dt DateTime) String() string

String returns the string representation.

func (DateTime) SubtractDuration

func (dt DateTime) SubtractDuration(value int, unit string) (DateTime, error)

SubtractDuration subtracts a duration from the datetime.

func (DateTime) TZOffset added in v1.3.0

func (dt DateTime) TZOffset() int

TZOffset returns the timezone offset in minutes.

func (DateTime) ToTime

func (dt DateTime) ToTime() time.Time

ToTime converts to time.Time.

func (DateTime) Type

func (dt DateTime) Type() string

Type returns the type name.

func (DateTime) WithElement added in v1.5.0

func (dt DateTime) WithElement(element *ObjectValue) DateTime

WithElement returns a copy carrying the FHIR element that accompanied the value in the JSON, which is where its extensions and id live.

func (DateTime) WithFHIRType added in v1.5.0

func (dt DateTime) WithFHIRType(fhirType string) DateTime

WithFHIRType returns a copy that reports the FHIR type it was declared with. FHIR primitives are types in their own right — a FHIR.boolean is not a System.Boolean — so a value keeps the name the model gave it.

func (DateTime) Year

func (dt DateTime) Year() int

Accessors

type DateTimePrecision

type DateTimePrecision int

DateTimePrecision indicates the precision of a datetime.

const (
	DTYearPrecision DateTimePrecision = iota
	DTMonthPrecision
	DTDayPrecision
	DTHourPrecision
	DTMinutePrecision
	DTSecondPrecision
	DTMillisPrecision
)

type Decimal

type Decimal struct {
	// contains filtered or unexported fields
}

Decimal represents a FHIRPath decimal value with arbitrary precision.

func MustDecimal

func MustDecimal(s string) Decimal

MustDecimal creates a new Decimal, panicking on error.

func NewDecimal

func NewDecimal(s string) (Decimal, error)

NewDecimal creates a new Decimal from a string.

func NewDecimalFromDecimal added in v1.5.0

func NewDecimalFromDecimal(v decimal.Decimal) Decimal

NewDecimalFromDecimal wraps an already-exact decimal value.

func NewDecimalFromFloat

func NewDecimalFromFloat(v float64) Decimal

NewDecimalFromFloat creates a new Decimal from a float64.

func NewDecimalFromInt

func NewDecimalFromInt(v int64) Decimal

NewDecimalFromInt creates a new Decimal from an int64.

func (Decimal) Abs

func (d Decimal) Abs() Decimal

Abs returns the absolute value.

func (Decimal) Add

func (d Decimal) Add(other Decimal) Decimal

Add returns the sum of two decimals.

func (Decimal) Ceiling

func (d Decimal) Ceiling() Integer

Ceiling returns the smallest integer >= d.

func (Decimal) Compare

func (d Decimal) Compare(other Value) (int, error)

Compare compares two numeric values.

func (Decimal) Divide

func (d Decimal) Divide(other Decimal) (Decimal, error)

Divide returns the result of division.

func (Decimal) Element added in v1.5.0

func (p Decimal) Element() *ObjectValue

Element returns the FHIR element a primitive was read with, or nil when the primitive stood alone in the JSON.

func (Decimal) Equal

func (d Decimal) Equal(other Value) bool

Equal returns true if other is numerically equal.

func (Decimal) Equivalent

func (d Decimal) Equivalent(other Value) bool

Equivalent compares two decimals at the precision of the less precise one.

"Decimal: values must be equal, comparison is done on values rounded to the precision of the least precise operand. Trailing zeroes after the decimal are ignored in determining precision."

This is what separates ~ from =: 1.2 / 1.8 is 0.666..., which equals nothing, but is equivalent to 0.67 because 0.67 is given to two places and the quotient rounded to two places is 0.67.

func (Decimal) Exp

func (d Decimal) Exp() Decimal

Exp returns e^d.

func (Decimal) Floor

func (d Decimal) Floor() Integer

Floor returns the largest integer <= d.

func (Decimal) HasElement added in v1.5.0

func (p Decimal) HasElement() bool

HasElement reports whether any element accompanied the value.

func (Decimal) ImplicitPrecision added in v1.4.0

func (d Decimal) ImplicitPrecision() int

ImplicitPrecision returns the number of decimal places in this value, inferred from the original string representation. For example, "1.0" has precision 1, "1.00" has precision 2, and "42" has precision 0. Returns 0 for computed values with no original string.

func (Decimal) IsEmpty

func (d Decimal) IsEmpty() bool

IsEmpty returns false for decimal values.

func (Decimal) IsInteger

func (d Decimal) IsInteger() bool

IsInteger returns true if the decimal has no fractional part.

func (Decimal) Ln

func (d Decimal) Ln() (Decimal, error)

Ln returns the natural logarithm.

func (Decimal) Log

func (d Decimal) Log(base Decimal) (Decimal, error)

Log returns the logarithm with the given base.

func (Decimal) Multiply

func (d Decimal) Multiply(other Decimal) Decimal

Multiply returns the product of two decimals.

func (Decimal) Negate

func (d Decimal) Negate() Decimal

Negate returns the negation of the decimal.

func (Decimal) Power

func (d Decimal) Power(exp Decimal) Decimal

Power returns d raised to the given power.

func (Decimal) Round

func (d Decimal) Round(precision int32) Decimal

Round rounds to the given precision.

func (Decimal) Sqrt

func (d Decimal) Sqrt() (Decimal, error)

Sqrt returns the square root.

func (Decimal) String

func (d Decimal) String() string

String returns the decimal string representation. For values created from string parsing, preserves the original representation (e.g., "1.0" stays "1.0"). For computed values, uses the default shopspring representation.

func (Decimal) Subtract

func (d Decimal) Subtract(other Decimal) Decimal

Subtract returns the difference of two decimals.

func (Decimal) ToDecimal

func (d Decimal) ToDecimal() Decimal

ToDecimal returns itself (implements Numeric interface).

func (Decimal) ToInteger

func (d Decimal) ToInteger() (Integer, bool)

ToInteger converts to Integer if it's a whole number.

func (Decimal) Truncate

func (d Decimal) Truncate() Integer

Truncate returns the integer part.

func (Decimal) Type

func (d Decimal) Type() string

Type returns "Decimal".

func (Decimal) Value

func (d Decimal) Value() decimal.Decimal

Value returns the underlying decimal.Decimal value.

func (Decimal) WithElement added in v1.5.0

func (d Decimal) WithElement(element *ObjectValue) Decimal

WithElement returns a copy carrying the FHIR element that accompanied the value in the JSON, which is where its extensions and id live.

func (Decimal) WithFHIRType added in v1.5.0

func (d Decimal) WithFHIRType(fhirType string) Decimal

WithFHIRType returns a copy that reports the FHIR type it was declared with. FHIR primitives are types in their own right — a FHIR.boolean is not a System.Boolean — so a value keeps the name the model gave it.

type ElementCarrier added in v1.5.0

type ElementCarrier interface {
	Element() *ObjectValue
	HasElement() bool
}

ElementCarrier is implemented by the primitive types, which may carry the FHIR element their JSON representation keeps beside the value.

Declared as an interface so that extension() and friends can ask any value for its element without knowing which primitive it is.

type ElementTypeResolver added in v1.5.0

type ElementTypeResolver interface {
	TypeOf(path string) string
}

ElementTypeResolver resolves a FHIR element path to its type, e.g. "Observation.subject" to "Reference". It is the single slice of the engine's FHIR model that type-aware child navigation needs, declared here so that this package stays independent of the evaluator.

type Integer

type Integer struct {
	// contains filtered or unexported fields
}

Integer represents a FHIRPath integer value.

func GetInteger

func GetInteger(n int64) Integer

GetInteger returns a cached Integer for values in range [-128, 127]. For other values, creates a new Integer.

func NewInteger

func NewInteger(v int64) Integer

NewInteger creates a new Integer value.

func (Integer) Abs

func (i Integer) Abs() Integer

Abs returns the absolute value.

func (Integer) Add

func (i Integer) Add(other Integer) Integer

Add returns the sum of two integers.

func (Integer) Compare

func (i Integer) Compare(other Value) (int, error)

Compare compares two numeric values.

func (Integer) Div

func (i Integer) Div(other Integer) (Integer, error)

Div returns the integer division result.

func (Integer) Divide

func (i Integer) Divide(other Integer) (Decimal, error)

Divide returns the result of division as a Decimal.

func (Integer) Element added in v1.5.0

func (p Integer) Element() *ObjectValue

Element returns the FHIR element a primitive was read with, or nil when the primitive stood alone in the JSON.

func (Integer) Equal

func (i Integer) Equal(other Value) bool

Equal returns true if other is an Integer with the same value, or a Decimal with an equivalent integer value.

func (Integer) Equivalent

func (i Integer) Equivalent(other Value) bool

Equivalent is the same as Equal for integers.

func (Integer) HasElement added in v1.5.0

func (p Integer) HasElement() bool

HasElement reports whether any element accompanied the value.

func (Integer) IsEmpty

func (i Integer) IsEmpty() bool

IsEmpty returns false for integer values.

func (Integer) Mod

func (i Integer) Mod(other Integer) (Integer, error)

Mod returns the modulo result.

func (Integer) Multiply

func (i Integer) Multiply(other Integer) Integer

Multiply returns the product of two integers.

func (Integer) Negate

func (i Integer) Negate() Integer

Negate returns the negation of the integer.

func (Integer) Power

func (i Integer) Power(exp Integer) Decimal

Power returns the integer raised to the given power.

func (Integer) Sqrt

func (i Integer) Sqrt() (Decimal, error)

Sqrt returns the square root as a Decimal.

func (Integer) String

func (i Integer) String() string

String returns the decimal string representation.

func (Integer) Subtract

func (i Integer) Subtract(other Integer) Integer

Subtract returns the difference of two integers.

func (Integer) ToDecimal

func (i Integer) ToDecimal() Decimal

ToDecimal converts the integer to a Decimal.

func (Integer) Type

func (i Integer) Type() string

Type returns "Integer".

func (Integer) Value

func (i Integer) Value() int64

Value returns the underlying int64 value.

func (Integer) WithElement added in v1.5.0

func (i Integer) WithElement(element *ObjectValue) Integer

WithElement returns a copy carrying the FHIR element that accompanied the value in the JSON, which is where its extensions and id live.

func (Integer) WithFHIRType added in v1.5.0

func (i Integer) WithFHIRType(fhirType string) Integer

WithFHIRType returns a copy that reports the FHIR type it was declared with. FHIR primitives are types in their own right — a FHIR.boolean is not a System.Boolean — so a value keeps the name the model gave it.

type Numeric

type Numeric interface {
	Value
	// ToDecimal converts the numeric to a Decimal.
	ToDecimal() Decimal
}

Numeric is implemented by numeric types (Integer, Decimal).

type ObjectValue

type ObjectValue struct {
	// contains filtered or unexported fields
}

ObjectValue represents a FHIR resource or complex type as a JSON object.

func ElementOf added in v1.5.0

func ElementOf(value Value) (*ObjectValue, bool)

ElementOf returns the FHIR element accompanying a value, and whether there is one. An ObjectValue is its own element: a complex type keeps its extensions in the same object as the rest of its fields.

func NewObjectValue

func NewObjectValue(data []byte) *ObjectValue

NewObjectValue creates a new ObjectValue from JSON bytes.

func NewObjectValueWithType added in v1.3.1

func NewObjectValueWithType(data []byte, typeName string) *ObjectValue

NewObjectValueWithType creates a new ObjectValue with an explicit FHIR type. Used when the type is known from polymorphic field resolution (e.g., valueQuantity → "Quantity").

func (*ObjectValue) Children

func (o *ObjectValue) Children() Collection

Children returns a collection of all child values.

func (*ObjectValue) Data

func (o *ObjectValue) Data() []byte

Data returns the raw JSON data.

func (*ObjectValue) Equal

func (o *ObjectValue) Equal(other Value) bool

Equal returns true if the JSON data is identical.

func (*ObjectValue) Equivalent

func (o *ObjectValue) Equivalent(other Value) bool

Equivalent is the same as Equal for objects.

func (*ObjectValue) Get

func (o *ObjectValue) Get(field string) (Value, bool)

Get retrieves a field value, caching the result.

func (*ObjectValue) GetCollection

func (o *ObjectValue) GetCollection(field string) Collection

GetCollection retrieves a field as a Collection. If the field is an array, returns all elements. If the field is a single value, returns a singleton collection.

func (*ObjectValue) GetCollectionParsedAs added in v1.5.0

func (o *ObjectValue) GetCollectionParsedAs(field, suffix string) Collection

GetCollectionParsedAs retrieves a field as a Collection from a type read off a polymorphic field name, such as the Oid in valueOid.

Such a name is capitalized to form the field, while FHIR writes primitive type names in lower camel case and complex ones capitalized. The value itself says which it is — a primitive parses to a primitive — so the recorded type is corrected accordingly rather than kept in the field's spelling.

func (*ObjectValue) GetCollectionWithType added in v1.3.0

func (o *ObjectValue) GetCollectionWithType(field, fhirType string) Collection

GetCollectionWithType retrieves a field as a Collection, using the FHIR type hint to properly parse string values as Date, DateTime, Time, etc.

func (*ObjectValue) IsEmpty

func (o *ObjectValue) IsEmpty() bool

IsEmpty returns false for object values.

func (*ObjectValue) Keys

func (o *ObjectValue) Keys() []string

Keys returns all field names in the object.

func (*ObjectValue) String

func (o *ObjectValue) String() string

String returns the JSON representation.

func (*ObjectValue) ToQuantity

func (o *ObjectValue) ToQuantity() (Quantity, bool)

ToQuantity attempts to convert an ObjectValue to a Quantity. This is used when the object represents a FHIR Quantity type (with fields like "value", "unit", "code", "system"). Returns the Quantity and true if successful, or zero Quantity and false if not.

func (*ObjectValue) Type

func (o *ObjectValue) Type() string

Type returns the FHIR type of this object. Checks explicit type (from polymorphic resolution), then resourceType, then infers from structure.

func (*ObjectValue) TypedChildren added in v1.5.0

func (o *ObjectValue) TypedChildren(basePath string, res ElementTypeResolver) []TypedChild

TypedChildren returns the object's children with their FHIR types resolved through res, which makes the model — not structural inference — decide what each child is. A child whose type the model does not know falls back to inference, exactly like ObjectValue.Children.

basePath is this object's FHIR path (e.g. "Observation.component"); it may be empty, in which case only the object's own type is used to resolve children.

type Quantity

type Quantity struct {
	// contains filtered or unexported fields
}

Quantity represents a FHIRPath quantity value with a numeric value and unit.

func NewQuantity

func NewQuantity(s string) (Quantity, error)

NewQuantity creates a Quantity from a string.

func NewQuantityFromDecimal

func NewQuantityFromDecimal(value decimal.Decimal, unit string) Quantity

NewQuantityFromDecimal creates a Quantity from a decimal value and unit.

func ParseQuantityString added in v1.5.0

func ParseQuantityString(s string) (Quantity, bool)

ParseQuantityString converts a string to a Quantity under the rule toQuantity() states, and reports whether the string is convertible at all.

The distinction the pattern draws is between a quoted unit and a bare word. A quoted unit is a UCUM code and is taken as written. A bare word is a calendar duration keyword, so it has to be one — '1 wk' does not convert, because wk is a UCUM code that was written without its quotes, while '4 days' does.

func (Quantity) Abs added in v1.5.0

func (q Quantity) Abs() Quantity

Abs returns the quantity with a non-negative value, keeping the unit.

func (Quantity) Add

func (q Quantity) Add(other Quantity) (Quantity, error)

Add adds two quantities. Commensurable units are converted into the left operand's unit, which is also the unit of the result: 1 'g' + 500 'mg' is 1.5 'g'.

func (Quantity) Comparable added in v1.5.0

func (q Quantity) Comparable(other Quantity) bool

Comparable reports whether the two quantities can be compared, that is whether their units are commensurable. Quantities sharing a unit always are; otherwise both units must reduce to the same canonical unit.

func (Quantity) Compare

func (q Quantity) Compare(other Value) (int, error)

Compare compares two quantities. Returns -1, 0, or 1 if units are compatible, or error if not. Uses UCUM normalization to compare quantities with different but compatible units. Implements the Comparable interface.

func (Quantity) ConvertTo added in v1.5.0

func (q Quantity) ConvertTo(unit string) (Quantity, bool)

ConvertTo restates a quantity in another unit, reporting false when the units are not commensurable — which the specification treats as an empty result rather than an error: "24 'm'.toQuantity('kg') // empty".

func (Quantity) Divide

func (q Quantity) Divide(divisor decimal.Decimal) (Quantity, error)

Divide divides the quantity by a number.

func (Quantity) DivideQuantity added in v1.5.0

func (q Quantity) DivideQuantity(other Quantity) (Quantity, error)

DivideQuantity divides two quantities, combining their units: 4 'g' by 2 'm' is 2 'g.m-1'.

func (Quantity) Equal

func (q Quantity) Equal(other Value) bool

Equal checks equality with another value. For quantities with different units, uses UCUM normalization per FHIRPath spec.

func (Quantity) Equivalent

func (q Quantity) Equivalent(other Value) bool

Equivalent checks equivalence with another value. For quantities, this uses UCUM normalization to compare values with different units. Per FHIRPath spec: quantities are equivalent if their canonical normalized forms are equal.

func (Quantity) IsEmpty

func (q Quantity) IsEmpty() bool

IsEmpty returns false for Quantity.

func (Quantity) Multiply

func (q Quantity) Multiply(factor decimal.Decimal) Quantity

Multiply multiplies the quantity by a number.

func (Quantity) MultiplyQuantity added in v1.5.0

func (q Quantity) MultiplyQuantity(other Quantity) (Quantity, error)

MultiplyQuantity multiplies two quantities, combining their units: 2 'cm' by 2 'm' is 0.04 'm2'.

func (Quantity) Negate added in v1.5.0

func (q Quantity) Negate() Quantity

Negate returns the quantity with its value's sign flipped, keeping the unit: a negative mass is still a mass.

func (Quantity) String

func (q Quantity) String() string

String returns the string representation. String returns the quantity in FHIRPath literal notation. A UCUM unit is quoted — 1 'wk' — while a calendar duration keyword is not — 1 week — which is how the grammar distinguishes the two.

func (Quantity) Subtract

func (q Quantity) Subtract(other Quantity) (Quantity, error)

Subtract subtracts two quantities. Units are handled as in Quantity.Add.

func (Quantity) Type

func (q Quantity) Type() string

Type returns the type name.

func (Quantity) Unit

func (q Quantity) Unit() string

Unit returns the unit string.

func (Quantity) Value

func (q Quantity) Value() decimal.Decimal

Value returns the numeric value.

type String

type String struct {
	// contains filtered or unexported fields
}

String represents a FHIRPath string value.

func NewString

func NewString(v string) String

NewString creates a new String value.

func NewStringWithFHIRType added in v1.3.1

func NewStringWithFHIRType(v, fhirType string) String

NewStringWithFHIRType creates a new String value with an explicit FHIR type. Used for URI subtypes (id, oid, url, uuid, code, etc.) to preserve type identity so that ofType() can discriminate between them.

func (String) Compare

func (s String) Compare(other Value) (int, error)

Compare compares two strings lexicographically.

func (String) Contains

func (s String) Contains(substr string) bool

Contains returns true if the string contains the substring.

func (String) Element added in v1.5.0

func (p String) Element() *ObjectValue

Element returns the FHIR element a primitive was read with, or nil when the primitive stood alone in the JSON.

func (String) EndsWith

func (s String) EndsWith(suffix string) bool

EndsWith returns true if the string ends with the suffix.

func (String) Equal

func (s String) Equal(other Value) bool

Equal returns true if other is a String with the same value.

func (String) Equivalent

func (s String) Equivalent(other Value) bool

Equivalent compares strings case-insensitively with normalized whitespace.

func (String) HasElement added in v1.5.0

func (p String) HasElement() bool

HasElement reports whether any element accompanied the value.

func (String) IndexOf

func (s String) IndexOf(substr string) int

IndexOf returns the index of the first occurrence of substr, or -1.

func (String) IsEmpty

func (s String) IsEmpty() bool

IsEmpty returns true if the string is empty.

func (String) Length

func (s String) Length() int

Length returns the number of characters.

func (String) Lower

func (s String) Lower() String

Lower returns a new String with all characters lowercase.

func (String) Replace

func (s String) Replace(old, replacement string) String

Replace returns a new String with all occurrences of old replaced by replacement.

func (String) StartsWith

func (s String) StartsWith(prefix string) bool

StartsWith returns true if the string starts with the prefix.

func (String) String

func (s String) String() string

String returns the string value.

func (String) Substring

func (s String) Substring(start, length int) String

Substring returns a substring starting at start with the given length.

func (String) ToChars

func (s String) ToChars() Collection

ToChars returns a collection of single-character strings.

func (String) Type

func (s String) Type() string

Type returns the FHIR type if set (e.g., "Oid", "Url"), otherwise "String".

func (String) Upper

func (s String) Upper() String

Upper returns a new String with all characters uppercase.

func (String) Value

func (s String) Value() string

Value returns the underlying string value.

func (String) WithElement added in v1.5.0

func (s String) WithElement(element *ObjectValue) String

WithElement returns a copy carrying the FHIR element that accompanied the value in the JSON, which is where its extensions and id live.

func (String) WithFHIRType added in v1.5.0

func (s String) WithFHIRType(fhirType string) String

WithFHIRType returns a copy that reports the FHIR type it was declared with. FHIR primitives are types in their own right — a FHIR.code is not a System.String — so a value keeps the name the model gave it.

type Time

type Time struct {
	// contains filtered or unexported fields
}

Time represents a FHIRPath time value.

func NewTime

func NewTime(s string) (Time, error)

NewTime creates a Time from a string.

func NewTimeFromGoTime

func NewTimeFromGoTime(t gotime.Time) Time

NewTimeFromGoTime creates a Time from time.Time.

func (Time) AddDuration added in v1.5.0

func (t Time) AddDuration(value int, unit string) (Time, error)

AddDuration shifts a time of day, wrapping around the day.

"As Time is cyclic, using arithmetic operations + or - on Time types can result in overflowing the time value, which will wrap around the beginning of the day. So adding 1 hour to @T23:30:00 will wrap around to @T00:30:00, which is consistent with the behavior of DateTime values."

A time carries no date, so only the clock units apply. Adding a day to a time of day names no value, and the specification says so: "This includes attempting to add date components to a Time."

func (Time) Compare

func (t Time) Compare(other Value) (int, error)

Compare compares two times. Returns -1, 0, or 1. Implements the Comparable interface. Returns error if precisions differ and comparison is ambiguous.

func (Time) Element added in v1.5.0

func (p Time) Element() *ObjectValue

Element returns the FHIR element a primitive was read with, or nil when the primitive stood alone in the JSON.

func (Time) Equal

func (t Time) Equal(other Value) bool

Equal checks equality with another value.

func (Time) Equivalent

func (t Time) Equivalent(other Value) bool

Equivalent checks equivalence with another value.

func (Time) HasElement added in v1.5.0

func (p Time) HasElement() bool

HasElement reports whether any element accompanied the value.

func (Time) Hour

func (t Time) Hour() int

Accessors

func (Time) IsEmpty

func (t Time) IsEmpty() bool

IsEmpty returns false for Time.

func (Time) Millisecond

func (t Time) Millisecond() int

func (Time) Minute

func (t Time) Minute() int

func (Time) Precision added in v1.3.0

func (t Time) Precision() TimePrecision

Precision returns the time precision.

func (Time) Second

func (t Time) Second() int

func (Time) String

func (t Time) String() string

String returns the string representation.

func (Time) SubtractDuration added in v1.5.0

func (t Time) SubtractDuration(value int, unit string) (Time, error)

SubtractDuration shifts a time of day backwards, wrapping around the day.

func (Time) Type

func (t Time) Type() string

Type returns the type name.

func (Time) WithElement added in v1.5.0

func (t Time) WithElement(element *ObjectValue) Time

WithElement returns a copy carrying the FHIR element that accompanied the value in the JSON, which is where its extensions and id live.

func (Time) WithFHIRType added in v1.5.0

func (t Time) WithFHIRType(fhirType string) Time

WithFHIRType returns a copy that reports the FHIR type it was declared with. FHIR primitives are types in their own right — a FHIR.boolean is not a System.Boolean — so a value keeps the name the model gave it.

type TimePrecision

type TimePrecision int

TimePrecision indicates the precision of a time.

const (
	HourPrecision TimePrecision = iota
	MinutePrecision
	SecondPrecision
	MillisPrecision
)

type TypeError

type TypeError struct {
	Expected  string
	Actual    string
	Operation string
}

TypeError represents a type mismatch error.

func NewTypeError

func NewTypeError(expected, actual, operation string) *TypeError

NewTypeError creates a new TypeError.

func (*TypeError) Error

func (e *TypeError) Error() string

Error implements the error interface.

type TypedChild added in v1.5.0

type TypedChild struct {
	Value Value
	Path  string
}

TypedChild is a child value together with the FHIR path it was reached by, so that a recursive walk (descendants()) can keep resolving types as it descends.

type Value

type Value interface {
	// Type returns the FHIRPath type name.
	Type() string

	// Equal compares exact equality (= operator).
	Equal(other Value) bool

	// Equivalent compares equivalence (~ operator).
	// For strings: case-insensitive, ignores leading/trailing whitespace.
	Equivalent(other Value) bool

	// String returns a string representation of the value.
	String() string

	// IsEmpty indicates if this value represents empty.
	IsEmpty() bool
}

Value is the base interface for all FHIRPath values.

Jump to

Keyboard shortcuts

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