Skip to content

Commit

Permalink
do not need []null.String for []string
Browse files Browse the repository at this point in the history
  • Loading branch information
jmccann committed May 15, 2018
1 parent 0d80728 commit 3afdd13
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 22 deletions.
2 changes: 1 addition & 1 deletion stats/kafka/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Collector struct {

// New creates an instance of the collector
func New(conf Config) (*Collector, error) {
producer, err := sarama.NewSyncProducer(StringsFromNulls(conf.Brokers), nil)
producer, err := sarama.NewSyncProducer(conf.Brokers, nil)
if err != nil {
return nil, err
}
Expand Down
10 changes: 3 additions & 7 deletions stats/kafka/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

type ConfigFields struct {
// Connection.
Brokers []null.String `json:"brokers" envconfig:"KAFKA_BROKERS"`
Brokers []string `json:"brokers" envconfig:"KAFKA_BROKERS"`

// Samples.
Topic null.String `json:"topic" envconfig:"KAFKA_TOPIC"`
Expand All @@ -54,11 +54,7 @@ func NewConfig() Config {

func (c Config) Apply(cfg Config) Config {
if len(cfg.Brokers) > 0 {
for _, b := range cfg.Brokers {
if b.Valid {
c.Brokers = append(c.Brokers, b)
}
}
c.Brokers = cfg.Brokers
}
if cfg.Format.Valid {
c.Format = cfg.Format
Expand Down Expand Up @@ -99,7 +95,7 @@ func ParseArg(arg string) (Config, error) {
brokers = append(brokers, null.StringFrom(b))
}

c.Brokers = brokers
c.Brokers = cfg.Brokers
c.Topic = null.StringFrom(cfg.Topic)
c.Format = null.StringFrom(cfg.Format)

Expand Down
4 changes: 2 additions & 2 deletions stats/kafka/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ import (
func TestConfigParseArg(t *testing.T) {
c, err := ParseArg("brokers=broker1,topic=someTopic,format=influx")
assert.Nil(t, err)
assert.Equal(t, []null.String{null.StringFrom("broker1")}, c.Brokers)
assert.Equal(t, []string{"broker1"}, c.Brokers)
assert.Equal(t, null.StringFrom("someTopic"), c.Topic)
assert.Equal(t, null.StringFrom("influx"), c.Format)

c, err = ParseArg("brokers={broker2,broker3:9092},topic=someTopic2,format=json")
assert.Nil(t, err)
assert.Equal(t, []null.String{null.StringFrom("broker2"), null.StringFrom("broker3:9092")}, c.Brokers)
assert.Equal(t, []string{"broker2","broker3:9092"}, c.Brokers)
assert.Equal(t, null.StringFrom("someTopic2"), c.Topic)
assert.Equal(t, null.StringFrom("json"), c.Format)
}
12 changes: 0 additions & 12 deletions stats/kafka/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/loadimpact/k6/stats"
"github.com/loadimpact/k6/stats/influxdb"
jsonCollector "github.com/loadimpact/k6/stats/json"
"gopkg.in/guregu/null.v3"
)

func formatSamples(format string, samples stats.Samples) ([]string, error) {
Expand Down Expand Up @@ -37,14 +36,3 @@ func formatSamples(format string, samples stats.Samples) ([]string, error) {

return metrics, nil
}

// StringsFromNulls returns []string from []null.String
func StringsFromNulls(nullStrings []null.String) []string {
var strs []string
for _, s := range nullStrings {
if s.Valid {
strs = append(strs, s.String)
}
}
return strs
}

0 comments on commit 3afdd13

Please sign in to comment.