Skip to content

Commit

Permalink
Pipeline: add more template functions
Browse files Browse the repository at this point in the history
- unflatten
- fileExists
- mergeFiles

Signed-off-by: Richard Kosegi <richard.kosegi@gmail.com>
  • Loading branch information
rkosegi committed Aug 9, 2024
1 parent 26fdda0 commit 921373f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 3 deletions.
44 changes: 41 additions & 3 deletions pipeline/template_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ package pipeline

import (
"bytes"
"fmt"
"github.com/rkosegi/yaml-toolkit/analytics"
"github.com/rkosegi/yaml-toolkit/dom"
"github.com/rkosegi/yaml-toolkit/utils"
"os"
"strconv"
"strings"
"text/template"
Expand Down Expand Up @@ -60,12 +64,46 @@ func isEmptyFunc(v interface{}) bool {
return false
}

// un-flatten map
func unflattenFunc(v map[string]interface{}) map[string]interface{} {
return utils.Unflatten(v)
}

// fileExistsFunc checks if files exists.
// Any error is swallowed and will cause function to return false, as if file does not exist.
func fileExistsFunc(f string) bool {
_, err := os.Stat(f)
if err != nil {
return false
}
return true
}

// mergeFilesFunc merges 0 or more files into single map[string]interface{}
func mergeFilesFunc(files ...string) (map[string]interface{}, error) {
ds := analytics.NewDocumentSet()
result := make(map[string]interface{})
for _, f := range files {
err := ds.AddDocumentFromFile(f, analytics.DefaultFileDecoderProvider(f))
if err != nil {
return nil, err
}
}
for k, v := range ds.AsOne().Merged(dom.ListsMergeAppend()).Flatten() {
result[k] = fmt.Sprintf("%v", v.Value())
}
return result, nil
}

func renderTemplate(tmplStr string, data interface{}, fm template.FuncMap) (string, error) {
tmpl := template.New("tmpl").Funcs(fm)
tmpl.Funcs(template.FuncMap{
"tpl": tplFunc(tmpl),
"toYaml": toYamlFunc,
"isEmpty": isEmptyFunc,
"tpl": tplFunc(tmpl),
"toYaml": toYamlFunc,
"isEmpty": isEmptyFunc,
"unflatten": unflattenFunc,
"fileExists": fileExistsFunc,
"mergeFiles": mergeFilesFunc,
})
_, err := tmpl.Parse(tmplStr)
if err != nil {
Expand Down
56 changes: 56 additions & 0 deletions pipeline/template_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package pipeline
import (
sprig "github.com/go-task/slim-sprig/v3"
"github.com/stretchr/testify/assert"
"os"
"testing"
)

Expand Down Expand Up @@ -126,3 +127,58 @@ func TestTemplateFuncIsEmpty(t *testing.T) {
assert.Equal(t, v.res, isEmptyFunc(v.v))
}
}

func TestTemplateFuncUnflatten(t *testing.T) {
r := unflattenFunc(map[string]interface{}{
"a.b": 1,
"c": "hello",
})
assert.Equal(t, 2, len(r))
assert.Equal(t, 1, r["a"].(map[string]interface{})["b"])
assert.Equal(t, "hello", r["c"])
}

func TestTemplateFuncFileExists(t *testing.T) {
assert.False(t, fileExistsFunc("/this/definitely/shouldn't exists"))
f, err := os.CreateTemp("", "yt*.txt")
assert.NoError(t, err)
if err != nil {
return
}
t.Cleanup(func() {
t.Logf("cleanup temporary file %s", f.Name())
_ = os.Remove(f.Name())
})
assert.True(t, fileExistsFunc(f.Name()))
}

func TestTemplateFuncMergeFiles(t *testing.T) {
f1, err := os.CreateTemp("", "yt*.yaml")
assert.NoError(t, err)
assert.NoError(t, os.WriteFile(f1.Name(), []byte("A: 1"), 0o664))
f2, err := os.CreateTemp("", "yt*.json")
assert.NoError(t, err)
assert.NoError(t, os.WriteFile(f2.Name(), []byte("{ \"B\": 2 }"), 0o664))
res, err := mergeFilesFunc(f1.Name(), f2.Name())
assert.NoError(t, err)
assert.NotNil(t, res)
t.Cleanup(func() {
t.Logf("cleanup temporary file %s", f1.Name())
_ = os.Remove(f1.Name())
t.Logf("cleanup temporary file %s", f2.Name())
_ = os.Remove(f2.Name())
})
}

func TestTemplateFuncMergeFilesInvalid(t *testing.T) {
f2, err := os.CreateTemp("", "yt*.json")
assert.NoError(t, err)
assert.NoError(t, os.WriteFile(f2.Name(), []byte("NOT_A_JSON"), 0o664))
res, err := mergeFilesFunc(f2.Name())
assert.Error(t, err)
assert.Nil(t, res)
t.Cleanup(func() {
t.Logf("cleanup temporary file %s", f2.Name())
_ = os.Remove(f2.Name())
})
}

0 comments on commit 921373f

Please sign in to comment.