mirror of
https://github.com/hatchet-dev/hatchet.git
synced 2026-05-03 07:59:31 -05:00
feat(go-sdk): add ability to create workflows from the Go SDK more easily, quickstart improvements (#30)
* feat: add initial docs site * feat: allow workflows to be defined from go sdk * fix release action * chore: remove server dependencies from client * fix: use correct certificate for server * chore: add port and bind address to grpc config * fix: add env for grpc config * fix: nil pointer when output is null * chore: support variation in output args * fix unresolve merge conflict * fix: quickstart improvements * temp remove database url * fix: action id not required for event * fix: actionid validation for events * Remove deleted files
This commit is contained in:
@@ -2,42 +2,63 @@ package datautils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
// RenderTemplateFields recursively processes the input map, rendering any string fields using the data map.
|
||||
func RenderTemplateFields(data map[string]interface{}, input map[string]interface{}) error {
|
||||
func RenderTemplateFields(data map[string]interface{}, input map[string]interface{}) (map[string]interface{}, error) {
|
||||
output := map[string]interface{}{}
|
||||
|
||||
for key, val := range input {
|
||||
switch v := val.(type) {
|
||||
case string:
|
||||
tmpl, err := template.New(key).Parse(v)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating template for key %s: %v", key, err)
|
||||
return nil, fmt.Errorf("error creating template for key %s: %v", key, err)
|
||||
}
|
||||
|
||||
var tpl bytes.Buffer
|
||||
err = tmpl.Execute(&tpl, data)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("error executing template for key %s: %v", key, err)
|
||||
return nil, fmt.Errorf("error executing template for key %s: %v", key, err)
|
||||
}
|
||||
|
||||
res := tpl.String()
|
||||
|
||||
// if the string can be unmarshalled into a map[string]interface{}, do so
|
||||
resMap := map[string]interface{}{}
|
||||
|
||||
if err := json.Unmarshal(tpl.Bytes(), &resMap); err == nil {
|
||||
output[key] = resMap
|
||||
|
||||
// if the key is "object", the entire input is replaced with the rendered value
|
||||
if key == "object" {
|
||||
// note we do not recursively render the new input, as it may contain untrusted data.
|
||||
return resMap, nil
|
||||
}
|
||||
} else {
|
||||
output[key] = res
|
||||
}
|
||||
input[key] = tpl.String()
|
||||
case map[string]interface{}:
|
||||
// if we hit a nested map[string]interface{}, render those recursively
|
||||
err := RenderTemplateFields(data, v)
|
||||
recOut, err := RenderTemplateFields(data, v)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
output[key] = recOut
|
||||
default:
|
||||
if reflect.TypeOf(v).Kind() == reflect.Map {
|
||||
// If it's a map but not map[string]interface{}, return an error
|
||||
return fmt.Errorf("encountered a map that is not map[string]interface{}: %s", key)
|
||||
return nil, fmt.Errorf("encountered a map that is not map[string]interface{}: %s", key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return output, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user