Skip to content

Commit 596e7c5

Browse files
committed
Add a map source interface
1 parent acc590b commit 596e7c5

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed

godoc-current.txt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -854,11 +854,19 @@ func (i MapBase[T, C, VC]) ToString(t map[string]T) string
854854
func (i *MapBase[T, C, VC]) Value() map[string]T
855855
Value returns the mapping of values set by this flag
856856

857-
type MapSource struct {
858-
// Has unexported fields.
857+
type MapSource interface {
858+
fmt.Stringer
859+
fmt.GoStringer
860+
861+
// Lookup returns the value from the source based on key
862+
// and if it was found
863+
// or returns an empty string and false
864+
Lookup(string) (any, bool)
859865
}
866+
MapSource is a source which can be used to look up a value based on a key
867+
typically for use with a cli.Flag
860868

861-
func NewMapSource(name string, m map[any]any) *MapSource
869+
func NewMapSource(name string, m map[any]any) MapSource
862870

863871
type MultiError interface {
864872
error
@@ -1012,7 +1020,7 @@ func EnvVar(key string) ValueSource
10121020

10131021
func File(path string) ValueSource
10141022

1015-
func NewMapValueSource(key string, ms *MapSource) ValueSource
1023+
func NewMapValueSource(key string, ms MapSource) ValueSource
10161024

10171025
type ValueSourceChain struct {
10181026
Chain []ValueSource

value_source.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

206224
type 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

218236
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())
220238
}
221239

222240
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())
224242
}
225243

226244
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 {
228246
return "", false
229247
} else {
230248
return fmt.Sprintf("%+v", v), true

value_source_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,6 @@ func TestMapValueSourceStringer(t *testing.T) {
325325
}
326326
mvs := NewMapValueSource("bar", NewMapSource("test", m))
327327

328-
assert.Equal(t, `&mapValueSource{key:"bar", src:map["foo":map["bar":'\n']]}`, mvs.GoString())
329-
assert.Equal(t, `map source key "bar" from "test"`, mvs.String())
328+
assert.Equal(t, `&mapValueSource{key:"bar", src:&mapSource{name:"test"}}`, mvs.GoString())
329+
assert.Equal(t, `key "bar" from map source "test"`, mvs.String())
330330
}

0 commit comments

Comments
 (0)