-
Notifications
You must be signed in to change notification settings - Fork 21
/
context.go
48 lines (37 loc) · 1009 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
43
44
45
46
47
48
package template
import "context"
const (
spi = "SourcePackageImport"
ael = "AllowEllipsis"
mainTagsContextKey = "MainTags"
)
func WithSourcePackageImport(parent context.Context, val string) context.Context {
return context.WithValue(parent, spi, val)
}
func SourcePackageImport(ctx context.Context) string {
return ctx.Value(spi).(string)
}
func WithTags(parent context.Context, tt TagsSet) context.Context {
return context.WithValue(parent, mainTagsContextKey, tt)
}
func Tags(ctx context.Context) TagsSet {
return ctx.Value(mainTagsContextKey).(TagsSet)
}
type TagsSet map[string]struct{}
func (s TagsSet) Has(item string) bool {
_, ok := s[item]
return ok
}
func (s TagsSet) HasAny(items ...string) bool {
if len(items) == 0 {
return false
}
return s.Has(items[0]) || s.HasAny(items[1:]...)
}
func (s TagsSet) Add(item string) {
s[item] = struct{}{}
}
func AllowEllipsis(ctx context.Context) bool {
v, ok := ctx.Value(ael).(bool)
return ok && v
}