gobuf/utils.go

48 lines
840 B
Go
Raw Permalink Normal View History

2023-03-20 17:08:22 +00:00
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()
}
2023-06-25 15:02:26 +00:00
func toLowerCamelCase(str string) string {
if len(str) != 0 {
return string(unicode.ToLower(rune(str[0]))) + str[1:]
}
return ""
}
2023-09-29 04:26:01 +00:00
func toUpperCamlCase(str string) string {
if len(str) != 0 {
return string(unicode.ToUpper(rune(str[0]))) + str[1:]
}
return ""
}
2023-06-25 15:02:26 +00:00
func getFieldValue(fieldName string, cases TagValueStyle) string {
switch cases {
case Underline:
return calm2Case(fieldName)
case LowerCase:
return toLowerCamelCase(fieldName)
default:
return fieldName
}
}