@@ -24,6 +24,19 @@ type EnvValueSource interface {
24
24
Key () string
25
25
}
26
26
27
+ // MapSource is a source which can be used to look up a value
28
+ // based on a key
29
+ // typically for use with a cli.Flag
30
+ type MapSource interface {
31
+ fmt.Stringer
32
+ fmt.GoStringer
33
+
34
+ // Lookup returns the value from the source based on key
35
+ // and if it was found
36
+ // or returns an empty string and false
37
+ Lookup (string ) (any , bool )
38
+ }
39
+
27
40
// ValueSourceChain contains an ordered series of ValueSource that
28
41
// allows for lookup where the first ValueSource to resolve is
29
42
// returned
@@ -159,19 +172,24 @@ func Files(paths ...string) ValueSourceChain {
159
172
return vsc
160
173
}
161
174
162
- type MapSource struct {
175
+ type mapSource struct {
163
176
name string
164
177
m map [any ]any
165
178
}
166
179
167
- func NewMapSource (name string , m map [any ]any ) * MapSource {
168
- return & MapSource {
180
+ func NewMapSource (name string , m map [any ]any ) MapSource {
181
+ return & mapSource {
169
182
name : name ,
170
183
m : m ,
171
184
}
172
185
}
173
186
174
- func (ms * MapSource ) lookup (name string ) (any , bool ) {
187
+ func (ms * mapSource ) String () string { return fmt .Sprintf ("map source %[1]q" , ms .name ) }
188
+ func (ms * mapSource ) GoString () string {
189
+ return fmt .Sprintf ("&mapSource{name:%[1]q}" , ms .name )
190
+ }
191
+
192
+ func (ms * mapSource ) Lookup (name string ) (any , bool ) {
175
193
// nestedVal checks if the name has '.' delimiters.
176
194
// If so, it tries to traverse the tree by the '.' delimited sections to find
177
195
// a nested value for the key.
@@ -205,26 +223,26 @@ func (ms *MapSource) lookup(name string) (any, bool) {
205
223
206
224
type mapValueSource struct {
207
225
key string
208
- ms * MapSource
226
+ ms MapSource
209
227
}
210
228
211
- func NewMapValueSource (key string , ms * MapSource ) ValueSource {
229
+ func NewMapValueSource (key string , ms MapSource ) ValueSource {
212
230
return & mapValueSource {
213
231
key : key ,
214
232
ms : ms ,
215
233
}
216
234
}
217
235
218
236
func (mvs * mapValueSource ) String () string {
219
- return fmt .Sprintf ("map source key %[1]q from %[2]q " , mvs .key , mvs .ms .name )
237
+ return fmt .Sprintf ("key %[1]q from %[2]s " , mvs .key , mvs .ms .String () )
220
238
}
221
239
222
240
func (mvs * mapValueSource ) GoString () string {
223
- return fmt .Sprintf ("&mapValueSource{key:%[1]q, src:%[2]q }" , mvs .key , mvs .ms .m )
241
+ return fmt .Sprintf ("&mapValueSource{key:%[1]q, src:%[2]s }" , mvs .key , mvs .ms .GoString () )
224
242
}
225
243
226
244
func (mvs * mapValueSource ) Lookup () (string , bool ) {
227
- if v , ok := mvs .ms .lookup (mvs .key ); ! ok {
245
+ if v , ok := mvs .ms .Lookup (mvs .key ); ! ok {
228
246
return "" , false
229
247
} else {
230
248
return fmt .Sprintf ("%+v" , v ), true
0 commit comments