Skip to content

Commit

Permalink
feat: Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
szkiba committed May 12, 2021
1 parent 7aca71e commit 0dc46e6
Show file tree
Hide file tree
Showing 17 changed files with 1,676 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/k6
.task
node_modules
42 changes: 42 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# MIT License
#
# Copyright (c) 2021 Iván Szkiba
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

linters:
presets:
- bugs
- style
- unused
- complexity
- format
- performance
enable:
- exportloopref
disable:
- nolintlint
- exhaustivestruct
- gochecknoglobals
- gochecknoinits
- lll
- maligned
- interfacer
- scopelint
- wrapcheck
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"go.lintTool": "golangci-lint",
"go.lintFlags": ["--fast"]
}
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# xk6-csv

A k6 extension enables k6 tests to comfortably parse CSV values.

The underlying implementation is https://github.com/gocarina/gocsv

Built for [k6](https://github.com/loadimpact/k6) using [xk6](https://github.com/k6io/xk6).

## Usage

Import an entire module's contents:
```JavaScript
import * as CSV from "k6/x/csv";
```

Import a single export from a module:
```JavaScript
import { parse } from "k6/x/csv";
```

## API

Functions:

- [parse](docs/README.md#parse)

For complete API documentation click [here](docs/README.md)!

## Build

To build a `k6` binary with this extension, first ensure you have the prerequisites:

- [Go toolchain](https://go101.org/article/go-toolchain.html)
- Git

Then:

1. Download `xk6`:
```bash
$ go get -u github.com/k6io/xk6
```

2. Build the binary:
```bash
$ xk6 build --with github.com/szkiba/xk6-csv
```
62 changes: 62 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# MIT License
#
# Copyright (c) 2021 Iván Szkiba
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

version: "3"

env:
K6_VERSION: v0.31.1

silent: true

tasks:
default:
cmds:
- task: test

clean:
desc: Clean up working directory
cmds:
- rm -rf k6 .task node_modules

build:
sources:
- "**/*.go"
generates:
- k6
cmds:
- xk6 build --with github.com/szkiba/xk6-csv=$(pwd)

test:
deps: [build]
cmds:
- ./k6 run --no-usage-report test/csv.test.js

npm:install:
cmds:
- npm ci
status:
- test -d node_modules

docs:
deps: [npm:install]
cmds:
- npx typedoc
86 changes: 86 additions & 0 deletions csv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// MIT License
//
// Copyright (c) 2021 Iván Szkiba
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package csv

import (
"context"
"encoding/csv"
"errors"
"fmt"
"io"
"strings"

"github.com/loadimpact/k6/js/modules"
)

// Register the extensions on module initialization.
func init() {
modules.Register("k6/x/csv", New())
}

type Module struct{}

func New() *Module {
return &Module{}
}

var ErrInvalidSeparator = errors.New("invalid separator")

func (m *Module) Parse(ctx context.Context, text string, separator []byte) (interface{}, error) {
if len(separator) > 1 {
return nil, fmt.Errorf("%w: %s", ErrInvalidSeparator, separator)
}

r := csv.NewReader(strings.NewReader(text))

if len(separator) == 1 {
r.Comma = rune(separator[0])
}

rows := []map[string]string{}

var header []string

for {
record, err := r.Read()
if err == io.EOF {
break
}

if err != nil {
return nil, err
}

if header == nil {
header = record
} else {
dict := map[string]string{}
for i := range header {
dict[header[i]] = record[i]
}
rows = append(rows, dict)
}
}

return rows, nil
}
42 changes: 42 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# xk6-csv

xk6-csv enables k6 tests to comfortably parse CSV values.

## Usage

Import an entire module's contents:
```JavaScript
import * as CSV from "k6/x/csv";
```

Import a single export from a module:
```JavaScript
import { parse } from "k6/x/csv";
```

## Table of contents

### Functions

- [parse](README.md#parse)

## Functions

### parse

**parse**(`text`: *string*, `separator?`: *string*): *object*[]

The parse() method parses a CSV string, constructing the JavaScript array objects described by the string.
The first line of CSV file must be the header with field names. The returned array will contains objects corresponding
to CSV lines, one object per line, with field names as property names and field values as property values.

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `text` | *string* | The string to parse as CSV |
| `separator?` | *string* | The field separator character, must be one character string if present. Default is comma (`,`) |

**Returns:** *object*[]

The array of objects corresponding to the given CSV text.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/szkiba/xk6-csv

go 1.16

require github.com/loadimpact/k6 v0.31.1
Loading

0 comments on commit 0dc46e6

Please sign in to comment.