...
1
2
3
4
5 package label
6
7 import (
8 "fmt"
9 "io"
10 "reflect"
11 "unsafe"
12 )
13
14
15
16
17 type Key interface {
18
19 Name() string
20
21 Description() string
22
23
24
25
26
27 Format(w io.Writer, buf []byte, l Label)
28 }
29
30
31
32 type Label struct {
33 key Key
34 packed uint64
35 untyped interface{}
36 }
37
38
39 type Map interface {
40
41 Find(key Key) Label
42 }
43
44
45
46
47 type List interface {
48
49
50 Valid(index int) bool
51
52 Label(index int) Label
53 }
54
55
56 type list struct {
57 labels []Label
58 }
59
60
61 type filter struct {
62 keys []Key
63 underlying List
64 }
65
66
67 type listMap struct {
68 labels []Label
69 }
70
71
72 type mapChain struct {
73 maps []Map
74 }
75
76
77
78
79 func OfValue(k Key, value interface{}) Label { return Label{key: k, untyped: value} }
80
81
82
83
84
85 func (t Label) UnpackValue() interface{} { return t.untyped }
86
87
88
89
90
91 func Of64(k Key, v uint64) Label { return Label{key: k, packed: v} }
92
93
94
95
96
97 func (t Label) Unpack64() uint64 { return t.packed }
98
99 type stringptr unsafe.Pointer
100
101
102
103
104 func OfString(k Key, v string) Label {
105 hdr := (*reflect.StringHeader)(unsafe.Pointer(&v))
106 return Label{
107 key: k,
108 packed: uint64(hdr.Len),
109 untyped: stringptr(hdr.Data),
110 }
111 }
112
113
114
115
116
117 func (t Label) UnpackString() string {
118 var v string
119 hdr := (*reflect.StringHeader)(unsafe.Pointer(&v))
120 hdr.Data = uintptr(t.untyped.(stringptr))
121 hdr.Len = int(t.packed)
122 return v
123 }
124
125
126 func (t Label) Valid() bool { return t.key != nil }
127
128
129 func (t Label) Key() Key { return t.key }
130
131
132 func (t Label) Format(f fmt.State, r rune) {
133 if !t.Valid() {
134 io.WriteString(f, `nil`)
135 return
136 }
137 io.WriteString(f, t.Key().Name())
138 io.WriteString(f, "=")
139 var buf [128]byte
140 t.Key().Format(f, buf[:0], t)
141 }
142
143 func (l *list) Valid(index int) bool {
144 return index >= 0 && index < len(l.labels)
145 }
146
147 func (l *list) Label(index int) Label {
148 return l.labels[index]
149 }
150
151 func (f *filter) Valid(index int) bool {
152 return f.underlying.Valid(index)
153 }
154
155 func (f *filter) Label(index int) Label {
156 l := f.underlying.Label(index)
157 for _, f := range f.keys {
158 if l.Key() == f {
159 return Label{}
160 }
161 }
162 return l
163 }
164
165 func (lm listMap) Find(key Key) Label {
166 for _, l := range lm.labels {
167 if l.Key() == key {
168 return l
169 }
170 }
171 return Label{}
172 }
173
174 func (c mapChain) Find(key Key) Label {
175 for _, src := range c.maps {
176 l := src.Find(key)
177 if l.Valid() {
178 return l
179 }
180 }
181 return Label{}
182 }
183
184 var emptyList = &list{}
185
186 func NewList(labels ...Label) List {
187 if len(labels) == 0 {
188 return emptyList
189 }
190 return &list{labels: labels}
191 }
192
193 func Filter(l List, keys ...Key) List {
194 if len(keys) == 0 {
195 return l
196 }
197 return &filter{keys: keys, underlying: l}
198 }
199
200 func NewMap(labels ...Label) Map {
201 return listMap{labels: labels}
202 }
203
204 func MergeMaps(srcs ...Map) Map {
205 var nonNil []Map
206 for _, src := range srcs {
207 if src != nil {
208 nonNil = append(nonNil, src)
209 }
210 }
211 if len(nonNil) == 1 {
212 return nonNil[0]
213 }
214 return mapChain{maps: nonNil}
215 }
216
View as plain text