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

Add input expressions documentation #107

Merged
merged 3 commits into from
Feb 17, 2018
Merged
Changes from 1 commit
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
Prev Previous commit
Removed expr.current() in favor of expr.task()
erwinvaneyk committed Feb 17, 2018
commit f0ca6ebc46cc6e30226c91cde76b989f2a0c5f22
5 changes: 2 additions & 3 deletions Docs/input-expressions.md
Original file line number Diff line number Diff line change
@@ -121,9 +121,8 @@ name | Usage | Description
uid | `uid()` | Generates a unique (string) id
input | `input("taskId", "key")` | Gets the input of a task for the given key. If no key is provided, the default key is used.
output | `output("taskId")` | Gets the output of a task. If no argument is provided the output of the current task is returned.
param | `param("key")` | Gets the invocation param for the given key.
task | `task("taskId")` | Gets the task for the given taskId.
me
param | `param("key")` | Gets the invocation param for the given key. If no key is provided, the default key is used.
task | `task("taskId")` | Gets the task for the given taskId. If no argument is provided the current task is returned.

### Adding Custom Function
The JavaScript expression interpreter is fully extensible, allowing you to add your own functions to the existing
72 changes: 39 additions & 33 deletions pkg/controller/expr/functions.go
Original file line number Diff line number Diff line change
@@ -11,12 +11,11 @@ import (

// Built-in functions for the expression parser
var BuiltinFunctions = map[string]Function{
"uid": &UidFn{},
"input": &InputFn{},
"output": &OutputFn{},
"param": &ParamFn{},
"task": &TaskFn{},
"current": &CurrentFn{},
"uid": &UidFn{},
"input": &InputFn{},
"output": &OutputFn{},
"param": &ParamFn{},
"task": &TaskFn{},
}

// UidFn provides a function to generate a unique (string) id
@@ -33,22 +32,28 @@ func (qf *UidFn) Apply(vm *otto.Otto, call otto.FunctionCall) otto.Value {
type InputFn struct{}

// Apply gets the input of a task for the given key. If no key is provided, the default key is used.
// If no argument is provided at all, the default key of the current task will be used.
func (qf *InputFn) Apply(vm *otto.Otto, call otto.FunctionCall) otto.Value {
var task, inputKey string
switch len(call.ArgumentList) {
case 0:
return otto.UndefinedValue()
task = varCurrentTask
fallthrough
case 1:
inputKey = types.INPUT_MAIN
fallthrough
case 2:
fallthrough
default:
task = call.Argument(0).String()
// Set task if argument provided
if len(call.ArgumentList) > 0 {
task = fmt.Sprintf("\"%s\"", call.Argument(0).String())
}
// Set input key if argument provided
if len(call.ArgumentList) > 1 {
inputKey = call.Argument(1).String()
}
lookup := fmt.Sprintf("$.Tasks.%s.Inputs.%s", task, inputKey)
lookup := fmt.Sprintf("$.Tasks[%s].Inputs[\"%s\"]", task, inputKey)
result, err := vm.Eval(lookup)
if err != nil {
logrus.Warnf("Failed to lookup input: %s", lookup)
@@ -69,10 +74,11 @@ func (qf *OutputFn) Apply(vm *otto.Otto, call otto.FunctionCall) otto.Value {
task = varCurrentTask
fallthrough
default:
if len(task) == 0 {
// Set task if argument provided
if len(call.ArgumentList) > 0 {
task = fmt.Sprintf("\"%s\"", call.Argument(0).String())
}
lookup := fmt.Sprintf("$.Tasks.%s.Output", task)
lookup := fmt.Sprintf("$.Tasks[%s].Output", task)
result, err := vm.Eval(lookup)
if err != nil {
logrus.Warnf("Failed to lookup output: %s", lookup)
@@ -82,17 +88,23 @@ func (qf *OutputFn) Apply(vm *otto.Otto, call otto.FunctionCall) otto.Value {
}
}

// ParmFn provides a function to get the invocation param for the given key
// ParmFn provides a function to get the invocation param for the given key. If no key is provided, the default key
// is used.
type ParamFn struct{}

// Apply gets the invocation param for the given key
// Apply gets the invocation param for the given key. If no key is provided, the default key is used.
func (qf *ParamFn) Apply(vm *otto.Otto, call otto.FunctionCall) otto.Value {
var key string
switch len(call.ArgumentList) {
case 0:
return otto.UndefinedValue()
key = types.INPUT_MAIN
fallthrough
default:
param := call.Argument(0).String()
lookup := fmt.Sprintf("$.Invocation.Inputs.%s", param)
// Set key if argument provided
if len(call.ArgumentList) > 0 {
key = call.Argument(0).String()
}
lookup := fmt.Sprintf("$.Invocation.Inputs[\"%s\"]", key)
result, err := vm.Eval(lookup)
if err != nil {
logrus.Warnf("Failed to lookup param: %s", lookup)
@@ -102,17 +114,23 @@ func (qf *ParamFn) Apply(vm *otto.Otto, call otto.FunctionCall) otto.Value {
}
}

// TaskFn provides a function to get a task for the given taskId.
// TaskFn provides a function to get a task for the given taskId. If no argument is provided the current task is
// returned.
type TaskFn struct{}

// Apply gets the task for the given taskId.
// Apply gets the task for the given taskId. If no argument is provided the current task is returned.
func (qf *TaskFn) Apply(vm *otto.Otto, call otto.FunctionCall) otto.Value {
var task string
switch len(call.ArgumentList) {
case 0:
return otto.UndefinedValue()
task = varCurrentTask
fallthrough
default:
param := call.Argument(0).String()
lookup := fmt.Sprintf("$.Tasks.%s", param)
// Set task if argument provided
if len(call.ArgumentList) > 0 {
task = fmt.Sprintf("\"%s\"", call.Argument(0).String())
}
lookup := fmt.Sprintf("$.Tasks[%s]", task)
result, err := vm.Eval(lookup)
if err != nil {
logrus.Warnf("Failed to lookup param: %s", lookup)
@@ -121,15 +139,3 @@ func (qf *TaskFn) Apply(vm *otto.Otto, call otto.FunctionCall) otto.Value {
return result
}
}

type CurrentFn struct{}

func (ct *CurrentFn) Apply(vm *otto.Otto, call otto.FunctionCall) otto.Value {
lookup := fmt.Sprintf("$.Tasks[%s]", varCurrentTask)
result, err := vm.Eval(lookup)
if err != nil {
logrus.Warnf("Failed to lookup param: %s", lookup)
return otto.UndefinedValue()
}
return result
}