Documentation
¶
Overview ¶
Package fhirpath provides a FHIRPath engine for evaluating expressions on FHIR resources.
Index ¶
- Variables
- func Count(resource []byte, expr string) (int, error)
- func Evaluate(resource []byte, expr string) (types.Collection, error)
- func EvaluateToBoolean(resource []byte, expr string) (bool, error)
- func EvaluateToString(resource []byte, expr string) (string, error)
- func EvaluateToStrings(resource []byte, expr string) ([]string, error)
- func Exists(resource []byte, expr string) (bool, error)
- func MustEvaluate(resource []byte, expr string) types.Collection
- type AnalysisError
- type CacheStats
- type Collection
- type EvalOption
- func WithContext(ctx context.Context) EvalOption
- func WithMaxCollectionSize(size int) EvalOption
- func WithMaxDepth(depth int) EvalOption
- func WithModel(m Model) EvalOption
- func WithResolver(r ReferenceResolver) EvalOption
- func WithTimeout(d time.Duration) EvalOption
- func WithVariable(name string, value types.Collection) EvalOption
- type EvalOptions
- type Expression
- func (e *Expression) Analyze(model Model, contextType string) error
- func (e *Expression) Evaluate(resource []byte) (types.Collection, error)
- func (e *Expression) EvaluateWithContext(ctx *eval.Context) (types.Collection, error)
- func (e *Expression) EvaluateWithOptions(resource []byte, opts ...EvalOption) (types.Collection, error)
- func (e *Expression) String() string
- type ExpressionCache
- type Model
- type ReferenceResolver
- type Resource
- type ResourceJSON
- type TypeRegistry
- type Value
- type VersionedModel
Constants ¶
This section is empty.
Variables ¶
var DefaultCache = NewExpressionCache(1000)
DefaultCache is a global expression cache for convenience. Use NewExpressionCache for finer control over cache lifetime.
Functions ¶
func Evaluate ¶
func Evaluate(resource []byte, expr string) (types.Collection, error)
Evaluate parses and evaluates a FHIRPath expression against a JSON resource. This is a convenience function that compiles and evaluates in one step.
func EvaluateToBoolean ¶
EvaluateToBoolean evaluates an expression and returns a boolean result. Returns false if the result is empty or not a boolean.
func EvaluateToString ¶
EvaluateToString evaluates an expression and returns a string result.
func EvaluateToStrings ¶
EvaluateToStrings evaluates an expression and returns all results as strings.
func MustEvaluate ¶
func MustEvaluate(resource []byte, expr string) types.Collection
MustEvaluate is like Evaluate but panics on error.
Types ¶
type AnalysisError ¶ added in v1.5.0
AnalysisError reports an expression that cannot be valid against the model.
func (*AnalysisError) Error ¶ added in v1.5.0
func (e *AnalysisError) Error() string
type CacheStats ¶
CacheStats holds cache performance statistics.
type Collection ¶
type Collection = types.Collection
Collection is an alias for types.Collection for easier external use.
func EvaluateCached ¶
func EvaluateCached(resource []byte, expr string) (Collection, error)
EvaluateCached compiles (with caching) and evaluates a FHIRPath expression. This is the recommended function for production use.
func EvaluateResource ¶
func EvaluateResource(resource Resource, expr string) (Collection, error)
EvaluateResource evaluates a FHIRPath expression against a Go struct. The resource is serialized to JSON first, then evaluated. For better performance with multiple evaluations, cache the JSON bytes.
func EvaluateResourceCached ¶
func EvaluateResourceCached(resource Resource, expr string) (Collection, error)
EvaluateResourceCached is like EvaluateResource but uses the expression cache.
type EvalOption ¶
type EvalOption func(*EvalOptions)
EvalOption is a functional option for configuring evaluation.
func WithContext ¶
func WithContext(ctx context.Context) EvalOption
WithContext sets the context for cancellation.
func WithMaxCollectionSize ¶
func WithMaxCollectionSize(size int) EvalOption
WithMaxCollectionSize sets the maximum output collection size.
func WithMaxDepth ¶
func WithMaxDepth(depth int) EvalOption
WithMaxDepth sets the maximum recursion depth.
func WithModel ¶ added in v1.2.0
func WithModel(m Model) EvalOption
WithModel sets the FHIR version-specific model for the evaluation. When provided, the engine uses precise choice type lists, full type hierarchy, and path-based type resolution instead of built-in heuristics.
func WithResolver ¶
func WithResolver(r ReferenceResolver) EvalOption
WithResolver sets the reference resolver.
func WithTimeout ¶
func WithTimeout(d time.Duration) EvalOption
WithTimeout sets the evaluation timeout.
func WithVariable ¶
func WithVariable(name string, value types.Collection) EvalOption
WithVariable sets an external variable.
type EvalOptions ¶
type EvalOptions struct {
// Context for cancellation and timeout
Ctx context.Context
// Timeout for evaluation (0 means no timeout)
Timeout time.Duration
// MaxDepth limits recursion depth for descendants() (0 means default of 100)
MaxDepth int
// MaxCollectionSize limits output collection size (0 means no limit)
MaxCollectionSize int
// Variables are external variables accessible via %name
Variables map[string]types.Collection
// Resolver handles reference resolution for resolve() function
Resolver ReferenceResolver
// Model provides FHIR version-specific type metadata.
// When nil, the engine uses built-in heuristics.
Model Model
}
EvalOptions configures expression evaluation.
func DefaultOptions ¶
func DefaultOptions() *EvalOptions
DefaultOptions returns default evaluation options suitable for production.
type Expression ¶
type Expression struct {
// contains filtered or unexported fields
}
Expression represents a compiled FHIRPath expression.
func Compile ¶
func Compile(expr string) (*Expression, error)
Compile parses a FHIRPath expression and returns a compiled Expression. The compiled expression can be evaluated multiple times against different resources.
func GetCached ¶
func GetCached(expr string) (*Expression, error)
GetCached retrieves or compiles an expression using the default cache.
func MustCompile ¶
func MustCompile(expr string) *Expression
MustCompile is like Compile but panics on error.
func MustGetCached ¶
func MustGetCached(expr string) *Expression
MustGetCached is like GetCached but panics on error.
func (*Expression) Analyze ¶ added in v1.5.0
func (e *Expression) Analyze(model Model, contextType string) error
Analyze checks the expression against a model, in the context of a resource or element type, and reports the first fault it can prove.
contextType names the type the expression is evaluated against — "Patient" for a constraint on Patient. An empty contextType leaves the starting type unknown, which limits the analysis to the checks that do not depend on it.
Returns nil when nothing is provably wrong. That is not a guarantee of correctness: a model cannot describe every function's result, and the analysis stops where it stops seeing.
func (*Expression) Evaluate ¶
func (e *Expression) Evaluate(resource []byte) (types.Collection, error)
Evaluate executes the expression against a JSON resource.
func (*Expression) EvaluateWithContext ¶
func (e *Expression) EvaluateWithContext(ctx *eval.Context) (types.Collection, error)
EvaluateWithContext executes the expression with a custom context.
func (*Expression) EvaluateWithOptions ¶
func (e *Expression) EvaluateWithOptions(resource []byte, opts ...EvalOption) (types.Collection, error)
EvaluateWithOptions evaluates an expression with custom options.
func (*Expression) String ¶
func (e *Expression) String() string
String returns the original expression string.
type ExpressionCache ¶
type ExpressionCache struct {
// contains filtered or unexported fields
}
ExpressionCache provides thread-safe caching of compiled FHIRPath expressions with LRU eviction. Use this in production to avoid recompiling the same expressions.
func NewExpressionCache ¶
func NewExpressionCache(limit int) *ExpressionCache
NewExpressionCache creates a new cache with the given size limit. If limit <= 0, the cache is unbounded.
func (*ExpressionCache) Clear ¶
func (c *ExpressionCache) Clear()
Clear removes all cached expressions.
func (*ExpressionCache) Get ¶
func (c *ExpressionCache) Get(expr string) (*Expression, error)
Get retrieves a compiled expression from the cache, compiling it if necessary.
func (*ExpressionCache) HitRate ¶
func (c *ExpressionCache) HitRate() float64
HitRate returns the cache hit rate as a percentage (0-100).
func (*ExpressionCache) MustGet ¶
func (c *ExpressionCache) MustGet(expr string) *Expression
MustGet is like Get but panics on error.
func (*ExpressionCache) Size ¶
func (c *ExpressionCache) Size() int
Size returns the number of cached expressions.
func (*ExpressionCache) Stats ¶
func (c *ExpressionCache) Stats() CacheStats
Stats returns cache performance statistics.
type Model ¶ added in v1.2.0
type Model interface {
// ChoiceTypes returns the permitted type suffixes for a polymorphic
// element path. For example, "Observation.value" might return
// ["Quantity","CodeableConcept","string","boolean",...].
// Returns nil if the path is not a choice type element.
ChoiceTypes(path string) []string
// TypeOf returns the FHIR type code for the given element path.
// For example, "Patient.name" returns "HumanName".
// Returns "" if the path is unknown.
TypeOf(path string) string
// ReferenceTargets returns the allowed target resource type names for
// a Reference or canonical element path.
// Returns nil if the path is not a reference or has no constrained targets.
ReferenceTargets(path string) []string
// ParentType returns the immediate parent type in the FHIR type hierarchy.
// For example, "Patient" returns "DomainResource", "Age" returns "Quantity".
// Returns "" if the type is unknown or has no parent.
ParentType(typeName string) string
// IsSubtype reports whether child is the same as, or a subtype of,
// parent in the FHIR type hierarchy.
IsSubtype(child, parent string) bool
// ResolvePath resolves a path whose element definition is borrowed from
// another location via contentReference.
// For example, "Questionnaire.item.item" returns "Questionnaire.item".
// Returns the original path if no redirection is needed.
ResolvePath(path string) string
// IsResource reports whether the given type name is a known FHIR resource type.
IsResource(typeName string) bool
}
Model provides FHIR version-specific type and path metadata for the FHIRPath engine. When a Model is supplied via WithModel, the evaluator uses it for precise polymorphic field resolution, type hierarchy checking, and path-based type inference. When nil (the default), the engine falls back to its built-in heuristics.
This interface is satisfied by the model returned from gofhir/models/r4.FHIRPathModel() (and the r4b/r5 equivalents) via Go's structural typing — no import dependency is required between the two packages:
import "github.com/gofhir/models/r4"
result, err := expr.EvaluateWithOptions(resource,
fhirpath.WithModel(r4.FHIRPathModel()))
Note that FHIRPathModelData is the struct type; FHIRPathModel() is the accessor that returns the populated instance.
type ReferenceResolver ¶
type ReferenceResolver interface {
// Resolve takes a reference string (e.g., "Patient/123") and returns the resource.
Resolve(ctx context.Context, reference string) ([]byte, error)
}
ReferenceResolver resolves FHIR references for the resolve() function.
type Resource ¶
type Resource interface {
GetResourceType() string
}
Resource represents any FHIR resource that can be evaluated.
type ResourceJSON ¶
type ResourceJSON struct {
// contains filtered or unexported fields
}
ResourceJSON wraps a resource with its pre-serialized JSON for efficient repeated evaluation.
func MustNewResourceJSON ¶
func MustNewResourceJSON(resource Resource) *ResourceJSON
MustNewResourceJSON is like NewResourceJSON but panics on error.
func NewResourceJSON ¶
func NewResourceJSON(resource Resource) (*ResourceJSON, error)
NewResourceJSON creates a ResourceJSON from a Go resource.
func (*ResourceJSON) Evaluate ¶
func (r *ResourceJSON) Evaluate(expr string) (Collection, error)
Evaluate evaluates a FHIRPath expression against this resource.
func (*ResourceJSON) EvaluateCached ¶
func (r *ResourceJSON) EvaluateCached(expr string) (Collection, error)
EvaluateCached evaluates using the expression cache.
func (*ResourceJSON) JSON ¶
func (r *ResourceJSON) JSON() []byte
JSON returns the pre-serialized JSON bytes.
func (*ResourceJSON) Resource ¶
func (r *ResourceJSON) Resource() Resource
Resource returns the original Go resource.
type TypeRegistry ¶ added in v1.5.0
type TypeRegistry interface {
// HasType reports whether the name resolves to a type this model declares.
// The name is unqualified: Patient rather than FHIR.Patient.
HasType(typeName string) bool
}
TypeRegistry is an optional interface a Model may also implement to report whether a name resolves to a type at all.
The specification requires it for the type operators: "A type specifier is an identifier that must resolve to the name of a type in a model." A name that resolves to nothing is not a filter that matches nothing — Patient.gender.as(string1) is an error, because string1 names no type — and only the model can tell the two apart.
A model that does not implement this interface has its type specifiers taken at face value, which is the behavior every existing caller has today.
type VersionedModel ¶ added in v1.5.0
type VersionedModel interface {
// FHIRVersion returns the FHIR version the model describes.
FHIRVersion() string
}
VersionedModel is an optional interface a Model may also implement to declare which FHIR version it describes, as "4.0.1" or "R5".
Some rules of the language changed with R5, and the reference validator applies them by version rather than picking one reading. The clearest case is the `as` operator: from R5 it must raise an error when its input holds more than one item, while before R5 it filters — which is what FHIR's own dom-3 invariant relies on, since it is written as %resource.descendants().as(canonical).
A model that does not implement this interface is treated as pre-R5, which keeps every existing caller on the behavior it has today.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package eval provides the FHIRPath expression evaluator.
|
Package eval provides the FHIRPath expression evaluator. |
|
Package funcs provides FHIRPath function implementations.
|
Package funcs provides FHIRPath function implementations. |
|
internal
|
|
|
ucum
Package ucum adapts UCUM unit handling to the exact decimal arithmetic this engine uses.
|
Package ucum adapts UCUM unit handling to the exact decimal arithmetic this engine uses. |
|
parser
|
|
|
Package types defines the FHIRPath type system.
|
Package types defines the FHIRPath type system. |