Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop map structure #2223

Merged
merged 3 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions cmd/login_influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ import (
"syscall"
"time"

"github.com/mitchellh/mapstructure"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"golang.org/x/term"
"gopkg.in/guregu/null.v3"

"go.k6.io/k6/lib/types"
"go.k6.io/k6/output/influxdb"
"go.k6.io/k6/ui"
)
Expand Down Expand Up @@ -101,17 +100,11 @@ This will set the default server used when just "-o influxdb" is passed.`,
return err
}

dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
DecodeHook: types.NullDecoder,
Result: &conf,
})
if err != nil {
return err
}
conf.Addr = null.StringFrom(vals["Addr"].(string))
conf.DB = null.StringFrom(vals["DB"].(string))
conf.Username = null.StringFrom(vals["Username"].(string))
conf.Password = null.StringFrom(vals["Password"].(string))

if err = dec.Decode(vals); err != nil {
return err
}
client, err := influxdb.MakeClient(conf)
if err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ require (
github.com/mattn/go-colorable v0.1.8
github.com/mattn/go-isatty v0.0.13
github.com/mccutchen/go-httpbin v1.1.2-0.20190116014521-c5cb2f4802fa
github.com/mitchellh/mapstructure v1.1.2
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d
github.com/onsi/ginkgo v1.14.0 // indirect
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eI
github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg=
github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY=
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
Expand Down
16 changes: 4 additions & 12 deletions js/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (

"github.com/dop251/goja"
"github.com/dop251/goja/parser"
"github.com/mitchellh/mapstructure"
"github.com/sirupsen/logrus"

"go.k6.io/k6/lib"
Expand Down Expand Up @@ -109,7 +108,7 @@ func (c *Compiler) initializeBabel() error {
}

// Transform the given code into ES5
func (c *Compiler) Transform(src, filename string) (code string, srcmap *SourceMap, err error) {
func (c *Compiler) Transform(src, filename string) (code string, srcmap []byte, err error) {
if c.babel == nil {
onceBabel.Do(func() {
globalBabel, err = newBabel()
Expand Down Expand Up @@ -197,7 +196,8 @@ func newBabel() (*babel, error) {

// Transform the given code into ES5, while synchronizing to ensure only a single
// bundle instance / Goja VM is in use at a time.
func (b *babel) Transform(logger logrus.FieldLogger, src, filename string) (string, *SourceMap, error) {
// TODO the []byte is there to be used as the returned sourcemap and will be done in PR #2082
func (b *babel) Transform(logger logrus.FieldLogger, src, filename string) (string, []byte, error) {
b.m.Lock()
defer b.m.Unlock()
opts := make(map[string]interface{})
Expand All @@ -218,15 +218,7 @@ func (b *babel) Transform(logger logrus.FieldLogger, src, filename string) (stri
if err = b.vm.ExportTo(vO.Get("code"), &code); err != nil {
return code, nil, err
}
var rawMap map[string]interface{}
if err = b.vm.ExportTo(vO.Get("map"), &rawMap); err != nil {
return code, nil, err
}
var srcMap SourceMap
if err = mapstructure.Decode(rawMap, &srcMap); err != nil {
return code, &srcMap, err
}
return code, &srcMap, err
return code, nil, err
na-- marked this conversation as resolved.
Show resolved Hide resolved
}

// Pool is a pool of compilers so it can be used easier in parallel tests as they have their own babel.
Expand Down
30 changes: 0 additions & 30 deletions js/compiler/sourcemap.go

This file was deleted.

56 changes: 0 additions & 56 deletions lib/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,67 +25,11 @@ import (
"encoding/json"
"fmt"
"math"
"reflect"
"strconv"
"strings"
"time"

"gopkg.in/guregu/null.v3"
)

// NullDecoder converts data with expected type f to a guregu/null value
// of equivalent type t. It returns an error if a type mismatch occurs.
func NullDecoder(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
typeFrom := f.String()
typeTo := t.String()

expectedType := ""
switch typeTo {
case "null.String":
if typeFrom == reflect.String.String() {
return null.StringFrom(data.(string)), nil
}
expectedType = reflect.String.String()
case "null.Bool":
if typeFrom == reflect.Bool.String() {
return null.BoolFrom(data.(bool)), nil
}
expectedType = reflect.Bool.String()
case "null.Int":
if typeFrom == reflect.Int.String() {
return null.IntFrom(int64(data.(int))), nil
}
if typeFrom == reflect.Int32.String() {
return null.IntFrom(int64(data.(int32))), nil
}
if typeFrom == reflect.Int64.String() {
return null.IntFrom(data.(int64)), nil
}
expectedType = reflect.Int.String()
case "null.Float":
if typeFrom == reflect.Float32.String() {
return null.FloatFrom(float64(data.(float32))), nil
}
if typeFrom == reflect.Float64.String() {
return null.FloatFrom(data.(float64)), nil
}
expectedType = reflect.Float32.String() + " or " + reflect.Float64.String()
case "types.NullDuration":
if typeFrom == reflect.String.String() {
var d NullDuration
err := d.UnmarshalText([]byte(data.(string)))
return d, err
}
expectedType = reflect.String.String()
}

if expectedType != "" {
return data, fmt.Errorf("expected '%s', got '%s'", expectedType, typeFrom)
}

return data, nil
}

// TODO: something better that won't require so much boilerplate and casts for NullDuration values...

// Duration is an alias for time.Duration that de/serialises to JSON as human-readable strings.
Expand Down
70 changes: 0 additions & 70 deletions lib/types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,79 +27,9 @@ import (
"testing"
"time"

"github.com/mitchellh/mapstructure"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/guregu/null.v3"
)

func TestNullDecoder(t *testing.T) {
type foo struct {
Strs []string
Str null.String
Boolean null.Bool
Integer null.Int
Integer32 null.Int
Integer64 null.Int
Float32 null.Float
Float64 null.Float
Dur NullDuration
}
f := foo{}
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
DecodeHook: NullDecoder,
Result: &f,
})
require.NoError(t, err)

conf := map[string]interface{}{
"strs": []string{"fake"},
"str": "bar",
"boolean": true,
"integer": 42,
"integer32": int32(42),
"integer64": int64(42),
"float32": float32(3.14),
"float64": float64(3.14),
"dur": "1m",
}

err = dec.Decode(conf)
require.NoError(t, err)

require.Equal(t, foo{
Strs: []string{"fake"},
Str: null.StringFrom("bar"),
Boolean: null.BoolFrom(true),
Integer: null.IntFrom(42),
Integer32: null.IntFrom(42),
Integer64: null.IntFrom(42),
Float32: null.FloatFrom(3.140000104904175),
Float64: null.FloatFrom(3.14),
Dur: NewNullDuration(1*time.Minute, true),
}, f)

input := map[string][]interface{}{
"Str": {true, "string", "bool"},
"Boolean": {"invalid", "bool", "string"},
"Integer": {"invalid", "int", "string"},
"Integer32": {true, "int", "bool"},
"Integer64": {"invalid", "int", "string"},
"Float32": {true, "float32 or float64", "bool"},
"Float64": {"invalid", "float32 or float64", "string"},
"Dur": {10, "string", "int"},
}

for k, v := range input {
t.Run("Error Message/"+k, func(t *testing.T) {
err = dec.Decode(map[string]interface{}{
k: v[0],
})
assert.EqualError(t, err, fmt.Sprintf("1 error(s) decoding:\n\n* error decoding '%s': expected '%s', got '%s'", k, v[1], v[2]))
})
}
}

func TestParseExtendedDuration(t *testing.T) {
testCases := []struct {
durStr string
Expand Down
3 changes: 0 additions & 3 deletions modtools_frozen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@
- path: github.com/manyminds/api2go
minVersion: v0.0.0-20180125085803-95be7bd0455e
validUntil: 2029-08-04T16:33:07+03:00
- path: github.com/mitchellh/mapstructure
minVersion: v1.1.2
validUntil: 2029-08-04T16:51:38+03:00
- path: golang.org/x/sys
minVersion: v0.0.0-20201204225414-ed752295db88
validUntil: 2029-08-04T17:00:51+03:00
Expand Down
19 changes: 0 additions & 19 deletions output/influxdb/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"time"

"github.com/kelseyhightower/envconfig"
"github.com/mitchellh/mapstructure"
"gopkg.in/guregu/null.v3"

"go.k6.io/k6/lib/types"
Expand Down Expand Up @@ -115,24 +114,6 @@ func (c Config) Apply(cfg Config) Config {
return c
}

// ParseMap parses a map[string]interface{} into a Config
func ParseMap(m map[string]interface{}) (Config, error) {
c := Config{}
if v, ok := m["tagsAsFields"].(string); ok {
m["tagsAsFields"] = []string{v}
}
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
DecodeHook: types.NullDecoder,
Result: &c,
})
if err != nil {
return c, err
}

err = dec.Decode(m)
return c, err
}

// ParseJSON parses the supplied JSON into a Config.
func ParseJSON(data json.RawMessage) (Config, error) {
conf := Config{}
Expand Down
21 changes: 0 additions & 21 deletions vendor/github.com/mitchellh/mapstructure/CHANGELOG.md

This file was deleted.

21 changes: 0 additions & 21 deletions vendor/github.com/mitchellh/mapstructure/LICENSE

This file was deleted.

Loading