-
Notifications
You must be signed in to change notification settings - Fork 148
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
Support custom functions #375
Open
HeeManSu
wants to merge
2
commits into
kro-run:main
Choose a base branch
from
HeeManSu:adding-custom-functions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package function | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/google/cel-go/cel" | ||
"github.com/google/cel-go/common/types" | ||
"github.com/google/cel-go/common/types/ref" | ||
xv1alpha1 "github.com/kro-run/kro/api/v1alpha1" | ||
) | ||
|
||
// FunctionRegistry manages custom CEL functions | ||
type FunctionRegistry struct { | ||
functions map[string]*xv1alpha1.Function | ||
} | ||
|
||
// NewFunctionRegistry creates a new function registry | ||
func NewFunctionRegistry() *FunctionRegistry { | ||
return &FunctionRegistry{ | ||
functions: make(map[string]*xv1alpha1.Function), | ||
} | ||
} | ||
|
||
// RegisterFunction adds a function to the registry | ||
func (r *FunctionRegistry) RegisterFunction(fn *xv1alpha1.Function) error { | ||
if _, exists := r.functions[fn.ID]; exists { | ||
return fmt.Errorf("function %s already registered", fn.ID) | ||
} | ||
r.functions[fn.ID] = fn | ||
return nil | ||
} | ||
|
||
// GetCELFunction converts a Function to a CEL function declaration | ||
func (r *FunctionRegistry) GetCELFunction(fn *xv1alpha1.Function) (cel.EnvOption, error) { | ||
|
||
//convert input types to CEL types | ||
celTypes := make([]*cel.Type, len((fn.Inputs))) | ||
envOpts := make([]cel.EnvOption, len(fn.Inputs)) | ||
|
||
for i, inputType := range fn.Inputs { | ||
celType, err := getCelType(inputType) | ||
if err != nil { | ||
return nil, err | ||
} | ||
celTypes[i] = celType | ||
envOpts[i] = cel.Variable(fmt.Sprintf("_%d", i), celType) | ||
} | ||
|
||
// Create CEL environment for function template | ||
env, err := cel.NewEnv(envOpts...) | ||
if err != nil { | ||
return nil, fmt.Errorf("environment creation failed: %w", err) | ||
} | ||
|
||
// Compile function template | ||
ast, iss := env.Compile(fn.Template) | ||
if iss.Err() != nil { | ||
return nil, fmt.Errorf("template compilation failed: %w", iss.Err()) | ||
} | ||
|
||
prg, err := env.Program(ast) | ||
if err != nil { | ||
return nil, fmt.Errorf("program creation failed: %w", err) | ||
} | ||
|
||
// Return CEL function declaration | ||
return cel.Function(fn.ID, | ||
cel.Overload(fn.ID, | ||
celTypes, | ||
ast.OutputType(), | ||
cel.FunctionBinding(func(args ...ref.Val) ref.Val { | ||
vars := map[string]any{} | ||
for i, arg := range args { | ||
vars[fmt.Sprintf("_%d", i)] = arg | ||
} | ||
out, _, err := prg.Eval(vars) | ||
if err != nil { | ||
return types.WrapErr(err) | ||
} | ||
return out | ||
}), | ||
), | ||
), nil | ||
|
||
} | ||
|
||
func getCelType(typeName string) (*cel.Type, error) { | ||
switch typeName { | ||
case "string": | ||
return cel.StringType, nil | ||
case "int": | ||
return cel.IntType, nil | ||
case "bool": | ||
return cel.BoolType, nil | ||
case "double", "float": | ||
return cel.DoubleType, nil | ||
case "bytes": | ||
return cel.BytesType, nil | ||
case "any": | ||
return cel.AnyType, nil | ||
case "list": | ||
return cel.ListType(cel.AnyType), nil | ||
case "map": | ||
return cel.MapType(cel.StringType, cel.AnyType), nil | ||
default: | ||
return nil, fmt.Errorf("unsupported type: %s", typeName) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're ok with making backwards incompatible changes to this (@a-hilaly your input please), we can start including list and map. If not, I suggest we start with just the primitives and add support for lists and maps later, probably when the design for complex types #144 is finished