Skip to content

Instantly share code, notes, and snippets.

@grantr grantr/main.go
Last active Apr 8, 2019

Embed
What would you like to do?
CEL int to float conversion bug
package main
import (
"fmt"
"log"
"strings"
"github.com/golang/protobuf/jsonpb"
structpb "github.com/golang/protobuf/ptypes/struct"
"github.com/google/cel-go/cel"
"github.com/google/cel-go/checker/decls"
)
func main() {
e, err := cel.NewEnv(
cel.Declarations(
decls.NewIdent("data", decls.NewObjectType("google.protobuf.Struct"), nil),
),
)
if err != nil {
log.Fatalf("environment creation error: %s\n", err)
}
// This doesn't work with an integer, but it works with a float: data.foo == 5.0
p, iss := e.Parse(`data.foo == 5`)
if iss != nil && iss.Err() != nil {
log.Fatalln(iss.Err())
}
c, iss := e.Check(p)
if iss != nil && iss.Err() != nil {
log.Fatalln(iss.Err())
}
prg, err := e.Program(c)
if err != nil {
log.Fatalf("program creation error: %s\n", err)
}
data := structpb.Struct{}
if err := jsonpb.Unmarshal(strings.NewReader(`{"foo":5}`), &data); err != nil {
log.Fatalf("json parse error: %s\n", err)
}
out, _, err := prg.Eval(map[string]interface{}{
"data": &data,
})
if err != nil {
log.Fatalf("runtime error: %s\n", err)
}
fmt.Printf("out: %#v\n", out)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.