Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codelab module and codelab refactors #567

Merged
merged 5 commits into from Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions codelab/solution/codelab.go
Expand Up @@ -223,7 +223,7 @@ func exercise5() {
out, _, _ := eval(
program,
map[string]interface{}{
"now": &tpb.Timestamp{Seconds: time.Now().Unix()},
"now": time.Now(),
},
)
// The output of the program is a CEL map type, but it can be converted
Expand Down Expand Up @@ -282,7 +282,7 @@ func exercise6() {
"group": "admin",
},
},
"now": &tpb.Timestamp{Seconds: time.Now().Unix()},
"now": time.Now(),
},
)
// Unwrap the CEL value to a proto. Make sure to use the `ConvertToNative` to convert
Expand Down
101 changes: 49 additions & 52 deletions examples/README.md
Expand Up @@ -6,14 +6,13 @@ Evaluate expression `"Hello world! I'm " + name + "."` with `CEL` passed as
name.

```go
import (
"github.com/google/cel-go/cel"
"github.com/google/cel-go/checker/decls"
)

d := cel.Declarations(decls.NewVar("name", decls.String))
env, err := cel.NewEnv(d)
import "github.com/google/cel-go/cel"

env, err := cel.NewEnv(cel.Variable("name", cel.StringType))
// Check iss for compilation errors.
TristonianJones marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Fatalln(err)
}
ast, iss := env.Compile(`"Hello world! I'm " + name + "."`)
// Check iss for compilation errors.
if iss.Err() != nil {
Expand All @@ -40,35 +39,31 @@ Evaluate expression `i.greet(you)` with:
```

First we need to declare two string variables and `greet` function.
`NewInstanceOverload` must be used if we want to declare function which will
operate on a type. First element of slice passed as `argTypes` into
`NewInstanceOverload` is declaration of instance type. Next elements are
parameters of function.
`Function` must be used if we want to declare an extension to CEL. The
`MemberOverload` declares an overload id, a list of arguments where the
first element the `argTypes` slice is the target type of the member
function. The remaining argument types are the signature of the member
method.

```go
decls.NewVar("i", decls.String),
decls.NewVar("you", decls.String),
decls.NewFunction("greet",
decls.NewInstanceOverload("string_greet_string",
[]*exprpb.Type{decls.String, decls.String},
decls.String))
... // Create env and compile
```

Let's implement `greet` function and pass it to `program`. We will be using
`Binary`, because `greet` function uses 2 parameters (1st instance, 2nd
function parameter).

```go
greetFunc := &functions.Overload{
Operator: "string_greet_string",
Binary: func(lhs ref.Val, rhs ref.Val) ref.Val {
return types.String(
fmt.Sprintf("Hello %s! Nice to meet you, I'm %s.\n", rhs, lhs))
}}
prg, err := env.Program(c, cel.Functions(greetFunc))

out, _, err := prg.Eval(map[string]interface{}{
env, _ := cel.NewEnv(
cel.Variable("i", cel.StringType),
cel.Variable("you", cel.StringType),
cel.Function("greet",
cel.MemberOverload("string_greet_string",
[]*cel.Type{cel.StringType, cel.StringType},
cel.StringType,
cel.BinaryBinding(func (lhs, rhs ref.Val) ref.Val {
return types.String(
fmt.Sprintf("Hello %s! Nice to meet you, I'm %s.\n", rhs, lhs))
},
),
),
),
)
// Create env and compile
TristonianJones marked this conversation as resolved.
Show resolved Hide resolved
prg, _ := env.Program(c)
out, _, _ := prg.Eval(map[string]interface{}{
"i": "CEL",
"you": "world",
})
Expand All @@ -87,26 +82,28 @@ Evaluate expression `shake_hands(i,you)` with:
shake_hands -> "%s and %s are shaking hands."
```

In order to declare global function we need to use `NewOverload`:
In order to declare global function we need to use `Overload` instead
of `MemberOverload` in the `Function` option:

