2016-08-03 23:42:21 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
// mapFetcher provides an implementation of the Fetcher interface by wrapping
|
|
|
|
// the `map[string]string` type.
|
2017-04-12 21:29:11 +00:00
|
|
|
type mapFetcher map[string][]string
|
2016-08-03 23:42:21 +00:00
|
|
|
|
2017-04-12 21:29:11 +00:00
|
|
|
func UniqMapFetcher(m map[string]string) Fetcher {
|
|
|
|
multi := make(map[string][]string, len(m))
|
|
|
|
for k, v := range m {
|
|
|
|
multi[k] = []string{v}
|
|
|
|
}
|
|
|
|
|
|
|
|
return MapFetcher(multi)
|
|
|
|
}
|
|
|
|
|
|
|
|
func MapFetcher(m map[string][]string) Fetcher {
|
2016-08-05 00:58:43 +00:00
|
|
|
return mapFetcher(m)
|
|
|
|
}
|
|
|
|
|
2016-08-03 23:42:21 +00:00
|
|
|
// Get implements the func `Fetcher.Get`.
|
2016-08-05 21:59:57 +00:00
|
|
|
func (m mapFetcher) Get(key string) (val string, ok bool) {
|
2017-04-12 21:29:11 +00:00
|
|
|
all := m.GetAll(key)
|
|
|
|
|
|
|
|
if len(all) == 0 {
|
|
|
|
return "", false
|
|
|
|
}
|
2017-04-14 17:29:52 +00:00
|
|
|
return all[len(all)-1], true
|
2017-04-12 21:29:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get implements the func `Fetcher.GetAll`.
|
|
|
|
func (m mapFetcher) GetAll(key string) []string {
|
|
|
|
return m[key]
|
2016-08-05 21:59:57 +00:00
|
|
|
}
|
2016-11-10 00:33:43 +00:00
|
|
|
|
2017-04-12 21:29:11 +00:00
|
|
|
func (m mapFetcher) All() map[string][]string {
|
|
|
|
newmap := make(map[string][]string)
|
|
|
|
for key, values := range m {
|
|
|
|
for _, value := range values {
|
|
|
|
newmap[key] = append(newmap[key], value)
|
|
|
|
}
|
2016-11-10 00:33:43 +00:00
|
|
|
}
|
|
|
|
return newmap
|
|
|
|
}
|