48 lines
894 B
Go
48 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)
|
||
|
})
|
||
|
}
|