Skip to content

Commit

Permalink
[core] Make JSON template functions available in all contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
teo committed Apr 9, 2020
1 parent 75d7067 commit 23ec77b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
2 changes: 1 addition & 1 deletion core/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func (t *Task) BuildPropertyMap(bindMap channel.BindMap) (propMap controlcommand
return
}

objStack := template.MakeJsonConversionFuncMap()
objStack := make(map[string]interface{})
objStack["GetConfig"] = template.MakeGetConfigFunc(varStack)

for k, v := range t.GetProperties() {
Expand Down
2 changes: 1 addition & 1 deletion core/workflow/iteratorrange.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (f *iteratorRangeExpr) GetRange(varStack map[string]string) (ran []string,
fields := template.Fields{
template.WrapPointer(&f.Range),
}
err = fields.Execute("", varStack, template.MakeJsonConversionFuncMap(), make(map[string]texttemplate.Template))
err = fields.Execute("", varStack, make(map[string]interface{}), make(map[string]texttemplate.Template))
if err != nil {
return
}
Expand Down
41 changes: 18 additions & 23 deletions core/workflow/template/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,6 @@ func MakeGetConfigFunc(varStack map[string]string) GetConfigFunc {
}
}

func MakeJsonConversionFuncMap() map[string]interface{} {
return map[string]interface{} {
"FromJson": func(in string) (out interface{}) {
bytes := []byte(in)
err := yaml.Unmarshal(bytes, out)
if err != nil {
log.WithError(err).Warn("error unmarshaling JSON/YAML in template system")
return
}
return
},
"ToJson": func(in interface{}) (out string) {
bytes, err := json.Marshal(in)
if err != nil {
log.WithError(err).Warn("error marshaling JSON/YAML in template system")
return
}
out = string(bytes)
return
},
}
}

func MakeStrOperationFuncMap() map[string]interface{} {
return map[string]interface{}{
"Atoi": func(in string) (out int) {
Expand All @@ -99,5 +76,23 @@ func MakeStrOperationFuncMap() map[string]interface{} {
out = strings.TrimSpace(in)
return
},
"FromJson": func(in string) (out interface{}) {
bytes := []byte(in)
err := yaml.Unmarshal(bytes, out)
if err != nil {
log.WithError(err).Warn("error unmarshaling JSON/YAML in template system")
return
}
return
},
"ToJson": func(in interface{}) (out string) {
bytes, err := json.Marshal(in)
if err != nil {
log.WithError(err).Warn("error marshaling JSON/YAML in template system")
return
}
out = string(bytes)
return
},
}
}

0 comments on commit 23ec77b

Please sign in to comment.