-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.go
183 lines (139 loc) · 3.64 KB
/
loader.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package confiq
import (
"errors"
"fmt"
"maps"
)
const noPrefix = ""
// Collection of loader errors.
var (
ErrCannotOpenConfig = errors.New("cannot open config")
ErrCannotLoadConfig = errors.New("cannot load config")
)
var (
errValueCannotBeNil = errors.New("value cannot be nil")
errCannotApplyValueOfThisType = errors.New("cannot apply value of this type")
errCannotApplyMapValue = errors.New("cannot apply map value")
errCannotApplySliceValue = errors.New("cannot apply slice value")
)
type loader struct {
prefix string
}
func (c *ConfigSet) Load(valueContainer IValueContainer, options ...loadOption) error {
if errs := valueContainer.Errors(); len(errs) > 0 {
return fmt.Errorf("%w: %w", ErrCannotLoadConfig, errors.Join(errs...))
}
return c.applyValues(valueContainer.Get(), options...)
}
// LoadRawValue loads a raw value into the config set.
// The value must be a map[string]any or a slice of any.
func (c *ConfigSet) LoadRawValue(newValues []any, options ...loadOption) error {
if newValues == nil {
return errValueCannotBeNil
}
return c.applyValues(newValues, options...)
}
func newLoader() *loader {
return &loader{
prefix: noPrefix,
}
}
func (c *ConfigSet) applyValues(newValues []any, options ...loadOption) error {
loader := newLoader()
for _, option := range options {
option(loader)
}
for _, newValue := range newValues {
switch v := newValue.(type) {
case map[string]any:
if err := c.applyMap(v, loader.prefix); err != nil {
return err
}
case []any:
if err := c.applySlice(v, loader.prefix); err != nil {
return err
}
default:
return fmt.Errorf("%w: %T", errCannotApplyValueOfThisType, newValue)
}
}
return nil
}
func (c *ConfigSet) applyMap(newValue map[string]any, prefix string) error {
if prefix == "" {
return c.applyMapWithoutPrefix(newValue)
}
return c.applyMapWithPrefix(newValue, prefix)
}
func (c *ConfigSet) applySlice(newValue []any, prefix string) error {
if prefix == "" {
return c.applySliceWithoutPrefix(newValue)
}
return c.applySliceWithPrefix(newValue, prefix)
}
func (c *ConfigSet) applyMapWithoutPrefix(newValue map[string]any) error {
if *c.value == nil {
*c.value = newValue
return nil
}
valueMap, ok := (*c.value).(map[string]any)
if !ok {
return errCannotApplyMapValue
}
maps.Copy(valueMap, newValue)
return nil
}
func (c *ConfigSet) applyMapWithPrefix(newValue map[string]any, prefix string) error {
if *c.value == nil {
*c.value = map[string]any{}
}
valueMap, ok := (*c.value).(map[string]any)
if !ok {
return errCannotApplyMapValue
}
valueMapAtPath, ok := valueMap[prefix]
if !ok {
valueMap[prefix] = newValue
return nil
}
valueMapAtPathMap, ok := valueMapAtPath.(map[string]any)
if !ok {
return errCannotApplyMapValue
}
maps.Copy(valueMapAtPathMap, newValue)
return nil
}
func (c *ConfigSet) applySliceWithoutPrefix(newValue []any) error {
if *c.value == nil {
*c.value = newValue
return nil
}
valueSlice, ok := (*c.value).([]any)
if !ok {
return errCannotApplySliceValue
}
valueSlice = append(valueSlice, newValue...)
*c.value = valueSlice
return nil
}
func (c *ConfigSet) applySliceWithPrefix(newValue []any, prefix string) error {
if *c.value == nil {
*c.value = map[string]any{}
}
valueMap, ok := (*c.value).(map[string]any)
if !ok {
return errCannotApplySliceValue
}
valueMapAtPath, ok := valueMap[prefix]
if !ok {
valueMap[prefix] = newValue
return nil
}
valueMapAtPathSlice, ok := valueMapAtPath.([]any)
if !ok {
return errCannotApplySliceValue
}
valueMapAtPathSlice = append(valueMapAtPathSlice, newValue...)
valueMap[prefix] = valueMapAtPathSlice
return nil
}