This repository has been archived by the owner on Oct 31, 2020. It is now read-only.
/
option_set.go
107 lines (87 loc) · 2.54 KB
/
option_set.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package config
import (
"fmt"
"strings"
)
// An OptionSet is map of Options, keyed by the Options' Names.
type OptionSet map[string]*Option
// Export returns a map that's suitable for pushing into a config.json file.
func (os OptionSet) Export(includeNonExportable bool, includeNonOverrides bool) map[string]interface{} {
tbr := make(map[string]interface{})
for _, v := range os {
if (v.Options.Exportable || includeNonExportable) && (v.overridden || includeNonOverrides) {
parts := strings.Split(v.Name, ".")
var i int
var cursor = &tbr
for i < len(parts)-1 {
if _, exists := (*cursor)[parts[i]]; !exists {
(*cursor)[parts[i]] = make(map[string]interface{})
}
v := (*cursor)[parts[i]].(map[string]interface{})
cursor = &v
i++
}
(*cursor)[parts[i]] = v.Value
}
}
return tbr
}
// Add adds an Option to an OptionSet with a key of the Option's name.
func (os OptionSet) Add(o *Option) {
os[o.Name] = o
}
// Get retrieves an Option with the Name of key, and a boolean to determine if it was found or not.
func (os OptionSet) Get(key string) (*Option, bool) {
result, exists := os[key]
return result, exists
}
// Require retrieves an Option with the Name of key. Panics if there was no key found.
func (os OptionSet) Require(key string) *Option {
result, exists := os.Get(key)
if !exists {
panic("Option with name " + key + " doesn't exist")
}
return result
}
// Validate checks all Options in an OptionSet and returns an error if any of them don't pass any of their Filters and are Required.
func (os OptionSet) Validate() error {
hasError := false
invalidOpts := make(optionFilterValidationSet, 0)
for _, v := range os {
validOption := true
errs := []string{}
for _, f := range v.Options.Filters {
res, err := f(v)
validOption = validOption && res
if err != nil {
errs = append(errs, err.Error())
}
}
if !validOption {
invalidOpts = append(invalidOpts, optionFilterValidation{
name: v.Name,
errors: errs,
})
}
hasError = hasError || !validOption
}
if hasError {
return invalidOpts
}
return nil
}
type optionFilterValidation struct {
name string
errors []string
}
func (e optionFilterValidation) Error() string {
return fmt.Sprintf("%s: %s", e.name, strings.Join(e.errors, "; "))
}
type optionFilterValidationSet []optionFilterValidation
func (e optionFilterValidationSet) Error() string {
str := []string{}
for _, v := range e {
str = append(str, " "+v.Error())
}
return fmt.Sprintf("Some options were empty or invalid:\n%s", strings.Join(str, "\n"))
}