-
Notifications
You must be signed in to change notification settings - Fork 218
/
extensions.go
57 lines (49 loc) · 1.51 KB
/
extensions.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
/*
Copyright 2021 The CloudEvents Authors
SPDX-License-Identifier: Apache-2.0
*/
package event
import (
"errors"
"fmt"
"strings"
)
const (
// DataContentEncodingKey is the key to DeprecatedDataContentEncoding for versions that do not support data content encoding
// directly.
DataContentEncodingKey = "datacontentencoding"
)
var (
// This determines the behavior of validateExtensionName(). For MaxExtensionNameLength > 0, an error will be returned,
// if len(key) > MaxExtensionNameLength
MaxExtensionNameLength = 0
)
func caseInsensitiveSearch(key string, space map[string]interface{}) (interface{}, bool) {
lkey := strings.ToLower(key)
for k, v := range space {
if strings.EqualFold(lkey, strings.ToLower(k)) {
return v, true
}
}
return nil, false
}
func IsExtensionNameValid(key string) bool {
if err := validateExtensionName(key); err != nil {
return false
}
return true
}
func validateExtensionName(key string) error {
if len(key) < 1 {
return errors.New("bad key, CloudEvents attribute names MUST NOT be empty")
}
if MaxExtensionNameLength > 0 && len(key) > MaxExtensionNameLength {
return fmt.Errorf("bad key, CloudEvents attribute name '%s' is longer than %d characters", key, MaxExtensionNameLength)
}
for _, c := range key {
if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {
return errors.New("bad key, CloudEvents attribute names MUST consist of lower-case letters ('a' to 'z') or digits ('0' to '9') from the ASCII character set")
}
}
return nil
}