Skip to content

Commit

Permalink
Remove regex to check the extension name (#617)
Browse files Browse the repository at this point in the history
* Remove regex to check the extension name

Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>

* Fix uppercase

Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
  • Loading branch information
slinkydeveloper authored Nov 24, 2020
1 parent 44f150f commit 2c1245b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion v2/event/eventcontext_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (ec EventContextV1) ExtensionAs(name string, obj interface{}) error {
// SetExtension adds the extension 'name' with value 'value' to the CloudEvents context.
// This function fails if the name doesn't respect the regex ^[a-zA-Z0-9]+$
func (ec *EventContextV1) SetExtension(name string, value interface{}) error {
if !IsAlphaNumeric(name) {
if !IsExtensionNameValid(name) {
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")
}

Expand Down
13 changes: 11 additions & 2 deletions v2/event/extensions.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package event

import (
"regexp"
"strings"
)

Expand All @@ -21,4 +20,14 @@ func caseInsensitiveSearch(key string, space map[string]interface{}) (interface{
return nil, false
}

var IsAlphaNumeric = regexp.MustCompile(`^[a-zA-Z0-9]+$`).MatchString
func IsExtensionNameValid(key string) bool {
if len(key) < 1 || len(key) > 20 {
return false
}
for _, c := range key {
if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {
return false
}
}
return true
}

0 comments on commit 2c1245b

Please sign in to comment.