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 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
- type ExpressionCache
- type Model
- type ReferenceResolver
- type Resource
- type ResourceJSON
- type Value
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 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) 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 gofhir/models/r4.FHIRPathModelData (and the r4b/r5 equivalents) via Go's structural typing — no import dependency is required between the two packages.
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.
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 provides UCUM (Unified Code for Units of Measure) normalization for FHIR quantity search parameters.
|
Package ucum provides UCUM (Unified Code for Units of Measure) normalization for FHIR quantity search parameters. |
|
parser
|
|
|
Package types defines the FHIRPath type system.
|
Package types defines the FHIRPath type system. |