From 0f82a07dca162c1fa1383eac672d8beb54827138 Mon Sep 17 00:00:00 2001 From: oleiade Date: Fri, 31 Jan 2025 15:53:46 +0100 Subject: [PATCH] Apply Pull Request suggestions --- .../js/modules/k6/experimental/csv/module.go | 2 +- .../js/modules/k6/experimental/csv/reader.go | 19 ++++++++++--------- .../k6/experimental/csv/reader_test.go | 4 ++-- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/internal/js/modules/k6/experimental/csv/module.go b/internal/js/modules/k6/experimental/csv/module.go index 3da524e163f..fecd5cc4327 100644 --- a/internal/js/modules/k6/experimental/csv/module.go +++ b/internal/js/modules/k6/experimental/csv/module.go @@ -99,7 +99,7 @@ func (mi *ModuleInstance) Parse(file sobek.Value, options sobek.Value) *sobek.Pr rt := mi.vu.Runtime() - // 1. Make sure the Sobek object is a fs.File (sobek operation) + // 1. Make sure the Sobek object is a fs.File (Sobek operation) var fileObj fs.File if err := mi.vu.Runtime().ExportTo(file, &fileObj); err != nil { reject(fmt.Errorf("first argument expected to be a fs.File instance, got %T instead", file)) diff --git a/internal/js/modules/k6/experimental/csv/reader.go b/internal/js/modules/k6/experimental/csv/reader.go index d26159424f0..449d2b51c18 100644 --- a/internal/js/modules/k6/experimental/csv/reader.go +++ b/internal/js/modules/k6/experimental/csv/reader.go @@ -21,8 +21,9 @@ type Reader struct { // options holds the reader's options. options options - // header stores the column names when header option is enabled. - header []string + // columnNames stores the column names when the asObjects option is enabled + // in order to be able to map each row values to their corresponding column. + columnNames []string } // NewReaderFrom creates a new CSV reader from the provided io.Reader. @@ -60,7 +61,7 @@ func NewReaderFrom(r io.Reader, options options) (*Reader, error) { if err != nil { return nil, fmt.Errorf("failed to read the first line; reason: %w", err) } - reader.header = header + reader.columnNames = header reader.currentLine.Add(1) } @@ -106,19 +107,19 @@ func (r *Reader) Read() (any, error) { r.currentLine.Add(1) - // If option is enabled, return a map of the record. + // If header option is enabled, return a map of the record. if r.options.AsObjects.Valid && r.options.AsObjects.Bool { - if r.header == nil { - return nil, fmt.Errorf("the '' option is enabled, but no header was found") + if r.columnNames == nil { + return nil, fmt.Errorf("the 'asObjects' option is enabled, but no header was found") } - if len(record) != len(r.header) { - return nil, fmt.Errorf("record length (%d) doesn't match header length (%d)", len(record), len(r.header)) + if len(record) != len(r.columnNames) { + return nil, fmt.Errorf("record length (%d) doesn't match header length (%d)", len(record), len(r.columnNames)) } recordMap := make(map[string]string) for i, value := range record { - recordMap[r.header[i]] = value + recordMap[r.columnNames[i]] = value } return recordMap, nil diff --git a/internal/js/modules/k6/experimental/csv/reader_test.go b/internal/js/modules/k6/experimental/csv/reader_test.go index 06b7c756751..fc9301c47a5 100644 --- a/internal/js/modules/k6/experimental/csv/reader_test.go +++ b/internal/js/modules/k6/experimental/csv/reader_test.go @@ -79,8 +79,8 @@ func TestNewReaderFrom(t *testing.T) { options{AsObjects: null.NewBool(true, true)}, ) require.NoError(t, err) - assert.NotNil(t, r.header) - assert.Equal(t, []string{"lastname", "firstname", "composer", "born", "died", "dates"}, r.header) + assert.NotNil(t, r.columnNames) + assert.Equal(t, []string{"lastname", "firstname", "composer", "born", "died", "dates"}, r.columnNames) assert.Equal(t, r.currentLine.Load(), int64(1)) })