```go
decls.NewVar("i", decls.String),
decls.NewVar("you", decls.String),
decls.NewFunction("shake_hands",
decls.NewOverload("shake_hands_string_string",
[]*exprpb.Type{decls.String, decls.String},
decls.String))
... // Create env and compile.

shakeFunc := &functions.Overload{
Operator: "shake_hands_string_string",
Binary: func(lhs ref.Val, rhs ref.Val) ref.Val {
return types.String(
fmt.Sprintf("%s and %s are shaking hands.\n", lhs, rhs))
}}
prg, err := env.Program(c, cel.Functions(shakeFunc))

out, _, err := prg.Eval(map[string]interface{}{
env, _ := cel.NewEnv(
cel.Variable("i", cel.StringType),
cel.Variable("you", cel.StringType),
cel.Function("shake_hands",
cel.Overload("shake_hands_string_string",
[]*cel.Type{cel.StringType, cel.StringType},
cel.StringType,
cel.BinaryBinding(func(lhs, rhs ref.Val) ref.Val {
return types.String(
fmt.Sprintf("%s and %s are shaking hands.\n", lhs, rhs))
},
),
),
),
)
// Create env and compile.
prg, _ := env.Program(c)
out, _, _ := prg.Eval(map[string]interface{}{
"i": "CEL",
"you": "world",
})
Expand Down
35 changes: 16 additions & 19 deletions examples/custom_global_function_test.go
Expand Up @@ -19,23 +19,26 @@ import (
"log"

"github.com/google/cel-go/cel"
"github.com/google/cel-go/checker/decls"
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
"github.com/google/cel-go/interpreter/functions"

exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
)

func ExampleCustomGlobalFunction() {
d := cel.Declarations(
decls.NewVar("i", decls.String),
decls.NewVar("you", decls.String),
decls.NewFunction("shake_hands",
decls.NewOverload("shake_hands_string_string",
[]*exprpb.Type{decls.String, decls.String},
decls.String)))
env, err := cel.NewEnv(d)
env, err := cel.NewEnv(
cel.Variable("i", cel.StringType),
cel.Variable("you", cel.StringType),
cel.Function("shake_hands",
cel.Overload("shake_hands_string_string",
[]*cel.Type{cel.StringType, cel.StringType},
cel.StringType,
cel.BinaryBinding(func(lhs, rhs ref.Val) ref.Val {
return types.String(
fmt.Sprintf("%s and %s are shaking hands.\n", lhs, rhs))
},
),
),
),
)
if err != nil {
log.Fatalf("environment creation error: %v\n", err)
}
Expand All @@ -44,13 +47,7 @@ func ExampleCustomGlobalFunction() {
if iss.Err() != nil {
log.Fatalln(iss.Err())
}
shakeFunc := &functions.Overload{
Operator: "shake_hands_string_string",
Binary: func(lhs ref.Val, rhs ref.Val) ref.Val {
return types.String(
fmt.Sprintf("%s and %s are shaking hands.\n", lhs, rhs))
}}
prg, err := env.Program(ast, cel.Functions(shakeFunc))
prg, err := env.Program(ast)
if err != nil {
log.Fatalf("Program creation error: %v\n", err)
}
Expand Down
35 changes: 13 additions & 22 deletions examples/custom_instance_function_test.go
Expand Up @@ -19,12 +19,8 @@ import (
"log"

"github.com/google/cel-go/cel"
"github.com/google/cel-go/checker/decls"
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
"github.com/google/cel-go/interpreter/functions"

exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
)

func ExampleCustomInstanceFunction() {
Expand Down Expand Up @@ -58,26 +54,21 @@ type customLib struct{}

func (customLib) CompileOptions() []cel.EnvOption {
return []cel.EnvOption{
cel.Declarations(
decls.NewVar("i", decls.String),
decls.NewVar("you", decls.String),
decls.NewFunction("greet",
decls.NewInstanceOverload("string_greet_string",
[]*exprpb.Type{decls.String, decls.String},
decls.String))),
}
}

func (customLib) ProgramOptions() []cel.ProgramOption {
return []cel.ProgramOption{
cel.Functions(
&functions.Overload{
Operator: "string_greet_string",
Binary: func(lhs ref.Val, rhs ref.Val) ref.Val {
cel.Variable("i", cel.StringType),
cel.Variable("you", cel.StringType),
cel.Function("greet",
cel.MemberOverload("string_greet_string",
[]*cel.Type{cel.StringType, cel.StringType},
cel.StringType,
cel.BinaryBinding(func(lhs, rhs ref.Val) ref.Val {
return types.String(
fmt.Sprintf("Hello %s! Nice to meet you, I'm %s.\n", rhs, lhs))
},
},
}),
),
),
}
}

func (customLib) ProgramOptions() []cel.ProgramOption {
return []cel.ProgramOption{}
}
4 changes: 1 addition & 3 deletions examples/simple_test.go
Expand Up @@ -19,12 +19,10 @@ import (
"log"

"github.com/google/cel-go/cel"
"github.com/google/cel-go/checker/decls"
)

func ExampleSimple() {
d := cel.Declarations(decls.NewVar("name", decls.String))
env, err := cel.NewEnv(d)
env, err := cel.NewEnv(cel.Variable("name", cel.StringType))
if err != nil {
log.Fatalf("environment creation error: %v\n", err)
}
Expand Down