generated from dogmatiq/template-go
/
attributes.go
415 lines (344 loc) 路 9.09 KB
/
attributes.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package dnssd
import (
"bytes"
"errors"
"fmt"
"sort"
"strings"
)
// Attributes represents the set of attributes conveyed in a DNS-SD service
// instance's TXT record.
//
// Each attribute may be either a key/value pair, where the value is a byte
// slice, or a flag (called a boolean attribute in RFC 6763).
//
// Pairs and flags occupy the same keyspace, meaning that it is not possible to
// have a flag with the same name as a pair's key.
//
// This is a consequence of how the attributes are represented inside the TXT
// records. A flag is represented as a key without value, which is also distinct
// from a pair with an empty value.
//
// Keys are case-insensitive. They MUST be at least one character long and
// SHOULD NOT be longer than 9 characters. The characters of a key MUST be
// printable US-ASCII values (0x20-0x7E), excluding '=' (0x3D).
//
// See https://www.rfc-editor.org/rfc/rfc6763#section-6.1
//
// Attributes is not safe for concurrent use without synchronization.
type Attributes struct {
// m is a map of normalized key to value.
//
// A value of nil means the attribute is a flag, any non-nil byte slice
// (including the empty slice) is a regular binary attribute.
m map[string][]byte
}
// NewAttributes returns a new empty attribute set.
func NewAttributes() Attributes {
return Attributes{}
}
// Get returns the value that is associated with the key k.
//
// ok is true there is a key/value pair with this key.
func (a Attributes) Get(k string) (v []byte, ok bool) {
v = a.m[mustNormalizeAttributeKey(k)]
return v, v != nil
}
// WithPair returns a clone of the attributes with an additional key/value pair.
//
// It replaces any existing key/value pair or flag with this key.
func (a Attributes) WithPair(k string, v []byte) Attributes {
return a.mutate(func(m map[string][]byte) {
// If v is nil, replace it with an empty slice instead, otherwise it is
// considered a flag.
if v == nil {
v = []byte{}
}
m[mustNormalizeAttributeKey(k)] = v
})
}
// Pairs returns the key/value pair (i.e. non-flag) attributes.
func (a Attributes) Pairs() map[string][]byte {
attrs := map[string][]byte{}
for k, v := range a.m {
if v != nil {
attrs[k] = v
}
}
return attrs
}
// WithFlag returns a lcone of the attributes with an additional flag.
//
// It replaces any existing key/value pair with this key.
//
// Use Without() to clear a flag.
func (a Attributes) WithFlag(k string) Attributes {
return a.mutate(func(m map[string][]byte) {
m[mustNormalizeAttributeKey(k)] = nil
})
}
// HasFlags returns true if all of the given flags are present in the
// attributes.
func (a Attributes) HasFlags(keys ...string) bool {
for _, k := range keys {
v, ok := a.m[mustNormalizeAttributeKey(k)]
if !ok || v != nil {
return false
}
}
return true
}
// Flags returns the flag (i.e. non-pair) attributes that are set.
func (a Attributes) Flags() map[string]struct{} {
flags := map[string]struct{}{}
for k, v := range a.m {
if v == nil {
flags[k] = struct{}{}
}
}
return flags
}
// Without returns a clone of the attributes without the given keys, regardless
// of whether they are key/value pairs or flags.
func (a Attributes) Without(keys ...string) Attributes {
return a.mutate(func(m map[string][]byte) {
for _, k := range keys {
delete(m, mustNormalizeAttributeKey(k))
}
})
}
// IsEmpty returns true if there are no attributes present.
func (a Attributes) IsEmpty() bool {
return len(a.m) == 0
}
// WithTXT returns a clone of the attributes containing an attribute parsed from
// a single value within in a DNS-SD service instance's TXT record.
//
// As per RFC 6763, TXT record values that begin with an '=' are ignored, in
// which case ok is false. Empty values are also ignored.
func (a Attributes) WithTXT(pair string) (_ Attributes, ok bool, err error) {
if pair == "" {
return a, false, nil
}
var (
k string
v []byte
)
switch n := strings.IndexByte(pair, '='); n {
case 0:
// DNS-SD TXT record strings beginning with an '=' character
// (i.e., the key is missing) MUST be silently ignored.
return a, false, nil
case -1:
// No equals sign, attribute is a flag.
k = pair
default:
v = []byte(pair[n+1:])
k = pair[:n]
}
k, err = normalizeAttributeKey(k)
if err != nil {
return Attributes{}, false, err
}
return a.mutate(func(m map[string][]byte) {
m[k] = v
}), true, nil
}
// ToTXT returns the string representation of each key/value pair, as they
// appear in the TXT record.
//
// The result is deterministic (keys are sorted) to avoid unnecessary DNS churn
// when the attributes are used to construct DNS records.
func (a Attributes) ToTXT() []string {
type pair struct {
key string
value []byte
}
pairs := make([]pair, 0, len(a.m))
for k, v := range a.m {
pairs = append(pairs, pair{k, v})
}
// https://www.rfc-editor.org/rfc/rfc6763#section-6.7
//
// Always place the 'version tag' attribute ("txtvers") in the first
// entry of the TXT record.
const versionKey = "txtvers"
sort.Slice(
pairs,
func(i, j int) bool {
a := pairs[i]
b := pairs[j]
if a.key == versionKey {
return true
}
if b.key == versionKey {
return false
}
return a.key < b.key
},
)
var result []string
for _, p := range pairs {
if p.value == nil {
// https://www.rfc-editor.org/rfc/rfc6763#section-6.4
//
// If there is no '=' in a DNS-SD TXT record string, then it is a
// boolean attribute, simply identified as being present, with no
// value.
result = append(result, p.key)
} else {
result = append(result, p.key+"="+string(p.value))
}
}
return result
}
// Equal returns true if the attributes are equal.
func (a Attributes) Equal(attr Attributes) bool {
if len(a.m) != len(attr.m) {
return false
}
for k, v1 := range a.m {
v2, ok := attr.m[k]
if !ok {
return false
}
isFlag1 := v1 == nil
isFlag2 := v2 == nil
if isFlag1 != isFlag2 {
return false
}
if !bytes.Equal(v1, v2) {
return false
}
}
return true
}
func (a Attributes) mutate(
fn func(map[string][]byte),
) Attributes {
m := make(map[string][]byte, len(a.m))
for k, v := range a.m {
m[k] = v
}
fn(m)
return Attributes{m}
}
// mustNormalizeAttributeKey normalizes the DNS-SD TXT key, k, or panics if it
// can not be normalized.
//
// See https://www.rfc-editor.org/rfc/rfc6763#section-6.4
func mustNormalizeAttributeKey(k string) string {
k, err := normalizeAttributeKey(k)
if err != nil {
panic(err)
}
return k
}
// normalizeAttributeKey normalizes the DNS-SD TXT key, k, or returns an error
// if it can not be normalized.
//
// See https://www.rfc-editor.org/rfc/rfc6763#section-6.4
func normalizeAttributeKey(k string) (string, error) {
if k == "" {
return "", errors.New("key must not be empty")
}
var w strings.Builder
for i := 0; i < len(k); i++ {
ch := k[i]
// https://www.rfc-editor.org/rfc/rfc6763#section-6.4
//
// The characters of a key MUST be printable US-ASCII values (0x20-0x7E)
// [RFC20], excluding '=' (0x3D).
if ch == '=' {
return "", fmt.Errorf("invalid key '%s', key must not contain '=' character", k)
}
if ch < 0x20 || ch > 0x7E {
return "", fmt.Errorf("invalid key '%s', key must contain only printable ASCII characters", k)
}
// Convert ASCII letters to lowercase.
if 'A' <= ch && ch <= 'Z' {
ch += 'a' - 'A'
}
w.WriteByte(ch)
}
return w.String(), nil
}
// AttributeCollection is a collection of [Attributes]. Each entry in the slice
// contains the attributes conveyed in a separate TXT record.
type AttributeCollection []Attributes
// Get returns the last value that is associated with the key k.
//
// ok is true there is a key/value pair with this key.
func (c AttributeCollection) Get(k string) (v []byte, ok bool) {
k = mustNormalizeAttributeKey(k)
for i := len(c) - 1; i >= 0; i-- {
a := c[i]
v = a.m[k]
if v != nil {
return v, true
}
}
return nil, false
}
// Pairs returns the key/value pair (i.e. non-flag) attributes.
func (c AttributeCollection) Pairs() map[string][]byte {
attrs := map[string][]byte{}
for _, a := range c {
for k, v := range a.m {
if v != nil {
attrs[k] = v
}
}
}
return attrs
}
// HasFlags returns true if all of the given flags are present in the
// attributes.
func (c AttributeCollection) HasFlags(keys ...string) bool {
key:
for _, k := range keys {
k = mustNormalizeAttributeKey(k)
for _, a := range c {
v, ok := a.m[k]
if ok && v == nil {
continue key
}
}
return false
}
return true
}
// Flags returns the flag (i.e. non-pair) attributes that are set.
func (c AttributeCollection) Flags() map[string]struct{} {
flags := map[string]struct{}{}
for _, a := range c {
for k, v := range a.m {
if v == nil {
flags[k] = struct{}{}
}
}
}
return flags
}
// Equal returns true if c and x contain the same sets of attributes, in any
// order.
func (c AttributeCollection) Equal(x AttributeCollection) bool {
if len(c) != len(x) {
return false
}
visited := make([]bool, len(x))
left:
for _, l := range c {
for i, r := range x {
if visited[i] {
continue
}
if l.Equal(r) {
visited[i] = true
continue left
}
}
return false
}
return true
}