Skip to content

Commit

Permalink
Apply Pull Request suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
oleiade committed Jan 31, 2025
1 parent a5ec9ff commit 0f82a07
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion internal/js/modules/k6/experimental/csv/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
19 changes: 10 additions & 9 deletions internal/js/modules/k6/experimental/csv/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/js/modules/k6/experimental/csv/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})

Expand Down

0 comments on commit 0f82a07

Please sign in to comment.