gapr/keys.go
2023-01-30 22:06:18 +01:00

47 lines
894 B
Go

package gapr
import (
"strings"
"unicode"
)
type KeyMapper interface {
MapKey(orig string) string
}
type KeyMapperFunc func(orig string) string
func (f KeyMapperFunc) MapKey(orig string) string {
return f(orig)
}
var (
LowercaseKeyMapper KeyMapper = KeyMapperFunc(func(orig string) string {
return strings.ToLower(orig)
})
UppercaseKeyMapper KeyMapper = KeyMapperFunc(func(orig string) string {
return strings.ToUpper(orig)
})
CamelCaseKeyMapper = genericFirstKeyMapper(unicode.ToLower)
PascalCaseKeyMapper = genericFirstKeyMapper(unicode.ToUpper)
)
func genericFirstKeyMapper(m func(r rune) rune) KeyMapper {
return KeyMapperFunc(func(orig string) string {
if len(orig) < 1 {
return ""
}
mapped := make([]byte, 1, len(orig))
mapped[0] = byte(m(rune(orig[0])))
if len(orig) > 1 {
mapped = append(mapped, orig[1:]...)
}
return string(mapped)
})
}