gobuf/utils.go
2024-03-19 23:38:00 +08:00

48 lines
840 B
Go

package gobuf
import (
"bytes"
"unicode"
)
// calm2Case 驼峰转下划线
func calm2Case(src string) string {
buffer := new(bytes.Buffer)
for i, r := range src {
if unicode.IsUpper(r) {
if i != 0 {
buffer.WriteRune('_')
}
buffer.WriteRune(unicode.ToLower(r))
} else {
buffer.WriteRune(r)
}
}
return buffer.String()
}
func toLowerCamelCase(str string) string {
if len(str) != 0 {
return string(unicode.ToLower(rune(str[0]))) + str[1:]
}
return ""
}
func toUpperCamlCase(str string) string {
if len(str) != 0 {
return string(unicode.ToUpper(rune(str[0]))) + str[1:]
}
return ""
}
func getFieldValue(fieldName string, cases TagValueStyle) string {
switch cases {
case Underline:
return calm2Case(fieldName)
case LowerCase:
return toLowerCamelCase(fieldName)
default:
return fieldName
}
}