Skip to content

Commit

Permalink
fix: duration compatibility with cel
Browse files Browse the repository at this point in the history
  • Loading branch information
davenewza committed Jan 30, 2025
1 parent e7f8bfa commit d34280a
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 578 deletions.
8 changes: 8 additions & 0 deletions expressions/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var typeCompatibilityMapping = map[string][][]*types.Type{
{typing.Boolean, types.BoolType},
{types.NewListType(types.StringType), typing.TextArray, typing.IDArray, typing.MarkdownArray},
{types.NewListType(types.IntType), types.NewListType(types.DoubleType), typing.NumberArray, typing.DecimalArray},
{typing.Duration},
},
operators.NotEquals: {
{types.StringType, typing.Text, typing.ID, typing.Markdown},
Expand All @@ -33,28 +34,35 @@ var typeCompatibilityMapping = map[string][][]*types.Type{
{typing.Boolean, types.BoolType},
{types.NewListType(types.StringType), typing.TextArray, typing.IDArray, typing.MarkdownArray},
{types.NewListType(types.IntType), types.NewListType(types.DoubleType), typing.NumberArray, typing.DecimalArray},
{typing.Duration},
},
operators.Greater: {
{types.IntType, types.DoubleType, typing.Number, typing.Decimal},
{typing.Date, typing.Timestamp, types.TimestampType},
{typing.Duration},
},
operators.GreaterEquals: {
{types.IntType, types.DoubleType, typing.Number, typing.Decimal},
{typing.Date, typing.Timestamp, types.TimestampType},
{typing.Duration},
},
operators.Less: {
{types.IntType, types.DoubleType, typing.Number, typing.Decimal},
{typing.Date, typing.Timestamp, types.TimestampType},
{typing.Duration},
},
operators.LessEquals: {
{types.IntType, types.DoubleType, typing.Number, typing.Decimal},
{typing.Date, typing.Timestamp, types.TimestampType},
{typing.Duration},
},
operators.Add: {
{types.IntType, types.DoubleType, typing.Number, typing.Decimal},
{typing.Duration},
},
operators.Subtract: {
{types.IntType, types.DoubleType, typing.Number, typing.Decimal},
{typing.Duration},
},
operators.Multiply: {
{types.IntType, types.DoubleType, typing.Number, typing.Decimal},
Expand Down
1 change: 1 addition & 0 deletions expressions/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ func typesAssignable(expected *types.Type, actual *types.Type) bool {
typing.Text.String(): {mapType(typing.Text.String()), mapType(typing.Markdown.String()), mapType(typing.ID.String())},
typing.Number.String(): {mapType(typing.Number.String()), mapType(typing.Decimal.String())},
typing.Decimal.String(): {mapType(typing.Number.String()), mapType(typing.Decimal.String())},
typing.Duration.String(): {mapType(typing.Duration.String()), mapType(typing.Text.String())},
}

// Check if there are specific compatibility rules for the expected type
Expand Down
6 changes: 4 additions & 2 deletions expressions/typing/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ var (
Boolean = cel.OpaqueType(parser.FieldTypeBoolean)
Timestamp = cel.OpaqueType(parser.FieldTypeTimestamp)
Date = cel.OpaqueType(parser.FieldTypeDate)
Duration = cel.OpaqueType(parser.FieldTypeDuration)
)

var (
IDArray = cel.OpaqueType(fmt.Sprintf("%s[]", parser.FieldTypeID))
TextArray = cel.OpaqueType(fmt.Sprintf("%s[]", parser.FieldTypeText))
Expand All @@ -31,6 +31,7 @@ var (
BooleanArray = cel.OpaqueType(fmt.Sprintf("%s[]", parser.FieldTypeBoolean))
TimestampArray = cel.OpaqueType(fmt.Sprintf("%s[]", parser.FieldTypeTimestamp))
DateArray = cel.OpaqueType(fmt.Sprintf("%s[]", parser.FieldTypeDate))
DurationArray = cel.OpaqueType(fmt.Sprintf("%s[]", parser.FieldTypeDuration))
)

var (
Expand Down Expand Up @@ -64,7 +65,8 @@ func MapType(schema []*parser.AST, typeName string, isRepeated bool) (*types.Typ
parser.FieldTypeFile,
parser.FieldTypeVector,
parser.FieldTypeSecret,
parser.FieldTypePassword:
parser.FieldTypePassword,
parser.FieldTypeDuration:
if isRepeated {
return cel.OpaqueType(fmt.Sprintf("%s[]", typeName)), nil
} else {
Expand Down
3 changes: 2 additions & 1 deletion migrations/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,8 @@ func toSqlLiteral(value any, field *proto.Field) (string, error) {
return fmt.Sprintf("%d", value), nil
case field.Type.Type == proto.Type_TYPE_BOOL:
return fmt.Sprintf("%v", value), nil

case field.Type.Type == proto.Type_TYPE_DURATION:
return db.QuoteLiteral(fmt.Sprintf("%s", value)), nil
default:
return "", fmt.Errorf("field %s has unexpected default value %s", field.Name, value)
}
Expand Down
2 changes: 1 addition & 1 deletion node/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ export interface PersonTable {
height: number
bio: string
file: FileDbRecord
heightInMetres: number
canHoldBreath: runtime.Duration
heightInMetres: number
id: Generated<string>
createdAt: Generated<Date>
updatedAt: Generated<Date>
Expand Down
7 changes: 0 additions & 7 deletions schema/parser/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ const (
FieldTypeDuration = "Duration" // a time duration
)

var ComparableTypes = [][]string{
{FieldTypeDate, FieldTypeTimestamp},
{FieldTypeMarkdown, FieldTypeText},
{FieldTypeDuration, FieldTypeText},
{FieldTypeDecimal, FieldTypeNumber},
}

// Types for Message fields
const (
MessageFieldTypeAny = "Any"
Expand Down
2 changes: 1 addition & 1 deletion schema/testdata/errors/set_attribute_built_in_fields.keel
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ model Post {
@set(post.createdAt = ctx.now)
//expect-error:18:32:AttributeExpressionError:Cannot set the field 'updatedAt' as it is a built-in field and can only be mutated internally
@set(post.updatedAt = ctx.now)
//expect-error:18:39:E026:post.timeToRead is Duration and 123 is Number
//expect-error:36:39:AttributeExpressionError:expression expected to resolve to type Duration but it is Number
@set(post.timeToRead = 123)
}
create createPost2() with (name, published, publisher.name) {
Expand Down
Loading

0 comments on commit d34280a

Please sign in to comment.