Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions fhirpath/compopts/compopts.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package compopts
import (
"errors"

"github.com/verily-src/fhirpath-go/fhirpath/internal/funcs"
"github.com/verily-src/fhirpath-go/fhirpath/internal/opts"
"github.com/verily-src/fhirpath-go/fhirpath/internal/parser"
)
Expand Down Expand Up @@ -53,3 +54,12 @@ func Permissive() opts.CompileOption {
return nil
})
}

// WithExperimentalFuncs is an option that enables experimental functions not
// in the N1 Normative specification.
func WithExperimentalFuncs() opts.CompileOption {
return opts.Transform(func(cfg *opts.CompileConfig) error {
cfg.Table = funcs.AddExperimentalFuncs(cfg.Table)
return nil
})
}
2 changes: 1 addition & 1 deletion fhirpath/evalopts/evalopts.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"fmt"
"time"

"github.com/verily-src/fhirpath-go/internal/fhir"
"github.com/verily-src/fhirpath-go/fhirpath/internal/opts"
"github.com/verily-src/fhirpath-go/fhirpath/system"
"github.com/verily-src/fhirpath-go/internal/fhir"
)

var (
Expand Down
4 changes: 2 additions & 2 deletions fhirpath/fhirpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package fhirpath
import (
"errors"

"github.com/verily-src/fhirpath-go/internal/slices"
"github.com/verily-src/fhirpath-go/internal/fhir"
"github.com/verily-src/fhirpath-go/fhirpath/evalopts"
"github.com/verily-src/fhirpath-go/fhirpath/internal/compile"
"github.com/verily-src/fhirpath-go/fhirpath/internal/expr"
"github.com/verily-src/fhirpath-go/fhirpath/internal/opts"
"github.com/verily-src/fhirpath-go/fhirpath/internal/parser"
"github.com/verily-src/fhirpath-go/fhirpath/system"
"github.com/verily-src/fhirpath-go/internal/fhir"
"github.com/verily-src/fhirpath-go/internal/slices"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion fhirpath/fhirpath_expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"math"
"testing"

"github.com/verily-src/fhirpath-go/internal/fhir"
"github.com/verily-src/fhirpath-go/fhirpath"
"github.com/verily-src/fhirpath-go/fhirpath/fhirpathtest"
"github.com/verily-src/fhirpath-go/fhirpath/system"
"github.com/verily-src/fhirpath-go/internal/fhir"
)

func TestExpressionString(t *testing.T) {
Expand Down
47 changes: 43 additions & 4 deletions fhirpath/fhirpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/shopspring/decimal"
"github.com/verily-src/fhirpath-go/internal/fhir"
"github.com/verily-src/fhirpath-go/internal/element/extension"
"github.com/verily-src/fhirpath-go/internal/element/reference"
"github.com/verily-src/fhirpath-go/internal/fhirconv"
"github.com/verily-src/fhirpath-go/fhirpath"
"github.com/verily-src/fhirpath-go/fhirpath/compopts"
"github.com/verily-src/fhirpath-go/fhirpath/evalopts"
"github.com/verily-src/fhirpath-go/fhirpath/system"
"github.com/verily-src/fhirpath-go/internal/element/extension"
"github.com/verily-src/fhirpath-go/internal/element/reference"
"github.com/verily-src/fhirpath-go/internal/fhir"
"github.com/verily-src/fhirpath-go/internal/fhirconv"
"google.golang.org/protobuf/testing/protocmp"
)

Expand Down Expand Up @@ -951,6 +951,20 @@ func TestFunctionInvocation_Evaluates(t *testing.T) {
inputCollection: []fhir.Resource{patientChu},
wantCollection: system.Collection{system.Boolean(false), system.Boolean(true)},
},
{
name: "returns concatenated family name value with join()",
inputPath: "name.family.value.join('-')",
inputCollection: []fhir.Resource{patientChu},
wantCollection: system.Collection{system.String("Chu-Chu")},
compileOptions: []fhirpath.CompileOption{compopts.WithExperimentalFuncs()},
},
{
name: "returns concatenated family name with join()",
inputPath: "name.family.join('-')",
inputCollection: []fhir.Resource{patientChu},
wantCollection: system.Collection{system.String("Chu-Chu")},
compileOptions: []fhirpath.CompileOption{compopts.WithExperimentalFuncs()},
},
}

