Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions examples/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

package(
default_visibility = ["//visibility:public"],
licenses = ["notice"], # Apache 2.0
)

go_library(
name = "go_default_library",
srcs = ["types.go"],
importpath = "cel.dev/cel-go/examples",
)

go_test(
name = "examples_test",
size = "small",
srcs = [
"example_cel_advanced_test.go",
"example_cel_collections_test.go",
"example_cel_compile_test.go",
"example_cel_context_eval_test.go",
"example_cel_custom_functions_test.go",
"example_cel_custom_macros_test.go",
"example_cel_execution_cost_test.go",
"example_cel_logic_and_conditions_test.go",
"example_cel_native_structs_test.go",
"example_cel_operators_test.go",
"example_cel_protocol_buffers_test.go",
"example_cel_strings_and_numbers_test.go",
"example_cel_time_test.go",
"example_cel_transforming_data_test.go",
"example_cel_type_conversions_test.go",
],
embed = [
":go_default_library",
],
deps = [
"//cel:go_default_library",
"//checker:go_default_library",
"//common:go_default_library",
"//common/ast:go_default_library",
"//common/operators:go_default_library",
"//common/types:go_default_library",
"//common/types/ref:go_default_library",
"//ext:go_default_library",
"//parser:go_default_library",
"//test/proto3pb:go_default_library",
"@org_golang_google_protobuf//types/known/structpb:go_default_library",
"@org_golang_google_protobuf//types/known/wrapperspb:go_default_library",
],
)
22 changes: 6 additions & 16 deletions examples/example_cel_native_structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,21 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package examples
package examples_test

import (
"fmt"
"log"
"reflect"

"cel.dev/cel-go/cel"
"cel.dev/cel-go/examples"
"cel.dev/cel-go/ext"
)

type User struct {
Name string `json:"name"`
Age int `json:"age"`
Roles []string `json:"roles"`
}

type Account struct {
ID int64 `cel:"id"`
OwnerName string `cel:"owner"`
}

