Enable the empty list sum support.

This commit is contained in:
Cici Huang 2022-07-12 19:22:33 +00:00
parent ae6243b431
commit 2f0e5af40b
2 changed files with 1 additions and 34 deletions

View File

@ -182,38 +182,6 @@ func isSorted(val ref.Val) ref.Val {
return types.True
}
func dynSum() functions.UnaryOp {
return func(val ref.Val) ref.Val {
iterable, ok := val.(traits.Iterable)
if !ok {
return types.MaybeNoSuchOverloadErr(val)
}
it := iterable.Iterator()
var initval ref.Val
if it.HasNext() == types.True {
first := it.Next()
switch first.Type() {
case types.IntType:
initval = types.Int(0)
case types.UintType:
initval = types.Uint(0)
case types.DoubleType:
initval = types.Double(0.0)
case types.DurationType:
initval = types.Duration{Duration: 0}
default:
return types.MaybeNoSuchOverloadErr(first)
}
} else {
initval = types.Int(0)
}
initFn := func() ref.Val {
return initval
}
return sum(initFn)(val)
}
}
func sum(init func() ref.Val) functions.UnaryOp {
return func(val ref.Val) ref.Val {
i := init()

View File

@ -1605,8 +1605,7 @@ func TestValidationExpressions(t *testing.T) {
"dyn([1, 2]).sum() == 3",
"dyn([1.0, 2.0]).sum() == 3.0",
// TODO: enable once type system fix it made to CEL
//"[].sum() == 0", // An empty list returns an 0 int
"[].sum() == 0", // An empty list returns an 0 int
},
errors: map[string]string{
// return an error for min/max on empty list