refactor: use strcase lib to handle text case conversions

This commit is contained in:
Stavros
2025-10-26 11:58:36 +02:00
parent ed3b1c49cb
commit 725150f197
3 changed files with 8 additions and 36 deletions

1
go.mod
View File

@@ -47,6 +47,7 @@ require (
github.com/quic-go/qpack v0.5.1 // indirect
github.com/quic-go/quic-go v0.54.1 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/stoewer/go-strcase v1.3.1 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.34.0 // indirect

2
go.sum
View File

@@ -259,6 +259,8 @@ github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.21.0 h1:x5S+0EU27Lbphp4UKm1C+1oQO+rKx36vfCoaVebLFSU=
github.com/spf13/viper v1.21.0/go.mod h1:P0lhsswPGWD/1lZJ9ny3fYnVqxiegrlNrEmgLjbTCAY=
github.com/stoewer/go-strcase v1.3.1 h1:iS0MdW+kVTxgMoE1LAZyMiYJFKlOzLooE4MxjirtkAs=
github.com/stoewer/go-strcase v1.3.1/go.mod h1:fAH5hQ5pehh+j3nZfvwdk2RgEgQjAoM8wodgtPmh1xo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=

View File

@@ -2,11 +2,9 @@ package decoders
import (
"reflect"
"regexp"
"strings"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"github.com/stoewer/go-strcase"
)
func normalizeKeys[T any](input map[string]string, root string, sep string) map[string]string {
@@ -47,11 +45,10 @@ func normalizeKeys[T any](input map[string]string, root string, sep string) map[
final := ""
for i, part := range parts {
if i == 0 {
final += kebabToCamel(part)
continue
if i > 0 {
final += "."
}
final += "." + kebabToCamel(part)
final += strcase.LowerCamelCase(part)
}
normalized[final] = v
@@ -67,41 +64,13 @@ func getKnownKeys[T any]() []string {
v := reflect.ValueOf(t)
typeOfT := v.Type()
re := regexp.MustCompile(`[A-Z]`)
for field := range typeOfT.NumField() {
if typeOfT.Field(field).Tag.Get("field") != "" {
keys = append(keys, typeOfT.Field(field).Tag.Get("field"))
continue
}
var key string
for i, char := range typeOfT.Field(field).Name {
if re.MatchString(string(char)) && i != 0 {
key += "-" + strings.ToLower(string(char))
continue
}
key += strings.ToLower(string(char))
}
keys = append(keys, key)
keys = append(keys, strcase.KebabCase(typeOfT.Field(field).Name))
}
return keys
}
func kebabToCamel(input string) string {
res := ""
parts := strings.Split(input, "-")
for i, part := range parts {
if i == 0 {
res += part
continue
}
res += cases.Title(language.English).String(part)
}
return res
}