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
7 changes: 7 additions & 0 deletions interpreter/operator_dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,13 @@ func (i *interpreter) binaryOverloads(m model.IBinaryExpression) ([]convert.Over
Result: evalLastPositionOf,
},
}, nil
case *model.StartsWith:
return []convert.Overload[evalBinarySignature]{
{
Operands: []types.IType{types.String, types.String},
Result: evalStartsWith,
},
}, nil
case *model.PositionOf:
return []convert.Overload[evalBinarySignature]{
{
Expand Down
17 changes: 17 additions & 0 deletions interpreter/operator_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,4 +338,21 @@ func evalPositionOf(m model.IBinaryExpression, lObj, rObj result.Value) (result.
return result.Value{}, err
}
return result.New(strings.Index(argument, pattern))
}

// StartsWith(argument String, prefix String) Boolean
// https://cql.hl7.org/09-b-cqlreference.html#startswith
func evalStartsWith(m model.IBinaryExpression, lObj, rObj result.Value) (result.Value, error) {
if result.IsNull(lObj) || result.IsNull(rObj) {
return result.New(nil)
}
argument, err := result.ToString(lObj)
if err != nil {
return result.Value{}, err
}
prefix, err := result.ToString(rObj)
if err != nil {
return result.Value{}, err
}
return result.New(strings.HasPrefix(argument, prefix))
}
6 changes: 6 additions & 0 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,9 @@ type EndsWith struct{ *BinaryExpression }
// LastPositionOf is https://cql.hl7.org/09-b-cqlreference.html#lastpositionof
type LastPositionOf struct{ *BinaryExpression }

// StartsWith is https://cql.hl7.org/09-b-cqlreference.html#startswith
type StartsWith struct { *BinaryExpression}

// Upper is https://cql.hl7.org/09-b-cqlreference.html#Upper
type Upper struct{ *UnaryExpression}

Expand Down Expand Up @@ -1554,6 +1557,9 @@ func (a *EndsWith) GetName() string { return "EndsWith" }
// GetName returns the name of the system operator.
func (a *LastPositionOf) GetName() string { return "LastPositionOf" }

// GetName returns the name of the system operator.
func (a *StartsWith) GetName() string { return "StartsWith" }

// GetName returns the name of the system operator.
func (a *Upper) GetName() string { return "Upper"}

Expand Down
13 changes: 13 additions & 0 deletions parser/operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,19 @@ func (p *Parser) loadSystemOperators() error {
}
},
},
{
name: "StartsWith",
operands: [][]types.IType{
{types.String, types.String},
},
model: func() model.IExpression {
return &model.StartsWith{
BinaryExpression: &model.BinaryExpression{
Expression: model.ResultType(types.Boolean),
},
}
},
},
// DATE AND TIME OPERATORS - https://cql.hl7.org/09-b-cqlreference.html#datetime-operators-2
{
name: "Add",
Expand Down
13 changes: 13 additions & 0 deletions parser/operators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,19 @@ func TestBuiltInFunctions(t *testing.T) {
},
},
},
{
name: "StartsWith",
cql: "StartsWith('Excellent', 'Ex')",
want: &model.StartsWith{
BinaryExpression: &model.BinaryExpression{
Operands: []model.IExpression{
model.NewLiteral("Excellent", types.String),
model.NewLiteral("Ex", types.String),
},
Expression: model.ResultType(types.Boolean),
},
},
},
// DATE AND TIME OPERATORS - https://cql.hl7.org/09-b-cqlreference.html#datetime-operators-2
{
name: "After",
Expand Down
68 changes: 68 additions & 0 deletions tests/enginetests/operator_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -870,4 +870,72 @@ func TestPositionOf(t *testing.T) {
}
})
}
}
func TestStartsWith(t *testing.T) {
tests := []struct {
name string
cql string
wantModel model.IExpression
wantResult result.Value
}{
{
name: "StartsWithTrue",
cql: "StartsWith('Appendix','App')",
wantModel: &model.StartsWith{
BinaryExpression: &model.BinaryExpression{
Operands: []model.IExpression{
model.NewLiteral("Appendix", types.String),
model.NewLiteral("App", types.String),
},
Expression: model.ResultType(types.Boolean),
},
},
wantResult: newOrFatal(t, true),
},
{
name: "StartsWithFalse",
cql: "StartsWith('Appendix','Dep')",
wantResult: newOrFatal(t, false),
},
{
name: "StartsWithLeftNull",
cql: "StartsWith(null, 'App')",
wantResult: newOrFatal(t, nil),
},
{
name: "StartsWithRightNull",
cql: "StartsWith('Appendix', null)",
wantResult: newOrFatal(t, nil),
},
{
name: "StartsWithLeftEmpty",
cql: "StartsWith('','App')",
wantResult: newOrFatal(t, false),
},
{
name: "StartsWithRightEmpty",
cql: "StartsWith('Appendix','')",
wantResult: newOrFatal(t, true),
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
p := newFHIRParser(t)
parsedLibs, err := p.Libraries(context.Background(), wrapInLib(t, tc.cql), parser.Config{})
if err != nil {
t.Fatalf("Parse returned unexpected error: %v", err)
}
if diff := cmp.Diff(tc.wantModel, getTESTRESULTModel(t, parsedLibs)); tc.wantModel != nil && diff != "" {
t.Errorf("Parse diff (-want +got):\n%s", diff)
}

results, err := interpreter.Eval(context.Background(), parsedLibs, defaultInterpreterConfig(t, p))
if err != nil {
t.Fatalf("Eval returned unexpected error: %v", err)
}
if diff := cmp.Diff(tc.wantResult, getTESTRESULT(t, results), protocmp.Transform()); diff != "" {
t.Errorf("Eval diff (-want +got)\n%v", diff)
}
})
}
}
1 change: 0 additions & 1 deletion tests/spectests/exclusions/exclusions.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,6 @@ func XMLTestFileExclusionDefinitions() map[string]XMLTestFileExclusions {
// TODO: b/342061715 - unsupported operators.
"Matches",
"ReplaceMatches",
"StartsWith",
"Substring",
},
NamesExcludes: []string{
Expand Down
Loading