-
Notifications
You must be signed in to change notification settings - Fork 0
/
expand.go
243 lines (201 loc) · 6.08 KB
/
expand.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// SPDX-FileCopyrightText: 2022 Weston Schmidt <weston_schmidt@alumni.purdue.edu>
// SPDX-License-Identifier: Apache-2.0
package goschtalt
import (
"fmt"
"os"
"github.com/goschtalt/goschtalt/internal/print"
"github.com/goschtalt/goschtalt/pkg/meta"
)
// Expander provides a method that can expand variables in values.
type Expander interface {
// Expand maps the incoming string to a new string. The string passed in
// will not contain the start and end delimiters. If the string is not
// found, return the bool value of false, otherwise return true.
Expand(string) (string, bool)
}
// The ExpanderFunc type is an adapter to allow the use of ordinary functions
// as Expanders. If f is a function with the appropriate signature,
// ExpanderFunc(f) is a Expander that calls f.
type ExpanderFunc func(string) (string, bool)
// Get calls f(s)
func (f ExpanderFunc) Expand(s string) (string, bool) {
return f(s)
}
var _ Expander = (*ExpanderFunc)(nil)
type envExpander struct{}
func (envExpander) Expand(s string) (string, bool) {
return os.LookupEnv(s)
}
// ExpandEnv is a simple way to add automatic environment variable expansion
// after the configuration has been compiled.
//
// Expand() and ExpandEnv() directives are evaluated in the order specified.
//
// Valid Option Types:
// - [ExpandOption]
// - [GlobalOption]
func ExpandEnv(opts ...ExpandOption) Option {
exp := expand{
origin: "environment",
expander: envExpander{},
start: "${",
end: "}",
}
for _, opt := range opts {
if err := opt.expandApply(&exp); err != nil {
return WithError(fmt.Errorf("ExpandEnv() err: %w", err))
}
}
exp.text = print.P("ExpandEnv",
print.Literal("..."),
print.Yields(
print.String(exp.start, "start"),
print.String(exp.end, "end"),
print.String(exp.origin, "origin"),
print.Int(exp.maximum, "maximum"),
),
)
return &exp
}
// Expand provides a way to expand variables in values throughout the
// configuration tree. Expand() can be called multiple times to expand
// variables based on additional configurations and mappers.
//
// The initial discovery of a variable to expand in the configuration tree
// value is determined by the Start and End delimiters options provided. The
// default delimiters are "${" and "}" respectively. Further expansions of
// values replaces ${var} or $var in the string based on the mapping function
// provided.
//
// Expand() and ExpandEnv() directives are evaluated in the order specified.
//
// Valid Option Types:
// - [ExpandOption]
// - [GlobalOption]
func Expand(expander Expander, opts ...ExpandOption) Option {
exp := expand{
expander: expander,
start: "${",
end: "}",
}
for _, opt := range opts {
if err := opt.expandApply(&exp); err != nil {
return WithError(fmt.Errorf("Expand() err: %w", err))
}
}
exp.text = print.P("Expand",
print.Obj(expander),
print.Literal("..."),
print.Yields(
print.String(exp.start, "start"),
print.String(exp.end, "end"),
print.String(exp.origin, "origin"),
print.Int(exp.maximum, "maximum"),
),
)
return &exp
}
// expand controls how variables are identified and processed.
type expand struct {
// The text of the option that provided this expand command.
text string
// Optional name showing where the value came from.
origin string
// The string that prefixes a variable. "${{" or "${" are common examples.
// Defaults to "${" if equal to "".
start string
// The string that trails a variable. "}}" or "}" are common examples.
// Defaults to "}" if equal to "".
end string
// The string to string mapping function.
// Mapping request ignored if nil.
expander Expander
// The maximum expansions of a value before a recursion error is returned.
// Defaults to 10000 if set to less than 1.
maximum int
}
func (exp expand) apply(opts *options) error {
if exp.maximum < 1 {
exp.maximum = 10000
}
if exp.expander != nil {
opts.expansions = append(opts.expansions, exp)
}
return nil
}
func (_ expand) ignoreDefaults() bool {
return false
}
func (exp expand) String() string {
return exp.text
}
// expandTree is a helper function that expands variables in the configuration
// tree. The maximum number of expansions is limited to the max value.
func expandTree(in meta.Object, max int, expansions []expand) (meta.Object, bool, error) {
changed := true
for i := 0; changed && i < max; i++ {
changed = false
for _, exp := range expansions {
var err error
in, err = in.ToExpanded(
exp.maximum,
exp.origin,
exp.start,
exp.end,
func(s string) (string, bool) {
got, found := exp.expander.Expand(s)
if found {
changed = true
}
return got, found
},
)
if err != nil {
return meta.Object{}, false, err
}
}
}
return in, changed, nil
}
// ---- ExpandOption follow --------------------------------------------------
// ExpandOption provides the means to configure options around variable
// expansion.
type ExpandOption interface {
expandApply(*expand) error
}
// WithOrigin provides the origin name to add showing where a value in the
// configuration tree originates from.
func WithOrigin(origin string) ExpandOption {
return withOriginOption(origin)
}
type withOriginOption string
func (w withOriginOption) expandApply(exp *expand) error {
exp.origin = string(w)
return nil
}
// WithDelimiters provides a way to define different delimiters for the start
// and end of a variable for matching purposes.
func WithDelimiters(start, end string) ExpandOption {
return &withDelimitersOption{start: start, end: end}
}
type withDelimitersOption struct {
start string
end string
}
func (w withDelimitersOption) expandApply(exp *expand) error {
exp.start = w.start
exp.end = w.end
return nil
}
// WithMaximum provides a way to overwrite the maximum number of times variables
// are expanded. Any value less than 1 will default to 10000 as a precaution
// against getting trapped in an infinite loop.
func WithMaximum(maximum int) ExpandOption {
return withMaximumOption(maximum)
}
type withMaximumOption int
func (w withMaximumOption) expandApply(exp *expand) error {
exp.maximum = int(w)
return nil
}