From cf5a7da3ecfeb75cb0eff6c9c16734a6766d26ff Mon Sep 17 00:00:00 2001 From: dobarx <111326505+dobarx@users.noreply.github.com> Date: Sun, 11 Feb 2024 17:47:22 +0200 Subject: [PATCH] Move 'delimiter' parameter to configuration on csv datasource (#77) --- examples/templates/example-openai.fabric | 3 +++ internal/builtin/data_csv.go | 21 +++++++++++++-------- internal/builtin/data_csv_test.go | 8 +++++--- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/examples/templates/example-openai.fabric b/examples/templates/example-openai.fabric index 9663b600..2e41b6eb 100644 --- a/examples/templates/example-openai.fabric +++ b/examples/templates/example-openai.fabric @@ -1,5 +1,8 @@ +config data csv {} + document "example-openai" { title = "Testing plugins" + data csv "csv_file" { path = "./data.csv" } diff --git a/internal/builtin/data_csv.go b/internal/builtin/data_csv.go index 51ae348d..b1d6ccfc 100644 --- a/internal/builtin/data_csv.go +++ b/internal/builtin/data_csv.go @@ -19,17 +19,19 @@ const defaultCSVDelimiter = ',' func makeCSVDataSource() *plugin.DataSource { return &plugin.DataSource{ DataFunc: fetchCSVData, + Config: &hcldec.ObjectSpec{ + "delimiter": &hcldec.AttrSpec{ + Name: "delimiter", + Type: cty.String, + Required: false, + }, + }, Args: &hcldec.ObjectSpec{ "path": &hcldec.AttrSpec{ Name: "path", Type: cty.String, Required: true, }, - "delimiter": &hcldec.AttrSpec{ - Name: "delimiter", - Type: cty.String, - Required: false, - }, }, } } @@ -42,9 +44,12 @@ func fetchCSVData(ctx context.Context, params *plugin.RetrieveDataParams) (plugi Summary: "path is required", }} } - delim := params.Args.GetAttr("delimiter") - if delim.IsNull() { - delim = cty.StringVal(string(defaultCSVDelimiter)) + delim := cty.StringVal(string(defaultCSVDelimiter)) + if !params.Config.IsNull() { + delim = params.Config.GetAttr("delimiter") + if delim.IsNull() { + delim = cty.StringVal(string(defaultCSVDelimiter)) + } } if len(delim.AsString()) != 1 { return nil, hcl.Diagnostics{{ diff --git a/internal/builtin/data_csv_test.go b/internal/builtin/data_csv_test.go index 17887b03..9f2174eb 100644 --- a/internal/builtin/data_csv_test.go +++ b/internal/builtin/data_csv_test.go @@ -17,7 +17,7 @@ func Test_makeCSVDataSchema(t *testing.T) { require.NotNil(t, schema, "expected data source csv") assert.NotNil(t, schema.DataFunc) assert.NotNil(t, schema.Args) - assert.Nil(t, schema.Config) + assert.NotNil(t, schema.Config) } func Test_fetchCSVData(t *testing.T) { @@ -193,11 +193,13 @@ func Test_fetchCSVData(t *testing.T) { delim = cty.NullVal(cty.String) } args := cty.ObjectVal(map[string]cty.Value{ - "path": cty.StringVal(tc.path), + "path": cty.StringVal(tc.path), + }) + cfg := cty.ObjectVal(map[string]cty.Value{ "delimiter": delim, }) ctx := context.Background() - data, diags := p.RetrieveData(ctx, "csv", &plugin.RetrieveDataParams{Args: args}) + data, diags := p.RetrieveData(ctx, "csv", &plugin.RetrieveDataParams{Config: cfg, Args: args}) assert.Equal(t, tc.expected, results{data, diags}) }) }