-
Notifications
You must be signed in to change notification settings - Fork 7
/
abs.go
134 lines (107 loc) · 2.78 KB
/
abs.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package annotations
import (
"golang.org/x/net/context"
"strings"
)
type Annotated interface {
GetAnnotations() map[string]string
}
const ctxValue = "annotationsCtx"
func WithContext(parent context.Context, annotated Annotated) context.Context {
existingAnnotations := FromCtx(parent).GetAnnotations()
return context.WithValue(parent, ctxValue, merge(existingAnnotations, annotated.GetAnnotations()))
}
func merge(annotations map[string]string, annotations2 map[string]string) Annotated {
if annotations == nil {
annotations = make(map[string]string)
}
if annotations2 == nil {
annotations2 = make(map[string]string)
}
for key, value := range annotations2 {
annotations[key] = value
}
return &annotated{
annotations: annotations,
}
}
func SetWithContext(parent context.Context, name, value string) context.Context {
val := FromCtx(parent).GetAnnotations()
if val == nil {
val = make(map[string]string)
}
val[name] = value
return WithContext(parent, &annotated{
annotations: val,
})
}
type annotated struct {
annotations map[string]string
}
func (a *annotated) GetAnnotations() map[string]string {
return a.annotations
}
//goland:noinspection GoUnusedExportedFunction
func FromCtx(ctx context.Context) Annotated {
if annotations, ok := ctx.Value(ctxValue).(Annotated); ok {
return annotations
}
return &annotated{
annotations: make(map[string]string),
}
}
func FromMap(m map[string]string) Annotated {
return &annotated{
annotations: m,
}
}
func IsEnabled(resource Annotated, name string) bool {
return resource.GetAnnotations() != nil && resource.GetAnnotations()[name] == "true"
}
func IsEnabledOnCtx(ctx context.Context, name string) bool {
return IsEnabled(FromCtx(ctx), name)
}
func Enable(resource Annotated, names ...string) {
for _, name := range names {
resource.GetAnnotations()[name] = "true"
}
}
func EnableWith(annotations map[string]string, names ...string) map[string]string {
if annotations == nil {
annotations = make(map[string]string)
}
for _, name := range names {
annotations[name] = "true"
}
return annotations
}
func Set(resource Annotated, name, value string) {
resource.GetAnnotations()[name] = value
}
func Get(resource Annotated, name string) string {
return resource.GetAnnotations()[name]
}
//goland:noinspection GoUnusedExportedFunction
func Disable(resource Annotated, names ...string) {
for _, name := range names {
resource.GetAnnotations()[name] = "false"
}
}
func ToString(resource Annotated) string {
var parts []string
for key, value := range resource.GetAnnotations() {
parts = append(parts, key+"="+value)
}
return strings.Join(parts, ";")
}
func IsSame(a, b map[string]string) bool {
if len(a) != len(b) {
return false
}
for key := range a {
if a[key] != b[key] {
return false
}
}
return true
}