Skip to content

Commit

Permalink
Add isNan and isInf (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfus authored Aug 23, 2023
1 parent b7524f0 commit 5a628a0
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
45 changes: 45 additions & 0 deletions internal/celext/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package celext

import (
"bytes"
"math"
"net"
"net/mail"
"net/url"
Expand Down Expand Up @@ -70,6 +71,50 @@ func (l lib) CompileOptions() []cel.EnvOption {
l.uniqueMemberOverload(cel.StringType, l.uniqueScalar),
l.uniqueMemberOverload(cel.BytesType, l.uniqueBytes),
),
cel.Function("isNan",
cel.MemberOverload(
"double_is_nan_bool",
[]*cel.Type{cel.DoubleType},
cel.BoolType,
cel.UnaryBinding(func(value ref.Val) ref.Val {
num, ok := value.Value().(float64)
if !ok {
return types.UnsupportedRefValConversionErr(value)
}
return types.Bool(math.IsNaN(num))
}),
),
),
cel.Function("isInf",
cel.MemberOverload(
"double_is_inf_bool",
[]*cel.Type{cel.DoubleType},
cel.BoolType,
cel.UnaryBinding(func(value ref.Val) ref.Val {
num, ok := value.Value().(float64)
if !ok {
return types.UnsupportedRefValConversionErr(value)
}
return types.Bool(math.IsInf(num, 0))
}),
),
cel.MemberOverload(
"double_int_is_inf_bool",
[]*cel.Type{cel.DoubleType, cel.IntType},
cel.BoolType,
cel.BinaryBinding(func(lhs ref.Val, rhs ref.Val) ref.Val {
num, ok := lhs.Value().(float64)
if !ok {
return types.UnsupportedRefValConversionErr(lhs)
}
sign, ok := rhs.Value().(int64)
if !ok {
return types.UnsupportedRefValConversionErr(rhs)
}
return types.Bool(math.IsInf(num, int(sign)))
}),
),
),
cel.Function("isHostname",
cel.MemberOverload(
"string_is_hostname_bool",
Expand Down
15 changes: 14 additions & 1 deletion internal/celext/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,26 @@ func TestCELLib(t *testing.T) {
env, err := cel.NewEnv(cel.Lib(lib{}))
require.NoError(t, err)

t.Run("unique", func(t *testing.T) {
t.Run("ext", func(t *testing.T) {
t.Parallel()

tests := []struct {
expr string
ex bool
}{
{"0.0.isInf()", false},
{"0.0.isNan()", false},
{"(1.0/0.0).isInf()", true},
{"(1.0/0.0).isInf(0)", true},
{"(1.0/0.0).isInf(1)", true},
{"(1.0/0.0).isInf(-1)", false},
{"(-1.0/0.0).isInf()", true},
{"(-1.0/0.0).isInf(0)", true},
{"(-1.0/0.0).isInf(1)", false},
{"(-1.0/0.0).isInf(-1)", true},
{"(0.0/0.0).isNan()", true},
{"(0.0/0.0).isInf()", false},
{"(1.0/0.0).isNan()", false},
{
"[].unique()",
true,
Expand Down

0 comments on commit 5a628a0

Please sign in to comment.