testEvaluate(t, testCases)
Expand Down Expand Up @@ -1408,6 +1422,31 @@ func TestPolarityExpression(t *testing.T) {
testEvaluate(t, testCases)
}

func TestAll_Evaluates(t *testing.T) {
testCases := []evaluateTestCase{
{
name: "returns false if not all elements are integers",
inputPath: "Patient.name.given.all($this is Integer)",
inputCollection: []fhir.Resource{patientChu},
wantCollection: system.Collection{system.Boolean(false)},
},
{
name: "returns true if input is empty",
inputPath: "{}.all($this is Integer)",
inputCollection: []fhir.Resource{},
wantCollection: system.Collection{system.Boolean(true)},
},
{
name: "returns true if born during the 21st century",
inputPath: "Patient.birthDate.all($this >= @2000-01-01 and $this < @2100-01-01)",
inputCollection: []fhir.Resource{patientChu},
wantCollection: system.Collection{system.Boolean(true)},
},
}

testEvaluate(t, testCases)
}

func TestMustCompile_CompileError_Panics(t *testing.T) {
defer func() { _ = recover() }()

Expand Down
2 changes: 1 addition & 1 deletion fhirpath/fhirpathtest/fhirpathtest_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"errors"
"fmt"

"github.com/verily-src/fhirpath-go/internal/fhir"
"github.com/verily-src/fhirpath-go/fhirpath/fhirpathtest"
"github.com/verily-src/fhirpath-go/fhirpath/system"
"github.com/verily-src/fhirpath-go/internal/fhir"
)

func ExampleError() {
Expand Down
4 changes: 2 additions & 2 deletions fhirpath/fhirpathtest/fhirpathtest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/verily-src/fhirpath-go/fhirpath/fhirpathtest"
"github.com/verily-src/fhirpath-go/fhirpath/system"
"github.com/verily-src/fhirpath-go/internal/fhir"
"github.com/verily-src/fhirpath-go/internal/fhirtest"
"github.com/verily-src/fhirpath-go/internal/resource"
"github.com/verily-src/fhirpath-go/fhirpath/fhirpathtest"
"github.com/verily-src/fhirpath-go/fhirpath/system"
"google.golang.org/protobuf/testing/protocmp"
)

Expand Down
8 changes: 4 additions & 4 deletions fhirpath/internal/expr/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
bcrpb "github.com/google/fhir/go/proto/google/fhir/proto/r4/core/resources/bundle_and_contained_resource_go_proto"
"github.com/iancoleman/strcase"
"github.com/shopspring/decimal"
"github.com/verily-src/fhirpath-go/internal/slices"
"github.com/verily-src/fhirpath-go/internal/fhir"
"github.com/verily-src/fhirpath-go/internal/containedresource"
"github.com/verily-src/fhirpath-go/internal/fhirconv"
"github.com/verily-src/fhirpath-go/fhirpath/internal/reflection"
"github.com/verily-src/fhirpath-go/fhirpath/system"
"github.com/verily-src/fhirpath-go/internal/containedresource"
"github.com/verily-src/fhirpath-go/internal/fhir"
"github.com/verily-src/fhirpath-go/internal/fhirconv"
"github.com/verily-src/fhirpath-go/internal/protofields"
"github.com/verily-src/fhirpath-go/internal/slices"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/known/anypb"
Expand Down
10 changes: 5 additions & 5 deletions fhirpath/internal/expr/expressions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ import (
ppb "github.com/google/fhir/go/proto/google/fhir/proto/r4/core/resources/patient_go_proto"
"github.com/google/go-cmp/cmp"
"github.com/shopspring/decimal"
"github.com/verily-src/fhirpath-go/internal/slices"
"github.com/verily-src/fhirpath-go/internal/fhir"
"github.com/verily-src/fhirpath-go/internal/bundle"
"github.com/verily-src/fhirpath-go/internal/element/extension"
"github.com/verily-src/fhirpath-go/internal/fhirconv"
"github.com/verily-src/fhirpath-go/fhirpath"
"github.com/verily-src/fhirpath-go/fhirpath/internal/expr"
"github.com/verily-src/fhirpath-go/fhirpath/internal/expr/exprtest"
"github.com/verily-src/fhirpath-go/fhirpath/internal/reflection"
"github.com/verily-src/fhirpath-go/fhirpath/system"
"github.com/verily-src/fhirpath-go/internal/bundle"
"github.com/verily-src/fhirpath-go/internal/element/extension"
"github.com/verily-src/fhirpath-go/internal/fhir"
"github.com/verily-src/fhirpath-go/internal/fhirconv"
"github.com/verily-src/fhirpath-go/internal/slices"
"google.golang.org/protobuf/testing/protocmp"
)

