File: map_fetcher.go

package info (click to toggle)
git-lfs 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,808 kB
  • sloc: sh: 21,256; makefile: 507; ruby: 417
file content (43 lines) | stat: -rw-r--r-- 943 bytes parent folder | download | duplicates (4)
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
package config

// mapFetcher provides an implementation of the Fetcher interface by wrapping
// the `map[string]string` type.
type mapFetcher map[string][]string

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 {
	return mapFetcher(m)
}

// Get implements the func `Fetcher.Get`.
func (m mapFetcher) Get(key string) (val string, ok bool) {
	all := m.GetAll(key)

	if len(all) == 0 {
		return "", false
	}
	return all[len(all)-1], true
}

// Get implements the func `Fetcher.GetAll`.
func (m mapFetcher) GetAll(key string) []string {
	return m[key]
}

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)
		}
	}
	return newmap
}