forked from szkiba/xk6-csv
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
1,676 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/k6 | ||
.task | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"go.lintTool": "golangci-lint", | ||
"go.lintFlags": ["--fast"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.