Pull WithDefaults from collectd and openTSDB

This pattern, which is present in Graphite, is not needed for collectd
and openTSDB.
pull/2974/head
Philip O'Toole 2015-06-12 14:17:18 -07:00
parent c622d6a340
commit 7f96d0ed6a
4 changed files with 5 additions and 52 deletions

View File

@ -42,28 +42,3 @@ func NewConfig() Config {
TypesDB: DefaultTypesDB, TypesDB: DefaultTypesDB,
} }
} }
// WithDefaults takes the given config and returns a new config with any required
// default values set.
func (c *Config) WithDefaults() *Config {
d := *c
if d.BindAddress == "" {
d.BindAddress = DefaultBindAddress
}
if d.Database == "" {
d.Database = DefaultDatabase
}
if d.RetentionPolicy == "" {
d.RetentionPolicy = DefaultRetentionPolicy
}
if d.BatchSize == 0 {
d.BatchSize = DefaultBatchSize
}
if d.BatchDuration == 0 {
d.BatchDuration = DefaultBatchDuration
}
if d.TypesDB == "" {
d.TypesDB = DefaultTypesDB
}
return &d
}

View File

@ -47,7 +47,7 @@ type Service struct {
// NewService returns a new instance of the collectd service. // NewService returns a new instance of the collectd service.
func NewService(c Config) *Service { func NewService(c Config) *Service {
s := &Service{ s := &Service{
Config: c.WithDefaults(), Config: &c,
Logger: log.New(os.Stderr, "[collectd] ", log.LstdFlags), Logger: log.New(os.Stderr, "[collectd] ", log.LstdFlags),
err: make(chan error), err: make(chan error),
} }

View File

@ -30,22 +30,3 @@ func NewConfig() Config {
ConsistencyLevel: DefaultConsistencyLevel, ConsistencyLevel: DefaultConsistencyLevel,
} }
} }
// WithDefaults takes the given config and returns a new config with any required
// default values set.
func (c *Config) WithDefaults() *Config {
d := *c
if d.BindAddress == "" {
d.BindAddress = DefaultBindAddress
}
if d.Database == "" {
d.Database = DefaultDatabase
}
if d.RetentionPolicy == "" {
d.RetentionPolicy = DefaultRetentionPolicy
}
if d.ConsistencyLevel == "" {
d.ConsistencyLevel = DefaultConsistencyLevel
}
return &d
}

View File

@ -47,19 +47,16 @@ type Service struct {
// NewService returns a new instance of Service. // NewService returns a new instance of Service.
func NewService(c Config) (*Service, error) { func NewService(c Config) (*Service, error) {
// Use defaults where necessary. consistencyLevel, err := cluster.ParseConsistencyLevel(c.ConsistencyLevel)
d := c.WithDefaults()
consistencyLevel, err := cluster.ParseConsistencyLevel(d.ConsistencyLevel)
if err != nil { if err != nil {
return nil, err return nil, err
} }
s := &Service{ s := &Service{
err: make(chan error), err: make(chan error),
BindAddress: d.BindAddress, BindAddress: c.BindAddress,
Database: d.Database, Database: c.Database,
RetentionPolicy: d.RetentionPolicy, RetentionPolicy: c.RetentionPolicy,
ConsistencyLevel: consistencyLevel, ConsistencyLevel: consistencyLevel,
Logger: log.New(os.Stderr, "[opentsdb] ", log.LstdFlags), Logger: log.New(os.Stderr, "[opentsdb] ", log.LstdFlags),
} }