-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
- Loading branch information
1 parent
20d691f
commit b70bf41
Showing
5 changed files
with
112 additions
and
70 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package cel | ||
|
||
import ( | ||
"github.com/google/cel-go/cel" | ||
"github.com/jmespath-community/go-jmespath/pkg/binding" | ||
) | ||
|
||
func Execute(program cel.Program, value any, bindings binding.Bindings) (any, error) { | ||
data := map[string]interface{}{ | ||
"object": value, | ||
"bindings": NewVal(bindings, BindingsType), | ||
} | ||
out, _, err := program.Eval(data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return out.Value(), nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package cel | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/google/cel-go/cel" | ||
"github.com/google/cel-go/common/types" | ||
"github.com/google/cel-go/common/types/ref" | ||
"github.com/jmespath-community/go-jmespath/pkg/binding" | ||
) | ||
|
||
var ( | ||
BindingsType = cel.OpaqueType("bindings") | ||
DefaultEnv = sync.OnceValues(func() (*cel.Env, error) { | ||
return cel.NewEnv( | ||
cel.Variable("object", cel.DynType), | ||
cel.Variable("bindings", BindingsType), | ||
cel.Function("resolve", | ||
cel.MemberOverload("bindings_resolve_string", | ||
[]*cel.Type{BindingsType, cel.StringType}, | ||
cel.AnyType, | ||
cel.BinaryBinding(func(lhs, rhs ref.Val) ref.Val { | ||
bindings, ok := lhs.(Val[binding.Bindings]) | ||
if !ok { | ||
return types.ValOrErr(bindings, "invalid bindings type") | ||
} | ||
name, ok := rhs.(types.String) | ||
if !ok { | ||
return types.ValOrErr(name, "invalid name type") | ||
} | ||
value, err := binding.Resolve("$"+string(name), bindings.Unwrap()) | ||
if err != nil { | ||
return types.WrapErr(err) | ||
} | ||
return types.DefaultTypeAdapter.NativeToValue(value) | ||
}), | ||
), | ||
), | ||
) | ||
}) | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package cel | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/google/cel-go/common/types" | ||
"github.com/google/cel-go/common/types/ref" | ||
) | ||
|
||
type Val[T comparable] struct { | ||
inner T | ||
celType ref.Type | ||
} | ||
|
||
func NewVal[T comparable](value T, celType ref.Type) Val[T] { | ||
return Val[T]{ | ||
inner: value, | ||
celType: celType, | ||
} | ||
} | ||
|
||
func (w Val[T]) Unwrap() T { | ||
return w.inner | ||
} | ||
|
||
func (w Val[T]) Value() interface{} { | ||
return w.Unwrap() | ||
} | ||
|
||
func (w Val[T]) ConvertToNative(typeDesc reflect.Type) (interface{}, error) { | ||
panic("not required") | ||
} | ||
|
||
func (w Val[T]) ConvertToType(typeVal ref.Type) ref.Val { | ||
panic("not required") | ||
} | ||
|
||
func (w Val[T]) Equal(other ref.Val) ref.Val { | ||
o, ok := other.Value().(Val[T]) | ||
if !ok { | ||
return types.ValOrErr(other, "no such overload") | ||
} | ||
return types.Bool(o == w) | ||
} | ||
|
||
func (w Val[T]) Type() ref.Type { | ||
return w.celType | ||
} |
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
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