Files
core/utils/ext.go

47 lines
928 B
Go
Raw Normal View History

2025-02-07 13:01:38 +08:00
package utils
import (
2026-08-11 15:27:21 +08:00
"fmt"
"regexp"
2025-02-07 13:01:38 +08:00
"strconv"
"strings"
)
2026-02-22 14:31:06 +08:00
func If(condition bool, trueValue, falseValue any) any {
2025-02-07 13:01:38 +08:00
if condition {
return trueValue
}
return falseValue
}
2025-08-22 12:15:55 +08:00
// 如果首字母是小写字母, 则变换为大写字母
2025-02-07 13:01:38 +08:00
func FirstToUpper(str string) string {
if str == "" {
return ""
}
return strings.ToUpper(str[:1]) + strings.ToLower(str[1:])
}
2026-02-22 14:31:06 +08:00
func ParseParams(in map[string]string) map[string]any {
out := make(map[string]any)
2025-02-07 13:01:38 +08:00
for k, v := range in {
fv, err := strconv.ParseFloat(v, 64)
if err != nil {
out[k] = fv
} else {
out[k] = v
}
}
return out
}
2026-08-11 15:27:21 +08:00
func MustString(value string) (string, error) {
var workspacePattern = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9_-]{0,63}$`)
value = strings.ToLower(strings.TrimSpace(value))
if !workspacePattern.MatchString(value) {
return "", fmt.Errorf("workspace must match %s", workspacePattern.String())
}
return value, nil
}