Skip to content

Implement ofType() #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
57 changes: 57 additions & 0 deletions fhirpath/fhirpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ var patientChu = &ppb.Patient{
System: &dtpb.ContactPoint_SystemCode{Value: cpb.ContactPointSystemCode_PHONE},
},
},
Deceased: &ppb.Patient_DeceasedX{
Choice: &ppb.Patient_DeceasedX_Boolean{Boolean: &dtpb.Boolean{Value: true}},
},
Name: []*dtpb.HumanName{
{
Use: &dtpb.HumanName_UseCode{
Expand Down Expand Up @@ -951,6 +954,60 @@ func TestFunctionInvocation_Evaluates(t *testing.T) {
inputCollection: []fhirpath.Resource{patientChu},
wantCollection: system.Collection{system.Boolean(false), system.Boolean(true)},
},
{
name: "filters child fields with ofType()",
inputPath: "children().ofType(string)",
inputCollection: []fhirpath.Resource{patientChu},
wantCollection: system.Collection{fhir.ID("123"), &ppb.Patient_GenderCode{Value: cpb.AdministrativeGenderCode_FEMALE}},
},
{
name: "return fhir resource with ofType()",
inputPath: "Patient.ofType(Patient)",
inputCollection: []fhirpath.Resource{patientChu},
wantCollection: system.Collection{patientChu},
},
{
name: "return fhir resource with ofType() type and namespace",
inputPath: "Patient.ofType(FHIR.Patient)",
inputCollection: []fhirpath.Resource{patientChu},
wantCollection: system.Collection{patientChu},
},
{
name: "return fhir resource with ofType() using base type",
inputPath: "Patient.ofType(FHIR.Resource)",
inputCollection: []fhirpath.Resource{patientChu},
wantCollection: system.Collection{patientChu},
},
{
name: "returns empty with ofType()",
inputPath: "Patient.ofType(FHIR.Observation)",
inputCollection: []fhirpath.Resource{patientChu},
wantCollection: system.Collection{},
},
{
name: "ofType() returns gender field",
inputPath: "Patient.gender.ofType(code)",
inputCollection: []fhirpath.Resource{patientChu},
wantCollection: system.Collection{&ppb.Patient_GenderCode{Value: cpb.AdministrativeGenderCode_FEMALE}},
},
{
name: "ofType() returns name.use fields",
inputPath: "Patient.name.ofType(HumanName).use",
inputCollection: []fhirpath.Resource{patientChu},
wantCollection: system.Collection{&dtpb.HumanName_UseCode{Value: cpb.NameUseCode_NICKNAME}, &dtpb.HumanName_UseCode{Value: cpb.NameUseCode_OFFICIAL}},
},
{
name: "ofType() returns name.use fields using a base type",
inputPath: "Patient.name.ofType(FHIR.Element).use.exists()",
inputCollection: []fhirpath.Resource{patientChu},
wantCollection: system.Collection{system.Boolean(true)},
},
{
name: "ofType() with choice element unwraps oneof",
inputPath: "Patient.deceased.ofType(boolean)",
inputCollection: []fhirpath.Resource{patientChu},
wantCollection: system.Collection{fhir.Boolean(true)},
},
{
name: "returns concatenated family name value with join()",
inputPath: "name.family.value.join('-')",
Expand Down
44 changes: 44 additions & 0 deletions fhirpath/internal/funcs/impl/filtering.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package impl

import (
"fmt"
"strings"

"github.com/verily-src/fhirpath-go/fhirpath/internal/expr"
"github.com/verily-src/fhirpath-go/fhirpath/internal/reflection"
"github.com/verily-src/fhirpath-go/fhirpath/system"
"github.com/verily-src/fhirpath-go/internal/fhir"
"github.com/verily-src/fhirpath-go/internal/protofields"
)

// Where evaluates the expression args[0] on each input item, collects the items that cause
Expand Down Expand Up @@ -33,3 +37,43 @@ func Where(ctx *expr.Context, input system.Collection, args ...expr.Expression)
}
return result, nil
}

func OfType(ctx *expr.Context, input system.Collection, args ...expr.Expression) (system.Collection, error) {
if len(args) != 1 {
return nil, fmt.Errorf("%w: received %v arguments, expected 1", ErrWrongArity, len(args))
}

var typeSpecifier reflection.TypeSpecifier
typeExpr, ok := args[0].(*expr.TypeExpression)
if !ok {
return nil, fmt.Errorf("received invalid argument, expected a type")
}
var err error
if parts := strings.Split(typeExpr.Type, "."); len(parts) == 2 {
if typeSpecifier, err = reflection.NewQualifiedTypeSpecifier(parts[0], parts[1]); err != nil {
return nil, err
}
} else if typeSpecifier, err = reflection.NewTypeSpecifier(typeExpr.Type); err != nil {
return nil, err
}
result := system.Collection{}
for _, item := range input {
typ, err := reflection.TypeOf(item)
if err != nil {
return nil, err
}
if !typ.Is(typeSpecifier) {
continue
}
// attempt to unwrap polymorphic types
message, ok := item.(fhir.Base)
if !ok {
result = append(result, item)
} else if oneOf := protofields.UnwrapOneofField(message, "choice"); oneOf != nil {
result = append(result, oneOf)
} else {
result = append(result, item)
}
}
return result, nil
}
77 changes: 77 additions & 0 deletions fhirpath/internal/funcs/impl/filtering_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,80 @@ func TestWhere_RaisesError(t *testing.T) {
})
}
}

