Skip to content

Commit 6bcc0ff

Browse files
committed
Drop mapstructure and report even more when options can't be parsed
1 parent d14e814 commit 6bcc0ff

File tree

4 files changed

+37
-34
lines changed

4 files changed

+37
-34
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ require (
77
github.com/influxdata/influxdb1-client v0.0.0-20190402204710-8ff2fc3824fc
88
github.com/kelseyhightower/envconfig v1.4.0
99
github.com/kubernetes/helm v2.9.0+incompatible
10-
github.com/mitchellh/mapstructure v1.1.2
1110
github.com/sirupsen/logrus v1.8.1
1211
github.com/stretchr/testify v1.7.0
1312
github.com/xdg/scram v1.0.3

pkg/kafka/config.go

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/Shopify/sarama"
3030
"github.com/kelseyhightower/envconfig"
3131
"github.com/kubernetes/helm/pkg/strvals"
32-
"github.com/mitchellh/mapstructure"
3332
"gopkg.in/guregu/null.v3"
3433

3534
"go.k6.io/k6/lib/types"
@@ -55,12 +54,6 @@ type Config struct {
5554
InfluxDBConfig influxdbConfig `json:"influxdb"`
5655
}
5756

58-
// config is a duplicate of ConfigFields as we can not mapstructure.Decode into
59-
// null types so we duplicate the struct with primitive types to Decode into
60-
type config struct {
61-
Brokers []string `mapstructure:"brokers"`
62-
}
63-
6457
// NewConfig creates a new Config instance with default values for some fields.
6558
func NewConfig() Config {
6659
return Config{
@@ -186,29 +179,38 @@ func ParseArg(arg string) (Config, error) {
186179
delete(params, "format")
187180
}
188181

189-
var cfg config
190182
if v, ok := params["brokers"].(string); ok {
191-
params["brokers"] = []string{v}
183+
c.Brokers = []string{v}
184+
185+
delete(params, "brokers")
192186
}
193-
err = mapstructure.Decode(params, &cfg)
194-
if err != nil {
195-
return c, err
187+
if v, ok := params["brokers"].([]interface{}); ok {
188+
c.Brokers = interfaceSliceToStringSlice(v)
189+
delete(params, "brokers")
196190
}
197-
delete(params, "brokers")
198-
199-
c.Brokers = cfg.Brokers
200191

201192
if len(params) > 0 {
202-
var s string
203-
for k, v := range params {
204-
s += fmt.Sprintf("%s=%v,", k, v)
205-
}
206-
s = s[:len(s)-1]
207-
return c, errors.New("Unknown or unparsed options '" + s + "'")
193+
return c, errors.New("Unknown or unparsed options '" + mapToString(params) + "'")
208194
}
209195
return c, nil
210196
}
211197

198+
func mapToString(m map[string]interface{}) string {
199+
var s string
200+
for k, v := range m {
201+
s += fmt.Sprintf("%s=%v,", k, v)
202+
}
203+
return s[:len(s)-1]
204+
}
205+
206+
func interfaceSliceToStringSlice(input []interface{}) []string {
207+
output := make([]string, len(input))
208+
for i, v := range input {
209+
output[i] = fmt.Sprintf("%v", v)
210+
}
211+
return output
212+
}
213+
212214
// GetConsolidatedConfig combines {default config values + JSON config +
213215
// environment vars + arg config values}, and returns the final result.
214216
func GetConsolidatedConfig(jsonRawConf json.RawMessage, env map[string]string, arg string) (Config, error) {

pkg/kafka/config_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ func TestConfigParseArg(t *testing.T) {
137137
c, err = ParseArg("ssl=nottrue")
138138
assert.Error(t, err)
139139
assert.Contains(t, err.Error(), `Unknown or unparsed options 'ssl=nottrue'`)
140+
141+
c, err = ParseArg("brokers={broker2,broker3:9092},topic=someTopic,format=influxdb,influxdb.tagsAsFields=fake,influxdb.something=else")
142+
assert.Error(t, err)
143+
assert.Contains(t, err.Error(), `Unknown or unparsed options 'something=else'`)
140144
}
141145

142146
func TestConsolidatedConfig(t *testing.T) {

pkg/kafka/format_influxdb.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@
2121
package kafka
2222

2323
import (
24+
"errors"
2425
"fmt"
2526
"strconv"
2627
"strings"
2728

2829
client "github.com/influxdata/influxdb1-client/v2"
29-
"github.com/mitchellh/mapstructure"
3030
"github.com/sirupsen/logrus"
31-
"go.k6.io/k6/lib/types"
3231
"go.k6.io/k6/stats"
3332
)
3433

@@ -172,18 +171,17 @@ func (c influxdbConfig) Apply(cfg influxdbConfig) influxdbConfig {
172171
func influxdbParseMap(m map[string]interface{}) (influxdbConfig, error) {
173172
c := influxdbConfig{}
174173
if v, ok := m["tagsAsFields"].(string); ok {
175-
m["tagsAsFields"] = []string{v}
174+
c.TagsAsFields = []string{v}
175+
delete(m, "tagsAsFields")
176176
}
177-
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
178-
DecodeHook: types.NullDecoder,
179-
Result: &c,
180-
})
181-
if err != nil {
182-
return c, err
177+
if v, ok := m["tagsAsFields"].([]interface{}); ok {
178+
c.TagsAsFields = interfaceSliceToStringSlice(v)
179+
delete(m, "tagsAsFields")
183180
}
184-
185-
err = dec.Decode(m)
186-
return c, err
181+
if len(m) > 0 {
182+
return c, errors.New("Unknown or unparsed options '" + mapToString(m) + "'")
183+
}
184+
return c, nil
187185
}
188186

189187
type influxdbConfig struct {

0 commit comments

Comments
 (0)