Skip to content

Commit

Permalink
Drop mapstructure usage in influxdb login
Browse files Browse the repository at this point in the history
As well as the whole lib.NullDecoder as it's only used for this
  • Loading branch information
mstoykov committed Nov 3, 2021
1 parent a46587f commit b67c8b1
Show file tree
Hide file tree
Showing 14 changed files with 5 additions and 1,651 deletions.
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
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
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.

46 changes: 0 additions & 46 deletions vendor/github.com/mitchellh/mapstructure/README.md

This file was deleted.

Loading

0 comments on commit b67c8b1

Please sign in to comment.