Skip to content

Commit

Permalink
Update cel-spec and fix strings.format to be conformant
Browse files Browse the repository at this point in the history
Signed-off-by: Justin King <[email protected]>
  • Loading branch information
jcking committed Jan 26, 2025
1 parent 2f7606a commit a4fa335
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 49 deletions.
4 changes: 2 additions & 2 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ go_repository(
go_repository(
name = "dev_cel_expr",
importpath = "cel.dev/expr",
sum = "h1:NciYrtDRIR0lNCnH1LFJegdjspNx9fI59O7TWcua/W4=",
version = "v0.19.1",
sum = "h1:V354PbqIXr9IQdwy4SYA4xa0HXaWq1BUPAGzugBY5V4=",
version = "v0.19.2",
)

# local_repository(
Expand Down
17 changes: 16 additions & 1 deletion common/types/double.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"fmt"
"math"
"reflect"
"strconv"
"strings"

"github.com/google/cel-go/common/types/ref"

Expand Down Expand Up @@ -136,7 +138,20 @@ func (d Double) ConvertToType(typeVal ref.Type) ref.Val {
case DoubleType:
return d
case StringType:
return String(fmt.Sprintf("%g", float64(d)))
if math.IsInf(float64(d), 1) {
return String("+Inf")
}
if math.IsInf(float64(d), -1) {
return String("-Inf")
}
if math.IsNaN(float64(d)) {
return String("NaN")
}
s := strconv.FormatFloat(float64(d), 'f', -1, 64)
if !strings.ContainsRune(s, '.') {
s = s + ".0"
}
return String(s)
case TypeType:
return DoubleType
}
Expand Down
7 changes: 7 additions & 0 deletions conformance/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ _TESTS_TO_SKIP = [
# Future enhancments.
"enums/strong_proto2",
"enums/strong_proto3",

# Type deductions.
"type_deductions/wrappers/wrapper_promotion_2",
"type_deductions/legacy_nullable_types/null_assignable_to_message_parameter_candidate",
"type_deductions/legacy_nullable_types/null_assignable_to_duration_parameter_candidate",
"type_deductions/legacy_nullable_types/null_assignable_to_timestamp_parameter_candidate",
"type_deductions/legacy_nullable_types/null_assignable_to_abstract_parameter_candidate",
]

go_test(
Expand Down
2 changes: 1 addition & 1 deletion conformance/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/google/cel-go/conformance
go 1.21.1

require (
cel.dev/expr v0.19.1
cel.dev/expr v0.19.2
github.com/bazelbuild/rules_go v0.49.0
github.com/google/cel-go v0.21.0
github.com/google/go-cmp v0.6.0
Expand Down
8 changes: 2 additions & 6 deletions conformance/go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
cel.dev/expr v0.18.0 h1:CJ6drgk+Hf96lkLikr4rFf19WrU0BOWEihyZnI2TAzo=
cel.dev/expr v0.18.0/go.mod h1:MrpN08Q+lEBs+bGYdLxxHkZoUSsCp0nSKTs0nTymJgw=
cel.dev/expr v0.19.0 h1:lXuo+nDhpyJSpWxpPVi5cPUwzKb+dsdOiw6IreM5yt0=
cel.dev/expr v0.19.0/go.mod h1:MrpN08Q+lEBs+bGYdLxxHkZoUSsCp0nSKTs0nTymJgw=
cel.dev/expr v0.19.1 h1:NciYrtDRIR0lNCnH1LFJegdjspNx9fI59O7TWcua/W4=
cel.dev/expr v0.19.1/go.mod h1:MrpN08Q+lEBs+bGYdLxxHkZoUSsCp0nSKTs0nTymJgw=
cel.dev/expr v0.19.2 h1:V354PbqIXr9IQdwy4SYA4xa0HXaWq1BUPAGzugBY5V4=
cel.dev/expr v0.19.2/go.mod h1:MrpN08Q+lEBs+bGYdLxxHkZoUSsCp0nSKTs0nTymJgw=
github.com/antlr4-go/antlr/v4 v4.13.0 h1:lxCg3LAv+EUK6t1i0y1V6/SLeUi0eKEKdhQAlS8TVTI=
github.com/antlr4-go/antlr/v4 v4.13.0/go.mod h1:pfChB/xh/Unjila75QW7+VU4TSnWnnk9UTnmpPaOR2g=
github.com/bazelbuild/rules_go v0.49.0 h1:5vCbuvy8Q11g41lseGJDc5vxhDjJtfxr6nM/IC4VmqM=
Expand Down
5 changes: 2 additions & 3 deletions ext/formatting.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ func clauseForType(argType ref.Type) (clauseImpl, error) {
if !ok {
return "", fmt.Errorf("couldn't convert %s to float64", arg.Type().TypeName())
}
fmtStr := fmt.Sprintf("%%.%df", defaultPrecision)
return fmt.Sprintf(fmtStr, argDouble), nil
return string(types.Double(argDouble).ConvertToType(types.StringType).(types.String)), nil
}, nil
case types.TypeType:
return func(arg ref.Val, locale string) (string, error) {
Expand Down Expand Up @@ -333,7 +332,7 @@ func (c *stringFormatter) Scientific(precision *int) func(ref.Val, string) (stri
return "", fmt.Errorf("error matching locale: %w", err)
}
fmtStr := fmt.Sprintf("%%%de", *precision)
return message.NewPrinter(matchedLocale).Sprintf(fmtStr, argFloat), nil
return strings.ReplaceAll(message.NewPrinter(matchedLocale).Sprintf(fmtStr, argFloat), "\u202f", ""), nil
}
}

Expand Down
38 changes: 10 additions & 28 deletions ext/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ func TestStringFormat(t *testing.T) {
name: "scientific notation formatting clause",
format: "%.6e",
formatArgs: "1052.032911275",
expectedOutput: "1.052033\u202f\u00d7\u202f10\u2070\u00b3",
expectedOutput: "1.052033\u00d710\u2070\u00b3",
expectedRuntimeCost: 11,
expectedEstimatedCost: checker.CostEstimate{Min: 11, Max: 11},
locale: "en_US",
Expand All @@ -688,7 +688,7 @@ func TestStringFormat(t *testing.T) {
name: "default precision for scientific notation",
format: "%e",
formatArgs: "2.71828",
expectedOutput: "2.718280\u202f\u00d7\u202f10\u2070\u2070",
expectedOutput: "2.718280\u00d710\u2070\u2070",
expectedRuntimeCost: 11,
expectedEstimatedCost: checker.CostEstimate{Min: 11, Max: 11},
locale: "en_US",
Expand All @@ -706,25 +706,7 @@ func TestStringFormat(t *testing.T) {
name: "default list precision for string",
format: "%s",
formatArgs: "[2.71]",
expectedOutput: "[2.710000]",
expectedRuntimeCost: 21,
expectedEstimatedCost: checker.CostEstimate{Min: 21, Max: 21},
locale: "en_US",
},
{
name: "default scientific notation for string",
format: "%s",
formatArgs: "0.000000002",
expectedOutput: "2e-09",
expectedRuntimeCost: 11,
expectedEstimatedCost: checker.CostEstimate{Min: 11, Max: 11},
locale: "en_US",
},
{
name: "default list scientific notation for string",
format: "%s",
formatArgs: "[0.000000002]",
expectedOutput: "[0.000000]",
expectedOutput: "[2.71]",
expectedRuntimeCost: 21,
expectedEstimatedCost: checker.CostEstimate{Min: 21, Max: 21},
locale: "en_US",
Expand All @@ -733,7 +715,7 @@ func TestStringFormat(t *testing.T) {
name: "unicode output for scientific notation",
format: "unescaped unicode: %e, escaped unicode: %e",
formatArgs: "2.71828, 2.71828",
expectedOutput: "unescaped unicode: 2.718280 × 10⁰⁰, escaped unicode: 2.718280\u202f\u00d7\u202f10\u2070\u2070",
expectedOutput: "unescaped unicode: 2.718280×10⁰⁰, escaped unicode: 2.718280\u00d710\u2070\u2070",
expectedRuntimeCost: 15,
expectedEstimatedCost: checker.CostEstimate{Min: 15, Max: 15},
locale: "en_US",
Expand Down Expand Up @@ -857,7 +839,7 @@ func TestStringFormat(t *testing.T) {
name: "list support for string",
format: "%s",
formatArgs: `["abc", 3.14, null, [9, 8, 7, 6], timestamp("2023-02-03T23:31:20Z")]`,
expectedOutput: `["abc", 3.140000, null, [9, 8, 7, 6], timestamp("2023-02-03T23:31:20Z")]`,
expectedOutput: `["abc", 3.14, null, [9, 8, 7, 6], timestamp("2023-02-03T23:31:20Z")]`,
expectedRuntimeCost: 32,
expectedEstimatedCost: checker.CostEstimate{Min: 32, Max: 32},
},
Expand All @@ -866,7 +848,7 @@ func TestStringFormat(t *testing.T) {
format: "%s",
formatArgs: `{"key1": b"xyz", "key5": null, "key2": duration("2h"), "key4": true, "key3": 2.71828}`,
locale: "nl_NL",
expectedOutput: `{"key1":b"xyz", "key2":duration("7200s"), "key3":2.718280, "key4":true, "key5":null}`,
expectedOutput: `{"key1":b"xyz", "key2":duration("7200s"), "key3":2.71828, "key4":true, "key5":null}`,
expectedRuntimeCost: 42,
expectedEstimatedCost: checker.CostEstimate{Min: 42, Max: 42},
},
Expand Down Expand Up @@ -996,7 +978,7 @@ func TestStringFormat(t *testing.T) {
dynArgs: map[string]any{
"dynE": 2.71828,
},
expectedOutput: "(dyntype) e: 2.718280\u202f\u00d7\u202f10\u2070\u2070",
expectedOutput: "(dyntype) e: 2.718280\u00d710\u2070\u2070",
expectedRuntimeCost: 13,
expectedEstimatedCost: checker.CostEstimate{Min: 13, Max: 13},
locale: "en_US",
Expand Down Expand Up @@ -1042,7 +1024,7 @@ func TestStringFormat(t *testing.T) {
dynArgs: map[string]any{
"dynList": []any{6, 4.2, "a string"},
},
expectedOutput: `dyntype list: [6, 4.200000, "a string"]`,
expectedOutput: `dyntype list: [6, 4.2, "a string"]`,
expectedRuntimeCost: 13,
expectedEstimatedCost: checker.CostEstimate{Min: 13, Max: 13},
},
Expand Down Expand Up @@ -1433,15 +1415,15 @@ func TestStringFormatHeterogeneousLiterals(t *testing.T) {
}{
{
expr: `"list: %s".format([[[1, 2, [3.0, 4]]]])`,
out: `list: [[1, 2, [3.000000, 4]]]`,
out: `list: [[1, 2, [3.0, 4]]]`,
},
{
expr: `"list size: %d".format([[[1, 2, [3.0, 4]]].size()])`,
out: `list size: 1`,
},
{
expr: `"list element: %s".format([[[1, 2, [3.0, 4]]][0]])`,
out: `list element: [1, 2, [3.000000, 4]]`,
out: `list element: [1, 2, [3.0, 4]]`,
},
}
env, err := cel.NewEnv(Strings(), cel.ASTValidators(cel.ValidateHomogeneousAggregateLiterals()))
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21.1
toolchain go1.23.0

require (
cel.dev/expr v0.19.1
cel.dev/expr v0.19.2
github.com/antlr4-go/antlr/v4 v4.13.0
github.com/stoewer/go-strcase v1.2.0
golang.org/x/text v0.16.0
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
cel.dev/expr v0.18.0 h1:CJ6drgk+Hf96lkLikr4rFf19WrU0BOWEihyZnI2TAzo=
cel.dev/expr v0.18.0/go.mod h1:MrpN08Q+lEBs+bGYdLxxHkZoUSsCp0nSKTs0nTymJgw=
cel.dev/expr v0.19.0 h1:lXuo+nDhpyJSpWxpPVi5cPUwzKb+dsdOiw6IreM5yt0=
cel.dev/expr v0.19.0/go.mod h1:MrpN08Q+lEBs+bGYdLxxHkZoUSsCp0nSKTs0nTymJgw=
cel.dev/expr v0.19.1 h1:NciYrtDRIR0lNCnH1LFJegdjspNx9fI59O7TWcua/W4=
cel.dev/expr v0.19.1/go.mod h1:MrpN08Q+lEBs+bGYdLxxHkZoUSsCp0nSKTs0nTymJgw=
cel.dev/expr v0.19.2 h1:V354PbqIXr9IQdwy4SYA4xa0HXaWq1BUPAGzugBY5V4=
cel.dev/expr v0.19.2/go.mod h1:MrpN08Q+lEBs+bGYdLxxHkZoUSsCp0nSKTs0nTymJgw=
github.com/antlr4-go/antlr/v4 v4.13.0 h1:lxCg3LAv+EUK6t1i0y1V6/SLeUi0eKEKdhQAlS8TVTI=
github.com/antlr4-go/antlr/v4 v4.13.0/go.mod h1:pfChB/xh/Unjila75QW7+VU4TSnWnnk9UTnmpPaOR2g=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
Expand Down
2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# cel.dev/expr v0.19.1
# cel.dev/expr v0.19.2
## explicit; go 1.21.1
cel.dev/expr
# github.com/antlr4-go/antlr/v4 v4.13.0
Expand Down

0 comments on commit a4fa335

Please sign in to comment.