50 lines
973 B
Go
50 lines
973 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 (
|
|
identityKeyMapper KeyMapper = KeyMapperFunc(func(orig string) string {
|
|
return orig
|
|
})
|
|
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) KeyMapperFunc {
|
|
return 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)
|
|
}
|
|
}
|