-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.go
55 lines (44 loc) · 992 Bytes
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package helios
import (
"errors"
"fmt"
"reflect"
"time"
)
var InvalidConfigurationError = errors.New("invalid configuration provided")
type Config struct {
configs []configuration
}
type configuration interface {
validate() error
}
func NewConfig(configs ...configuration) (*Config, error) {
for _, config := range configs {
if err := config.validate(); err != nil {
return nil, fmt.Errorf(
"%w for %s: %s",
InvalidConfigurationError,
reflect.TypeOf(config).Elem().Name(),
err.Error(),
)
}
}
return &Config{configs: configs}, nil
}
func (c *Config) GetPollInterval() *PollInterval {
for _, config := range c.configs {
if pollInterval, isPollInterval := config.(*PollInterval); isPollInterval {
return pollInterval
}
}
return nil
}
type PollInterval struct {
time.Duration
}
func (c *PollInterval) validate() error {
if c.Seconds() < 0.1 {
return errors.New("poll time must be greater than or equal to 0.1 seconds")
}
return nil
}