package util import ( "fmt" "math/rand" "strings" "unicode" ) const ( maxNameLength = 30 specialChars = "!@#$%^*-_=+~()[]{}" // Allowed special characters ) var allowedSpecialChars map[rune]bool func init() { allowedSpecialChars = make(map[rune]bool) for _, r := range []rune(specialChars) { allowedSpecialChars[r] = true } } func ValidateTypeName(input string) (string, bool) { // Trim leading and trailing spaces trimmed := strings.TrimSpace(input) // If the string is empty after trimming, return false if len(trimmed) == 0 { return "", false } runes := []rune(trimmed) if len(runes) > maxNameLength { return "", false } // Dash cannot be at the beginning or end if allowedSpecialChars[runes[0]] || allowedSpecialChars[runes[len(runes)-1]] { return "", false } // if runes[0] == '-' || runes[len(runes)-1] == '-' { // return "", false // } var specialCount uint8 for _, r := range runes { // Check if the character is a whitespace, which is not allowed if unicode.IsSpace(r) { return "", false } // Letters (including any alphabet) and digits are allowed if unicode.IsLetter(r) || unicode.IsDigit(r) { specialCount = 0 continue } // Combining marks (accents) are allowed if unicode.IsMark(r) { specialCount = 0 continue } // Check for allowed special characters if allowedSpecialChars[r] { if specialCount == 2 { return "", false } specialCount++ continue } // If any other character is encountered, return false return "", false } // Return the trimmed string and true if all conditions are met return trimmed, true } func AppendRandomSuffix(v string) string { return AppendRandomSuffixGenerator(v, RandomSuffixGenerator) } func AppendRandomSuffixGenerator(v string, s func() string) string { suffix := []rune(s()) str := []rune(v) max := maxNameLength - len(suffix) if len(str) > max { str = str[:max] } return string(append(str, suffix...)) } func RandomSuffixGenerator() string { return fmt.Sprintf("%04d", rand.Intn(9999)) }