Skip to content

Commit

Permalink
feat(reflectx): is a valid tag
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Jan 14, 2025
1 parent 54f4387 commit 276eded
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
27 changes: 26 additions & 1 deletion reflectx/tags.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package reflectx

import "strings"
import (
"strings"
"unicode"

"github.com/zeiss/pkg/utilx"
)

// TagOptions ...
type TagOptions string
Expand All @@ -11,3 +16,23 @@ func ParseTag(tag string) (string, TagOptions) {
tag, opt, _ := strings.Cut(tag, ",")
return tag, TagOptions(opt)
}

// IsValidTag returns true if the tag is not empty.
func IsValidTag(tag string) bool {
if utilx.Empty(tag) {
return false
}

for _, c := range tag {
switch {
case strings.ContainsRune("!#$%&()*+-./:;<=>?@[]^_{|}~ ", c):
// Backslash and quote chars are reserved, but
// otherwise any punctuation chars are allowed
// in a tag name.
case !unicode.IsLetter(c) && !unicode.IsDigit(c):
return false
}
}

return true
}
22 changes: 22 additions & 0 deletions reflectx/tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,25 @@ func TestParseTag(t *testing.T) {
assert.Equal(t, test.expectedOptions, opt)
}
}

func TestIsValidTag(t *testing.T) {
t.Parallel()

tests := []struct {
tag string
expected bool
}{
{
tag: "name",
expected: true,
},
{
tag: "",
expected: false,
},
}

for _, test := range tests {
assert.Equal(t, test.expected, reflectx.IsValidTag(test.tag))
}
}

0 comments on commit 276eded

Please sign in to comment.