Skip to content

Commit

Permalink
feat: add iter template fn for initting inline slices
Browse files Browse the repository at this point in the history
  • Loading branch information
femnad committed Jan 8, 2024
1 parent 56d39ff commit c174fd3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
2 changes: 1 addition & 1 deletion base/fup.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func getConfigReader(config string) (configReader, error) {
}

func evalConfig(data []byte) ([]byte, error) {
tmpl := template.New("config").Funcs(precheck.FactFns)
tmpl := template.New("config").Funcs(precheck.FactFns).Funcs(internal.UtilFns)
parsed, err := tmpl.Parse(string(data))
if err != nil {
return data, err
Expand Down
25 changes: 15 additions & 10 deletions internal/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"text/template"
)

var funcMap = template.FuncMap{
var UtilFns = template.FuncMap{
"cut": cut,
"head": head,
"iter": iterItems,
"revCut": reverseCut,
"split": split,
"splitBy": splitBy,
Expand All @@ -36,6 +37,14 @@ func cut(i int, s string) (string, error) {
return s[i:], nil
}

func head(i int, s string) (string, error) {
return splitBy("\n", i, s)
}

func iterItems(item ...string) []string {
return item
}

func reverseCut(i int, s string) (string, error) {
i, err := absIndex(s, i)
if err != nil {
Expand All @@ -45,6 +54,10 @@ func reverseCut(i int, s string) (string, error) {
return s[:i], nil
}

func split(i int, s string) (string, error) {
return splitBy(" ", i, s)
}

func splitBy(delimiter string, i int, s string) (string, error) {
s = strings.Trim(s, delimiter)
fields := strings.Split(s, delimiter)
Expand All @@ -58,16 +71,8 @@ func splitBy(delimiter string, i int, s string) (string, error) {
return fields[i], nil
}

func head(i int, s string) (string, error) {
return splitBy("\n", i, s)
}

func split(i int, s string) (string, error) {
return splitBy(" ", i, s)
}

func RunTemplateFn(input, tmplFn string) (string, error) {
tmpl := template.New("post-proc").Funcs(funcMap)
tmpl := template.New("post-proc").Funcs(UtilFns)

ctx := struct {
Args string
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

const (
version = "0.27.1"
version = "0.28.0"
)

type args struct {
Expand Down

0 comments on commit c174fd3

Please sign in to comment.