@@ -24,6 +24,19 @@ type EnvValueSource interface {
2424 Key () string
2525}
2626
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+
2740// ValueSourceChain contains an ordered series of ValueSource that
2841// allows for lookup where the first ValueSource to resolve is
2942// returned
@@ -159,19 +172,24 @@ func Files(paths ...string) ValueSourceChain {
159172 return vsc
160173}
161174
162- type MapSource struct {
175+ type mapSource struct {
163176 name string
164177 m map [any ]any
165178}
166179
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 {
169182 name : name ,
170183 m : m ,
171184 }
172185}
173186
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 ) {
175193 // nestedVal checks if the name has '.' delimiters.
176194 // If so, it tries to traverse the tree by the '.' delimited sections to find
177195 // a nested value for the key.
@@ -205,26 +223,26 @@ func (ms *MapSource) lookup(name string) (any, bool) {
205223
206224type mapValueSource struct {
207225 key string
208- ms * MapSource
226+ ms MapSource
209227}
210228
211- func NewMapValueSource (key string , ms * MapSource ) ValueSource {
229+ func NewMapValueSource (key string , ms MapSource ) ValueSource {
212230 return & mapValueSource {
213231 key : key ,
214232 ms : ms ,
215233 }
216234}
217235
218236func (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 () )
220238}
221239
222240func (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 () )
224242}
225243
226244func (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 {
228246 return "" , false
229247 } else {
230248 return fmt .Sprintf ("%+v" , v ), true
0 commit comments