Skip to content

Commit

Permalink
feat: this variable in expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
davenewza committed Jan 16, 2025
1 parent d4a309f commit db8a2e0
Show file tree
Hide file tree
Showing 9 changed files with 441 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ model Post {
@where(whereArg == post.title)
}
list listPostsNotEqualString(whereArg: Text) {
@where(post.title != whereArg)
@where(this.title != whereArg)
}
list listPostsEqualDate(whereArg: Date) {
@where(post.aDate == whereArg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ model Post {
@set(post.identity = ctx.identity)
}
list listWithTextPermissionLiteral(isActive) {
@permission(expression: post.title == "hello")
@permission(expression: this.title == "hello")
}
list listWithNumberPermissionLiteral(isActive) {
@permission(expression: post.views == 1)
Expand Down
1 change: 1 addition & 0 deletions schema/attributes/computed.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func ValidateComputedExpression(schema []*parser.AST, model *parser.ModelNode, f
opts := []expressions.Option{
options.WithSchemaTypes(schema),
options.WithVariable(strcase.ToLowerCamel(model.Name.Value), model.Name.Value, false),
options.WithVariable(parser.ThisVariable, model.Name.Value, false),
options.WithComparisonOperators(),
options.WithLogicalOperators(),
options.WithArithmeticOperators(),
Expand Down
1 change: 1 addition & 0 deletions schema/attributes/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func ValidatePermissionExpression(schema []*parser.AST, model *parser.ModelNode,

if model != nil {
opts = append(opts, options.WithVariable(strcase.ToLowerCamel(model.Name.Value), model.Name.Value, false))
opts = append(opts, options.WithVariable(parser.ThisVariable, model.Name.Value, false))
}

p, err := expressions.NewParser(opts...)
Expand Down
1 change: 1 addition & 0 deletions schema/attributes/where.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func ValidateWhereExpression(schema []*parser.AST, action *parser.ActionNode, ex
options.WithSchemaTypes(schema),
options.WithActionInputs(schema, action),
options.WithVariable(strcase.ToLowerCamel(model.Name.Value), model.Name.Value, false),
options.WithVariable(parser.ThisVariable, model.Name.Value, false),
options.WithComparisonOperators(),
options.WithLogicalOperators(),
options.WithReturnTypeAssertion(parser.FieldTypeBoolean, false),
Expand Down
22 changes: 22 additions & 0 deletions schema/attributes/where_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,28 @@ func TestWhere_Valid(t *testing.T) {
require.Empty(t, issues)
}

func TestWhere_This(t *testing.T) {
schema := parse(t, &reader.SchemaFile{FileName: "test.keel", Contents: `
model Person {
fields {
name Text
isActive Boolean
}
actions {
list listPeople(name) {
@where(this.name == "Keel")
}
}
}`})

action := query.Action(schema, "listPeople")
expression := action.Attributes[0].Arguments[0].Expression

issues, err := attributes.ValidateWhereExpression(schema, action, expression)
require.NoError(t, err)
require.Empty(t, issues)
}

func TestWhere_InvalidType(t *testing.T) {
schema := parse(t, &reader.SchemaFile{FileName: "test.keel", Contents: `
model Person {
Expand Down
4 changes: 4 additions & 0 deletions schema/parser/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ const (
AttributeComputed = "computed"
)

const (
ThisVariable = "this"
)

const (
OrderByAscending = "asc"
OrderByDescending = "desc"
Expand Down
Loading

0 comments on commit db8a2e0

Please sign in to comment.