-
Notifications
You must be signed in to change notification settings - Fork 5
/
context.go
42 lines (36 loc) · 904 Bytes
/
context.go
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
package format
import (
"fmt"
"sort"
"strings"
)
// Context ...
type Context interface {
GetFormatter(name string) (Formatter, error)
}
type contextFromBuilder map[string]Formatter
func (ctx contextFromBuilder) GetFormatter(name string) (res Formatter, err error) {
res, ok := ctx[name]
if !ok {
formatters := []string{}
for key := range ctx {
formatters = append(formatters, key)
}
sort.Sort(sort.StringSlice(formatters))
for i, key := range formatters {
formatters[i] = "\t" + key
}
err = fmt.Errorf(
"unknown formatter `%s`, only these are available:\n%s\n",
name,
strings.Join(formatters, "\n"),
)
}
return
}
// contextFunc implementation of context using given func as a source of information
type contextFunc func(string) string
func (f contextFunc) GetFormatter(name string) (Formatter, error) {
value := f(name)
return stringFormatter(value), nil
}