func TestOfType_Evaluates(t *testing.T) {
testCases := []struct {
name string
inputCollection system.Collection
inputArgs expr.Expression
wantCollection system.Collection
}{
{
name: "filter on base node",
inputCollection: slices.MustConvert[any](contact),
inputArgs: &expr.TypeExpression{Type: "FHIR.ContactDetail"},
wantCollection: slices.MustConvert[any](contact),
},
{
name: "filter on base node without namespace",
inputCollection: slices.MustConvert[any](contact[0:3]),
inputArgs: &expr.TypeExpression{Type: "ContactDetail"},
wantCollection: slices.MustConvert[any](contact),
},
{
name: "returns empty collection",
inputCollection: slices.MustConvert[any](contact[0:2]),
inputArgs: &expr.TypeExpression{Type: "string"},
wantCollection: system.Collection{},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got, err := impl.OfType(&expr.Context{}, tc.inputCollection, tc.inputArgs)
if err != nil {
t.Fatalf("OfType function returned unexpected error: %v", err)
}
if diff := cmp.Diff(tc.wantCollection, got, protocmp.Transform()); diff != "" {
t.Errorf("OfType function returned unexpected diff (-want, +got):\n%s", diff)
}
})
}
}

func TestOfType_RaisesError(t *testing.T) {
testCases := []struct {
name string
inputArgs []expr.Expression
inputCollection system.Collection
}{
{
name: "multiple arguments",
inputArgs: []expr.Expression{exprtest.Return(1), exprtest.Return(1)},
inputCollection: slices.MustConvert[any](contact),
},
{
name: "argument expression raises error",
inputArgs: []expr.Expression{exprtest.Error(errors.New("some error"))},
inputCollection: slices.MustConvert[any](contact),
},
{
name: "invalid argument expression",
inputArgs: []expr.Expression{exprtest.Return(1, 2)},
inputCollection: slices.MustConvert[any](contact),
},
{
name: "invalid type",
inputArgs: []expr.Expression{&expr.TypeExpression{Type: "foo"}},
inputCollection: slices.MustConvert[any](contact),
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if _, err := impl.OfType(&expr.Context{}, tc.inputCollection, tc.inputArgs...); err == nil {
t.Fatalf("evaluating OfType function didn't return error when expected")
}
})
}
}
7 changes: 6 additions & 1 deletion fhirpath/internal/funcs/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ var baseTable = FunctionTable{
false,
},
"repeat": notImplemented,
"ofType": notImplemented,
"ofType": Function{
impl.OfType,
1,
1,
true,
},
"single": notImplemented,
"first": Function{
impl.First,
Expand Down
3 changes: 2 additions & 1 deletion fhirpath/internal/grammar/fhirpath.g4
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ invocation // Terms that can be used after the function
;

function
: identifier '(' paramList? ')'
: 'ofType(' typeSpecifier ')'
| identifier '(' paramList? ')'
;

paramList
Expand Down
Loading