mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-13 12:06:29 +00:00
34 lines
915 B
Go
34 lines
915 B
Go
|
package meddler
|
||
|
|
||
|
import (
|
||
|
"strings"
|
||
|
"unicode"
|
||
|
)
|
||
|
|
||
|
// MapperFunc signature. Argument is field name, return value is database column.
|
||
|
type MapperFunc func(in string) string
|
||
|
|
||
|
// Mapper defines the function to transform struct field names into database columns.
|
||
|
// Default is strings.TrimSpace, basically a no-op
|
||
|
var Mapper MapperFunc = strings.TrimSpace
|
||
|
|
||
|
// LowerCase returns a lowercased version of the input string
|
||
|
func LowerCase(in string) string {
|
||
|
return strings.ToLower(in)
|
||
|
}
|
||
|
|
||
|
// SnakeCase returns a snake_cased version of the input string
|
||
|
func SnakeCase(in string) string {
|
||
|
runes := []rune(in)
|
||
|
|
||
|
var out []rune
|
||
|
for i := 0; i < len(runes); i++ {
|
||
|
if i > 0 && (unicode.IsUpper(runes[i]) || unicode.IsNumber(runes[i])) && ((i+1 < len(runes) && unicode.IsLower(runes[i+1])) || unicode.IsLower(runes[i-1])) {
|
||
|
out = append(out, '_')
|
||
|
}
|
||
|
out = append(out, unicode.ToLower(runes[i]))
|
||
|
}
|
||
|
|
||
|
return string(out)
|
||
|
}
|