Expand Down
2 changes: 1 addition & 1 deletion fhirpath/internal/funcs/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (

ppb "github.com/google/fhir/go/proto/google/fhir/proto/r4/core/resources/patient_go_proto"
"github.com/google/go-cmp/cmp"
"github.com/verily-src/fhirpath-go/internal/fhir"
"github.com/verily-src/fhirpath-go/fhirpath/internal/expr"
"github.com/verily-src/fhirpath-go/fhirpath/internal/expr/exprtest"
"github.com/verily-src/fhirpath-go/fhirpath/internal/funcs"
"github.com/verily-src/fhirpath-go/fhirpath/system"
"github.com/verily-src/fhirpath-go/internal/fhir"
)

func TestToFunction_EvaluatesCorrectly(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion fhirpath/internal/funcs/impl/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package impl_test
import (
"testing"

"github.com/verily-src/fhirpath-go/internal/fhir"
"github.com/verily-src/fhirpath-go/fhirpath/internal/expr/exprtest"
"github.com/verily-src/fhirpath-go/internal/fhir"
"google.golang.org/protobuf/testing/protocmp"

ppb "github.com/google/fhir/go/proto/google/fhir/proto/r4/core/resources/patient_go_proto"
Expand Down
36 changes: 36 additions & 0 deletions fhirpath/internal/funcs/impl/existence.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package impl

import (
"fmt"

"github.com/verily-src/fhirpath-go/fhirpath/internal/expr"
"github.com/verily-src/fhirpath-go/fhirpath/system"
)
Expand Down Expand Up @@ -74,6 +75,41 @@ func AnyFalse(ctx *expr.Context, input system.Collection, args ...expr.Expressio
return system.Collection{system.Boolean(false)}, nil
}

// All returns true if for every element in the input collection, criteria evaluates to true.
// Otherwise, the result is false. If the input collection is empty ({}), the result is true.
// FHIRPath docs here: https://hl7.org/fhirpath/N1/#allcriteria-expression-boolean
func All(ctx *expr.Context, input system.Collection, args ...expr.Expression) (system.Collection, error) {
// If the input collection is empty, return true
if input.IsEmpty() {
return system.Collection{system.Boolean(true)}, nil
}

// Validate that exactly one argument (the criteria expression) is provided
if len(args) != 1 {
return nil, fmt.Errorf("%w: received %v arguments, expected 1", ErrWrongArity, len(args))
}

// Evaluate the criteria expression for each element in the input collection
for _, element := range input {
// Evaluate the criteria expression
output, err := args[0].Evaluate(ctx, system.Collection{element})
if err != nil {
return nil, fmt.Errorf("evaluating criteria expression resulted in an error: %w", err)
}

// Check that the output for false
// if 'err' is non-nil, `v` is false
if v, err := output.ToBool(); err != nil {
return nil, err
} else if !v {
return system.Collection{system.Boolean(false)}, nil
}
}

// Return true if all elements satisfy the criteria
return system.Collection{system.Boolean(true)}, nil
}

// Count returns the integer count of the number of items in the input collection.
// returns 0 when the input collection is empty.
// FHIRPath docs here: https://hl7.org/fhirpath/N1/#convertstodate-boolean
Expand Down
Loading
Loading