From 725150f19771cfc6d0b3db9ecd3a90a22c9f3c3d Mon Sep 17 00:00:00 2001 From: Stavros Date: Sun, 26 Oct 2025 11:58:36 +0200 Subject: [PATCH] refactor: use strcase lib to handle text case conversions --- go.mod | 1 + go.sum | 2 ++ internal/utils/decoders/decoders.go | 41 ++++------------------------- 3 files changed, 8 insertions(+), 36 deletions(-) diff --git a/go.mod b/go.mod index 12b29b9..2348875 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index be59848..741a59f 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/utils/decoders/decoders.go b/internal/utils/decoders/decoders.go index a3779c2..28b72fb 100644 --- a/internal/utils/decoders/decoders.go +++ b/internal/utils/decoders/decoders.go @@ -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 -}