Skip to content

Commit

Permalink
add sprig template to table content provider (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
dobarx authored Apr 3, 2024
1 parent cb306f8 commit 9fd919d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion internal/builtin/content_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func renderListContent(format string, tmpl *template.Template, datactx plugin.Ma
var buf bytes.Buffer
for i, item := range items {
tmpbuf := bytes.Buffer{}
err := tmpl.Execute(&tmpbuf, item)
err := tmpl.Execute(&tmpbuf, item.Any())
if err != nil {
return "", err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/builtin/content_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (s *ListGeneratorTestSuite) TestBasic() {

func (s *ListGeneratorTestSuite) TestAdvanced() {
args := cty.ObjectVal(map[string]cty.Value{
"item_template": cty.StringVal("foo {{.bar}} {{.baz}}"),
"item_template": cty.StringVal("foo {{.bar}} {{.baz | upper}}"),
"format": cty.NullVal(cty.String),
})
ctx := context.Background()
Expand All @@ -166,7 +166,7 @@ func (s *ListGeneratorTestSuite) TestAdvanced() {
},
},
})
s.Equal("* foo bar1 baz1\n* foo bar2 baz2\n", result.Content.Print())
s.Equal("* foo bar1 BAZ1\n* foo bar2 BAZ2\n", result.Content.Print())
s.Empty(diags)
}

Expand Down
9 changes: 5 additions & 4 deletions internal/builtin/content_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"text/template"

"github.com/Masterminds/sprig/v3"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/zclconf/go-cty/cty"
Expand Down Expand Up @@ -78,11 +79,11 @@ func parseTableContentArgs(params *plugin.ProvideContentParams) (headers, values
return nil, nil, fmt.Errorf("missing value in table cell")
}

headerTmpl, err := template.New("header").Parse(header.AsString())
headerTmpl, err := template.New("header").Funcs(sprig.FuncMap()).Parse(header.AsString())
if err != nil {
return nil, nil, fmt.Errorf("failed to parse header template: %w", err)
}
valueTmpl, err := template.New("value").Parse(value.AsString())
valueTmpl, err := template.New("value").Funcs(sprig.FuncMap()).Parse(value.AsString())
if err != nil {
return nil, nil, fmt.Errorf("failed to parse value template: %w", err)
}
Expand All @@ -97,7 +98,7 @@ func renderTableContent(headers, values []tableCellTmpl, datactx plugin.MapData)
vstr := [][]string{}
for i, header := range headers {
var buf bytes.Buffer
err := header.Execute(&buf, datactx)
err := header.Execute(&buf, datactx.Any())
if err != nil {
return "", fmt.Errorf("failed to render header: %w", err)
}
Expand All @@ -117,7 +118,7 @@ func renderTableContent(headers, values []tableCellTmpl, datactx plugin.MapData)
rowstr := make([]string, len(values))
for i, value := range values {
var buf bytes.Buffer
err := value.Execute(&buf, row)
err := value.Execute(&buf, row.Any())
if err != nil {
return "", fmt.Errorf("failed to render value: %w", err)
}
Expand Down
34 changes: 34 additions & 0 deletions internal/builtin/content_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,40 @@ func (s *TableGeneratorTestSuite) TestBasic() {
s.Nil(diags)
}

func (s *TableGeneratorTestSuite) TestSprigTemplate() {
args := cty.ObjectVal(map[string]cty.Value{
"columns": cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"header": cty.StringVal("{{.col_prefix | upper}} Name"),
"value": cty.StringVal("{{.name | upper}}"),
}),
cty.ObjectVal(map[string]cty.Value{
"header": cty.StringVal("{{.col_prefix}} Age"),
"value": cty.StringVal("{{.age}}"),
}),
}),
})
ctx := context.Background()
result, diags := s.schema.ContentFunc(ctx, &plugin.ProvideContentParams{
Args: args,
DataContext: plugin.MapData{
"col_prefix": plugin.StringData("User"),
"query_result": plugin.ListData{
plugin.MapData{
"name": plugin.StringData("John"),
"age": plugin.NumberData(42),
},
plugin.MapData{
"name": plugin.StringData("Jane"),
"age": plugin.NumberData(43),
},
},
},
})
s.Equal("|USER Name|User Age|\n|---|---|\n|JOHN|42|\n|JANE|43|\n", result.Content.Print())
s.Nil(diags)
}

func (s *TableGeneratorTestSuite) TestMissingHeader() {
args := cty.ObjectVal(map[string]cty.Value{
"columns": cty.ListVal([]cty.Value{
Expand Down

0 comments on commit 9fd919d

Please sign in to comment.