Last active
April 8, 2019 22:06
-
-
Save grantr/f9064831f358dcc57c0b3816a606764d to your computer and use it in GitHub Desktop.
CEL int to float conversion bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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