Skip to content

Commit

Permalink
Move 'delimiter' parameter to configuration on csv datasource (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
dobarx authored Feb 11, 2024
1 parent 6d71278 commit cf5a7da
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
3 changes: 3 additions & 0 deletions examples/templates/example-openai.fabric
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
config data csv {}

document "example-openai" {
title = "Testing plugins"

data csv "csv_file" {
path = "./data.csv"
}
Expand Down
21 changes: 13 additions & 8 deletions internal/builtin/data_csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
}
}
Expand All @@ -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{{
Expand Down
8 changes: 5 additions & 3 deletions internal/builtin/data_csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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})
})
}
Expand Down

0 comments on commit cf5a7da

Please sign in to comment.