Skip to content

Commit

Permalink
Extension to support Golang structs as CEL types (#612)
Browse files Browse the repository at this point in the history
 Implementation of a Golang native type provider
  • Loading branch information
TristonianJones authored Dec 1, 2022
1 parent db2d910 commit 35783e9
Show file tree
Hide file tree
Showing 8 changed files with 1,151 additions and 17 deletions.
4 changes: 2 additions & 2 deletions cel/cel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1878,7 +1878,7 @@ func TestDynamicDispatch(t *testing.T) {

func TestOptionalValues(t *testing.T) {
env, err := NewEnv(
OptionalTypes(true),
OptionalTypes(),
// Container and test message types.
Container("google.expr.proto2.test"),
Types(&proto2pb.TestAllTypes{}),
Expand Down Expand Up @@ -2205,7 +2205,7 @@ func TestOptionalValues(t *testing.T) {

func BenchmarkOptionalValues(b *testing.B) {
env, err := NewEnv(
OptionalTypes(true),
OptionalTypes(),
Variable("x", OptionalType(IntType)),
Variable("y", OptionalType(IntType)),
Variable("z", IntType),
Expand Down
5 changes: 0 additions & 5 deletions cel/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,11 +485,6 @@ func (e *Env) configure(opts []EnvOption) (*Env, error) {
if err != nil {
return nil, err
}
// If optional types have been enabled, then configure the optional library.
e, err = e.maybeApplyFeature(featureOptionalTypes, Lib(optionalLibrary{}))
if err != nil {
return nil, err
}

// Initialize all of the functions configured within the environment.
for _, fn := range e.functions {
Expand Down
2 changes: 1 addition & 1 deletion cel/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (optionalLibrary) CompileOptions() []EnvOption {
return opt.GetValue()
}))),
Function("hasValue",
MemberOverload("optional_hasValue", []*Type{optionalTypeV}, paramTypeV,
MemberOverload("optional_hasValue", []*Type{optionalTypeV}, BoolType,
UnaryBinding(func(value ref.Val) ref.Val {
opt := value.(*types.Optional)
return types.Bool(opt.HasValue())
Expand Down
10 changes: 5 additions & 5 deletions cel/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,11 +542,11 @@ func DefaultUTCTimeZone(enabled bool) EnvOption {
return features(featureDefaultUTCTimeZone, enabled)
}

// OptionalTypes determines whether CEL's optional value type is enabled. The optional value type makes it
// possible to express whether variables have been provided, whether a result has been computed, and
// in the future whether an object field path, map key value, or list index has a value.
func OptionalTypes(enabled bool) EnvOption {
return features(featureOptionalTypes, enabled)
// OptionalTypes enable support for optional syntax and types in CEL. The optional value type makes
// it possible to express whether variables have been provided, whether a result has been computed,
// and in the future whether an object field path, map key value, or list index has a value.
func OptionalTypes() EnvOption {
return Lib(optionalLibrary{})
}

// features sets the given feature flags. See list of Feature constants above.
Expand Down
2 changes: 0 additions & 2 deletions common/types/ref/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ type TypeProvider interface {

// FieldFieldType returns the field type for a checked type value. Returns
// false if the field could not be found.
//
// Used during type-checking only.
FindFieldType(messageType string, fieldName string) (*FieldType, bool)

// NewValue creates a new type value from a qualified name and map of field
Expand Down
Loading

0 comments on commit 35783e9

Please sign in to comment.