From 9fd919d05e6d73886ad795e67463dac4ecc5818c Mon Sep 17 00:00:00 2001 From: dobarx <111326505+dobarx@users.noreply.github.com> Date: Wed, 3 Apr 2024 18:29:18 +0300 Subject: [PATCH] add sprig template to table content provider (#147) --- internal/builtin/content_list.go | 2 +- internal/builtin/content_list_test.go | 4 +-- internal/builtin/content_table.go | 9 ++++--- internal/builtin/content_table_test.go | 34 ++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/internal/builtin/content_list.go b/internal/builtin/content_list.go index bd14e7ed..e2e4ab1b 100644 --- a/internal/builtin/content_list.go +++ b/internal/builtin/content_list.go @@ -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 } diff --git a/internal/builtin/content_list_test.go b/internal/builtin/content_list_test.go index 83e26469..a7e39636 100644 --- a/internal/builtin/content_list_test.go +++ b/internal/builtin/content_list_test.go @@ -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() @@ -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) } diff --git a/internal/builtin/content_table.go b/internal/builtin/content_table.go index b11177bb..a3dc132b 100644 --- a/internal/builtin/content_table.go +++ b/internal/builtin/content_table.go @@ -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" @@ -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) } @@ -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) } @@ -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) } diff --git a/internal/builtin/content_table_test.go b/internal/builtin/content_table_test.go index fdc44ab1..e0822951 100644 --- a/internal/builtin/content_table_test.go +++ b/internal/builtin/content_table_test.go @@ -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{