Skip to content

Commit

Permalink
Fix issue with properties on null values inside queries.
Browse files Browse the repository at this point in the history
This ensures the valueProperty helper also returns null on null value inputs.

PiperOrigin-RevId: 651886526
  • Loading branch information
suyashkumar authored and copybara-github committed Jul 12, 2024
1 parent 723b8d6 commit 3c31bb5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
3 changes: 3 additions & 0 deletions interpreter/property.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ func (i *interpreter) valueProperty(v result.Value, property string, staticResul
if property == "" {
return v, nil
}
if result.IsNull(v) {
return result.New(nil)
}

switch ot := v.GolangValue().(type) {
case result.Tuple:
Expand Down
38 changes: 33 additions & 5 deletions interpreter/property_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@ func TestEvalPropertyValue_Errors(t *testing.T) {
}),
resultType: &types.List{ElementType: types.Integer},
},
{
name: "null",
property: "name",
value: newOrFatal(t, nil),
},
{
name: "interval invalid property",
property: "invalid",
Expand Down Expand Up @@ -119,6 +114,39 @@ func TestEvalPropertyValue_Errors(t *testing.T) {
}
}

func TestEvalPropertyValue(t *testing.T) {
tests := []struct {
name string
property string
value result.Value
resultType types.IType
wantValue result.Value
}{
{
name: "property on null input value",
property: "apple",
value: newOrFatal(t, nil),
wantValue: newOrFatal(t, nil),
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
i := &interpreter{
refs: reference.NewResolver[result.Value, *model.FunctionDef](),
modelInfo: newFHIRModelInfo(t),
evaluationTimestamp: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC),
}
got, err := i.valueProperty(tc.value, tc.property, tc.resultType)
if err != nil {
t.Errorf("evalPropertyValue(%q) unexpected error: %v", tc.property, err)
}
if !got.Equal(tc.wantValue) {
t.Errorf("evalPropertyValue(%q) = %v, want %v", tc.property, got, tc.wantValue)
}
})
}
}

func newFHIRModelInfo(t *testing.T) *modelinfo.ModelInfos {
t.Helper()
fhirMIBytes, err := embeddata.ModelInfos.ReadFile("third_party/cqframework/fhir-modelinfo-4.0.1.xml")
Expand Down
6 changes: 6 additions & 0 deletions tests/enginetests/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,12 @@ func TestQuery(t *testing.T) {
StaticType: &types.List{ElementType: &types.Tuple{ElementTypes: map[string]types.IType{"A": types.Integer, "B": types.Integer}}},
}),
},
{
// This ensures that properties on null values inside queries are handled correctly.
name: "Property on null alias in query",
cql: "define TESTRESULT: (null as Code) l return l.code",
wantResult: newOrFatal(t, nil),
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
Expand Down

0 comments on commit 3c31bb5

Please sign in to comment.