-
Notifications
You must be signed in to change notification settings - Fork 2
/
environment_options.go
86 lines (64 loc) · 1.69 KB
/
environment_options.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package config
import (
"errors"
"fmt"
)
type EnvironmentOption interface {
Key() string
SetValue(interface{}) error
}
type StringEnvironmentOption struct {
key string
value string
}
var _ EnvironmentOption = &StringEnvironmentOption{}
func NewStringEnvironmentOption(key string) StringEnvironmentOption {
return StringEnvironmentOption{key: key}
}
func (eo *StringEnvironmentOption) Key() string {
return eo.key
}
func (eo *StringEnvironmentOption) SetValue(value interface{}) error {
stringValue, ok := value.(string)
if !ok {
return fmt.Errorf("Cannot convert option value to string: %#+v", value)
}
eo.value = stringValue
return nil
}
func (eo *StringEnvironmentOption) GetValue() string {
return eo.value
}
type StringSliceEnvironmentOption struct {
key string
value []string
}
var _ EnvironmentOption = &StringSliceEnvironmentOption{}
func NewStringSliceEnvironmentOption(key string) StringSliceEnvironmentOption {
return StringSliceEnvironmentOption{key: key}
}
func (eo *StringSliceEnvironmentOption) Key() string {
return eo.key
}
func (eo *StringSliceEnvironmentOption) SetValue(value interface{}) error {
if stringSliceValue, ok := value.([]string); ok {
eo.value = stringSliceValue
return nil
}
sliceValue, ok := value.([]interface{})
if !ok {
return fmt.Errorf("Cannot convert option value to slice: %#+v", value)
}
eo.value = []string{}
for _, itemValue := range sliceValue {
stringItemValue, ok := itemValue.(string)
if !ok {
return errors.New("Cannot convert option slice value item to string")
}
eo.value = append(eo.value, stringItemValue)
}
return nil
}
func (eo *StringSliceEnvironmentOption) GetValue() []string {
return eo.value
}