gapr/keys.go
Peter ec31dd14d4
All checks were successful
concourse-ci/lint/golangci-lint Lint Go files
refactor: apply golangci-lint findings
2023-01-31 21:14:34 +01:00

51 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)
}
}