// Example_cel_NativeTypes showcases evaluating Go native structs using ext.NativeTypes() with json struct tags
func Example_cel_NativeTypes() {
u := User{
u := examples.User{
Name: "Alice",
Age: 30,
Roles: []string{"admin", "editor"},
Expand All @@ -52,7 +42,7 @@ func Example_cel_NativeTypes() {
for _, expr := range exprs {
prg, err := cel.Compile(expr,
cel.Variable("user", cel.ObjectType("examples.User")),
ext.NativeTypes(reflect.TypeOf(User{}), ext.ParseStructTag("json")),
ext.NativeTypes(reflect.TypeFor[examples.User](), ext.ParseStructTag("json")),
)
if err != nil {
log.Fatalf("cel.Compile() error for %q: %v", expr, err)
Expand All @@ -73,14 +63,14 @@ func Example_cel_NativeTypes() {

// Example_cel_NativeTypes_structTags showcases mapping struct field names via cel tags
func Example_cel_NativeTypes_structTags() {
acc := Account{
acc := examples.Account{
ID: 1001,
OwnerName: "Bob",
}

prg, err := cel.Compile(`acc.owner == "Bob" && acc.id == 1001`,
cel.Variable("acc", cel.ObjectType("examples.Account")),
ext.NativeTypes(reflect.TypeFor[Account](), ext.ParseStructTags(true)),
ext.NativeTypes(reflect.TypeFor[examples.Account](), ext.ParseStructTags(true)),
)
if err != nil {
log.Fatalf("cel.Compile() error: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion examples/example_cel_protocol_buffers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func Example_cel_WellKnownTypes() {
}

opts := []cel.EnvOption{
cel.Types(wrapperspb.String(""), structVal),
cel.Types(structVal),
cel.Variable("wrapped_str", cel.ObjectType("google.protobuf.StringValue")),
cel.Variable("json_obj", cel.ObjectType("google.protobuf.Struct")),
}
Expand Down
2 changes: 1 addition & 1 deletion examples/example_cel_type_conversions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package examples
package examples_test

import (
"fmt"
Expand Down
17 changes: 13 additions & 4 deletions ext/security/jwt/export_test.go → examples/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,18 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package jwt
// Package examples provides example types and functions used in examples.
package examples

// NewJWTLib constructs an internal jwtLib instance for testing.
func NewJWTLib() *jwtLib {
return &jwtLib{}
// User is a sample user struct with fields tagged for JSON serialization.
type User struct {
Name string `json:"name"`
Age int `json:"age"`
Roles []string `json:"roles"`
}

// Account is a sample account struct with fields tagged for CEL field access.
type Account struct {
ID int64 `cel:"id"`
OwnerName string `cel:"owner"`
}
78 changes: 0 additions & 78 deletions ext/security/hmac/hmac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ package hmac_test
import (
"crypto"
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
Expand Down Expand Up @@ -437,82 +435,6 @@ func TestHMACSpecificAlgorithmOptions(t *testing.T) {
}
}

func TestHMACCustomAlgorithmOption(t *testing.T) {
env, err := cel.NewEnv(
hmaclib.Library(
hmaclib.Algorithm(crypto.MD5, "MD5", "HASH-MD5"),
hmaclib.Algorithm(crypto.SHA1, "SHA1"),
),
cel.Variable("msg", cel.StringType),
cel.Variable("secret", cel.StringType),
)
if err != nil {
t.Fatalf("cel.NewEnv failed: %v", err)
}

msgStr := "hello custom alg"
secretStr := "key"
vars := map[string]any{
"msg": msgStr,
"secret": secretStr,
}

hMD5 := hmac.New(md5.New, []byte(secretStr))
hMD5.Write([]byte(msgStr))
macMD5Bytes := hMD5.Sum(nil)
macMD5Hex := hex.EncodeToString(macMD5Bytes)

hSHA1 := hmac.New(sha1.New, []byte(secretStr))
hSHA1.Write([]byte(msgStr))
macSHA1Hex := hex.EncodeToString(hSHA1.Sum(nil))

tests := []struct {
name string
expr string
want any
}{
{
name: "md5_constant",
expr: `hmac.MD5`,
want: "MD5",
},
{
name: "sha1_constant",
expr: `hmac.SHA1`,
want: "SHA1",
},
{
name: "compute_custom_md5",
expr: `hmac.compute(msg, secret, hmac.MD5)`,
want: macMD5Bytes,
},
{
name: "compute_custom_md5_alias",
expr: `hmac.compute(msg, secret, 'HASH-MD5')`,
want: macMD5Bytes,
},
{
name: "verify_custom_md5_prefixed",
expr: `hmac.verify(msg, 'md5=` + macMD5Hex + `', secret, hmac.MD5)`,
want: true,
},
{
name: "verify_custom_sha1_prefixed",
expr: `hmac.verify(msg, 'sha1=` + macSHA1Hex + `', secret, hmac.SHA1)`,
want: true,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := evalExpr(t, env, tc.expr, vars)
if !reflect.DeepEqual(got, tc.want) {
t.Errorf("Eval(%q) = %v (%T), want %v (%T)", tc.expr, got, got, tc.want, tc.want)
}
})
}
}

func TestHMACCommonAlgorithmsOption(t *testing.T) {
env, err := cel.NewEnv(
hmaclib.Library(hmaclib.CommonAlgorithms()),
Expand Down
1 change: 0 additions & 1 deletion ext/security/jwt/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ go_test(
name = "go_default_test",
size = "small",
srcs = [
"export_test.go",
"jwt_test.go",
],
embed = [
Expand Down
11 changes: 8 additions & 3 deletions ext/security/jwt/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,17 @@ func defaultNowFunc() time.Time {
return time.Now().UTC()
}

// Library returns a cel.EnvOption to configure extended functions for JWT data handling and claims inspection.
func Library(options ...Option) cel.EnvOption {
l := &jwtLib{
// NewJWTLib constructs an internal jwtLib instance for direct use.
func NewJWTLib() *jwtLib {
return &jwtLib{
version: ^uint32(0),
now: defaultNowFunc,
}
}

// Library returns a cel.EnvOption to configure extended functions for JWT data handling and claims inspection.
func Library(options ...Option) cel.EnvOption {
l := NewJWTLib()
for _, o := range options {
l = o(l)
}
Expand Down
2 changes: 1 addition & 1 deletion test/async.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func FakeRPC(timeout time.Duration) func(context.Context, ...ref.Val) ref.Val {
in := args[0].(types.String)
return in.Add(types.String(" success!"))
case <-rpcCtx.Done():
return types.NewErr(rpcCtx.Err().Error())
return types.WrapErr(rpcCtx.Err())
}
}
}
1 change: 1 addition & 0 deletions test/proto3pb/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package(
"//cel:__subpackages__",
"//checker:__subpackages__",
"//common:__subpackages__",
"//examples:__subpackages__",
"//ext:__subpackages__",
"//interpreter:__subpackages__",
"//parser:__subpackages__",
Expand Down
Loading