-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.go
More file actions
212 lines (178 loc) · 5.74 KB
/
builder.go
File metadata and controls
212 lines (178 loc) · 5.74 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package prefer
import (
"os"
"strings"
)
// Source represents a configuration source for the ConfigBuilder.
type Source interface {
// Load returns configuration data as a map.
Load() (map[string]interface{}, error)
}
// DeepMerge merges override into base, returning a new map.
// Nested maps are merged recursively; other values are overwritten.
func DeepMerge(base, override map[string]interface{}) map[string]interface{} {
result := make(map[string]interface{})
// Copy base
for k, v := range base {
result[k] = v
}
// Merge override
for k, v := range override {
if baseVal, exists := result[k]; exists {
baseMap, baseIsMap := baseVal.(map[string]interface{})
overrideMap, overrideIsMap := v.(map[string]interface{})
if baseIsMap && overrideIsMap {
result[k] = DeepMerge(baseMap, overrideMap)
continue
}
}
result[k] = v
}
return result
}
// ConfigBuilder builds configuration from multiple layered sources.
// Sources are applied in order, with later sources overriding earlier ones.
type ConfigBuilder struct {
sources []Source
}
// NewConfigBuilder creates a new ConfigBuilder.
func NewConfigBuilder() *ConfigBuilder {
return &ConfigBuilder{
sources: make([]Source, 0),
}
}
// AddSource adds a custom source to the builder.
func (b *ConfigBuilder) AddSource(source Source) *ConfigBuilder {
b.sources = append(b.sources, source)
return b
}
// AddDefaults adds in-memory default values.
func (b *ConfigBuilder) AddDefaults(defaults map[string]interface{}) *ConfigBuilder {
return b.AddSource(&MemorySource{data: defaults})
}
// AddFile adds a required configuration file.
func (b *ConfigBuilder) AddFile(identifier string) *ConfigBuilder {
return b.AddSource(&FileSource{identifier: identifier, required: true})
}
// AddOptionalFile adds an optional configuration file.
// If the file doesn't exist, it's silently skipped.
func (b *ConfigBuilder) AddOptionalFile(identifier string) *ConfigBuilder {
return b.AddSource(&FileSource{identifier: identifier, required: false})
}
// AddEnv adds environment variables with the given prefix.
// Variables are converted to nested structure using the separator.
// Example: MYAPP__DATABASE__HOST with prefix "MYAPP" becomes database.host
func (b *ConfigBuilder) AddEnv(prefix string) *ConfigBuilder {
return b.AddSource(&EnvSource{prefix: prefix, separator: "__"})
}
// AddEnvWithSeparator adds environment variables with a custom separator.
func (b *ConfigBuilder) AddEnvWithSeparator(prefix, separator string) *ConfigBuilder {
return b.AddSource(&EnvSource{prefix: prefix, separator: separator})
}
// Build loads and merges all sources, returning a ConfigMap.
func (b *ConfigBuilder) Build() (*ConfigMap, error) {
merged := make(map[string]interface{})
for _, source := range b.sources {
data, err := source.Load()
if err != nil {
return nil, err
}
merged = DeepMerge(merged, data)
}
return &ConfigMap{data: merged}, nil
}
// MemorySource provides configuration from an in-memory map.
type MemorySource struct {
data map[string]interface{}
}
// NewMemorySource creates a new MemorySource.
func NewMemorySource(data map[string]interface{}) *MemorySource {
return &MemorySource{data: data}
}
func (s *MemorySource) Load() (map[string]interface{}, error) {
// Return a copy to prevent mutation
result := make(map[string]interface{})
for k, v := range s.data {
result[k] = v
}
return result, nil
}
// FileSource loads configuration from a file.
type FileSource struct {
identifier string
required bool
}
// NewFileSource creates a required file source.
func NewFileSource(identifier string) *FileSource {
return &FileSource{identifier: identifier, required: true}
}
// NewOptionalFileSource creates an optional file source.
func NewOptionalFileSource(identifier string) *FileSource {
return &FileSource{identifier: identifier, required: false}
}
func (s *FileSource) Load() (map[string]interface{}, error) {
var result map[string]interface{}
_, err := Load(s.identifier, &result)
if err != nil {
if !s.required {
// Return empty map for optional files that don't exist
return make(map[string]interface{}), nil
}
return nil, err
}
return result, nil
}
// EnvSource loads configuration from environment variables.
type EnvSource struct {
prefix string
separator string
}
// NewEnvSource creates a new EnvSource with the default separator "__".
func NewEnvSource(prefix string) *EnvSource {
return &EnvSource{prefix: prefix, separator: "__"}
}
// NewEnvSourceWithSeparator creates an EnvSource with a custom separator.
func NewEnvSourceWithSeparator(prefix, separator string) *EnvSource {
return &EnvSource{prefix: prefix, separator: separator}
}
func (s *EnvSource) Load() (map[string]interface{}, error) {
result := make(map[string]interface{})
prefix := s.prefix + s.separator
for _, env := range os.Environ() {
parts := strings.SplitN(env, "=", 2)
if len(parts) != 2 {
continue
}
key, value := parts[0], parts[1]
if !strings.HasPrefix(key, prefix) {
continue
}
// Remove prefix and convert to nested structure
key = strings.TrimPrefix(key, prefix)
key = strings.ToLower(key)
keyParts := strings.Split(key, s.separator)
setNested(result, keyParts, value)
}
return result, nil
}
// setNested sets a value in a nested map structure.
func setNested(data map[string]interface{}, parts []string, value interface{}) {
current := data
for i, part := range parts {
if i == len(parts)-1 {
current[part] = value
} else {
if _, exists := current[part]; !exists {
current[part] = make(map[string]interface{})
}
if nested, ok := current[part].(map[string]interface{}); ok {
current = nested
} else {
// Overwrite non-map value with a new map
newMap := make(map[string]interface{})
current[part] = newMap
current = newMap
}
}
}
}