gapr/keys.go

51 lines
973 B
Go
Raw Normal View History

2023-01-30 21:06:18 +00:00
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 (
2023-01-31 20:14:34 +00:00
identityKeyMapper KeyMapper = KeyMapperFunc(func(orig string) string {
return orig
})
2023-01-30 21:06:18 +00:00
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)
)
2023-01-31 20:14:34 +00:00
func genericFirstKeyMapper(m func(r rune) rune) KeyMapperFunc {
return func(orig string) string {
2023-01-30 21:06:18 +00:00
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)
2023-01-31 20:14:34 +00:00
}
2023-01-30 21:06:18 +00:00
}