53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
package boltease
|
|
|
|
import (
|
|
"strings"
|
|
"unicode"
|
|
)
|
|
|
|
func isValidTag(s string) bool {
|
|
if s == "" {
|
|
return false
|
|
}
|
|
for _, c := range s {
|
|
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
|
|
}
|
|
|
|
// tagOptions is the string following a comma in a struct field's "boltease"
|
|
// tag, or the empty string. It does not include the leading comma
|
|
type tagOptions string
|
|
|
|
// parseTag splits a struct field's boltease tag into it sname and
|
|
// comma-separated options.
|
|
func parseTag(tag string) (string, tagOptions) {
|
|
tag, opt, _ := strings.Cut(tag, ",")
|
|
return tag, tagOptions(opt)
|
|
}
|
|
|
|
// Contains reports whether a comma-separated list of options
|
|
// contains a particular substr flag. Substr must be surrounded by a
|
|
// string boundary or commas.
|
|
func (o tagOptions) Contains(optionName string) bool {
|
|
if len(o) == 0 {
|
|
return false
|
|
}
|
|
s := string(o)
|
|
for s != "" {
|
|
var name string
|
|
name, s, _ = strings.Cut(s, ",")
|
|
if name == optionName {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|