Skip to content
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

Filtering Declaration Options for Constants #277

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
86 changes: 85 additions & 1 deletion filtering/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package filtering
import (
"testing"

syntaxv1 "go.einride.tech/aip/proto/gen/einride/example/syntax/v1"
expr "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
"gotest.tools/v3/assert"

syntaxv1 "go.einride.tech/aip/proto/gen/einride/example/syntax/v1"
)

func TestChecker(t *testing.T) {
Expand Down Expand Up @@ -412,6 +414,88 @@ func TestChecker(t *testing.T) {
},
},

{
filter: "IsGreat = true",
declarations: []DeclarationOption{
DeclareStandardFunctions(),
DeclareIdent("IsGreat", TypeBool),
DeclareConstant("true", TypeBool, &expr.Constant{ConstantKind: &expr.Constant_BoolValue{BoolValue: true}}),
},
},

{
filter: "IsGreat = true",
declarations: []DeclarationOption{
DeclareStandardFunctions(),
DeclareStandardBoolConstants(),
DeclareIdent("IsGreat", TypeBool),
},
},

{
filter: "IsGreat = True",
declarations: []DeclarationOption{
DeclareStandardFunctions(),
DeclareStandardBoolConstants(),
DeclareIdent("IsGreat", TypeBool),
},
},

{
filter: "IsGreat = TRUE",
declarations: []DeclarationOption{
DeclareStandardFunctions(),
DeclareStandardBoolConstants(),
DeclareIdent("IsGreat", TypeBool),
},
},

{
filter: "IsGreat = false",
declarations: []DeclarationOption{
DeclareStandardFunctions(),
DeclareIdent("IsGreat", TypeBool),
DeclareConstant("false", TypeBool, &expr.Constant{ConstantKind: &expr.Constant_BoolValue{BoolValue: true}}),
},
},

{
filter: "IsGreat = false",
declarations: []DeclarationOption{
DeclareStandardFunctions(),
DeclareStandardBoolConstants(),
DeclareIdent("IsGreat", TypeBool),
},
},

{
filter: "IsGreat = False",
declarations: []DeclarationOption{
DeclareStandardFunctions(),
DeclareStandardBoolConstants(),
DeclareIdent("IsGreat", TypeBool),
},
},

{
filter: "IsGreat = FALSE",
declarations: []DeclarationOption{
DeclareStandardFunctions(),
DeclareStandardBoolConstants(),
DeclareIdent("IsGreat", TypeBool),
},
},

{
filter: "IsGreat = FalsE",
declarations: []DeclarationOption{
DeclareStandardFunctions(),
DeclareStandardBoolConstants(),
DeclareIdent("IsGreat", TypeBool),
},
errorContains: "undeclared identifier 'FalsE'",
},

{
filter: "<",
errorContains: "unexpected token <",
Expand Down
29 changes: 28 additions & 1 deletion filtering/declarations.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type Declarations struct {
// DeclarationOption configures Declarations.
type DeclarationOption func(*Declarations) error

// DeclareStandardFunction is a DeclarationOption that declares all standard functions and their overloads.
// DeclareStandardFunctions is a DeclarationOption that declares all standard functions and their overloads.
func DeclareStandardFunctions() DeclarationOption {
return func(declarations *Declarations) error {
for _, declaration := range StandardFunctionDeclarations() {
Expand All @@ -85,6 +85,25 @@ func DeclareStandardFunctions() DeclarationOption {
}
}

// DeclareStandardBoolConstants is a DeclarationOption that adds a number of variations of "true" and "false" as constant values.
func DeclareStandardBoolConstants() DeclarationOption {
return func(declarations *Declarations) error {
for n, v := range map[string]bool{
"true": true,
"True": true,
"TRUE": true,
"false": false,
"False": false,
"FALSE": false,
} {
if err := declarations.declareConstant(n, TypeBool, &expr.Constant{ConstantKind: &expr.Constant_BoolValue{BoolValue: v}}); err != nil {
return err
}
}
return nil
}
}

// DeclareFunction is a DeclarationOption that declares a single function and its overloads.
func DeclareFunction(name string, overloads ...*expr.Decl_FunctionDecl_Overload) DeclarationOption {
return func(declarations *Declarations) error {
Expand All @@ -99,12 +118,20 @@ func DeclareIdent(name string, t *expr.Type) DeclarationOption {
}
}

// DeclareEnumIdent is a DeclarationOption that declares an enumeration.
func DeclareEnumIdent(name string, enumType protoreflect.EnumType) DeclarationOption {
return func(declarations *Declarations) error {
return declarations.declareEnumIdent(name, enumType)
}
}

// DeclareConstant is a DeclarationOption that adds a constant value to the declarations.
func DeclareConstant(name string, constantType *expr.Type, constantValue *expr.Constant) DeclarationOption {
return func(declarations *Declarations) error {
return declarations.declareConstant(name, constantType, constantValue)
}
}

// NewDeclarations creates a new set of Declarations for filter expression type-checking.
func NewDeclarations(opts ...DeclarationOption) (*Declarations, error) {
d := &Declarations{
Expand Down