mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2025-10-28 04:35:40 +00:00
refactor: use strcase lib to handle text case conversions
This commit is contained in:
1
go.mod
1
go.mod
@@ -47,6 +47,7 @@ require (
|
|||||||
github.com/quic-go/qpack v0.5.1 // indirect
|
github.com/quic-go/qpack v0.5.1 // indirect
|
||||||
github.com/quic-go/quic-go v0.54.1 // indirect
|
github.com/quic-go/quic-go v0.54.1 // indirect
|
||||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // 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
|
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
|
||||||
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
|
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
|
||||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.34.0 // indirect
|
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.34.0 // indirect
|
||||||
|
|||||||
2
go.sum
2
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/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 h1:x5S+0EU27Lbphp4UKm1C+1oQO+rKx36vfCoaVebLFSU=
|
||||||
github.com/spf13/viper v1.21.0/go.mod h1:P0lhsswPGWD/1lZJ9ny3fYnVqxiegrlNrEmgLjbTCAY=
|
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.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.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||||
|
|||||||
@@ -2,11 +2,9 @@ package decoders
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"golang.org/x/text/cases"
|
"github.com/stoewer/go-strcase"
|
||||||
"golang.org/x/text/language"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func normalizeKeys[T any](input map[string]string, root string, sep string) map[string]string {
|
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 := ""
|
final := ""
|
||||||
|
|
||||||
for i, part := range parts {
|
for i, part := range parts {
|
||||||
if i == 0 {
|
if i > 0 {
|
||||||
final += kebabToCamel(part)
|
final += "."
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
final += "." + kebabToCamel(part)
|
final += strcase.LowerCamelCase(part)
|
||||||
}
|
}
|
||||||
|
|
||||||
normalized[final] = v
|
normalized[final] = v
|
||||||
@@ -67,41 +64,13 @@ func getKnownKeys[T any]() []string {
|
|||||||
v := reflect.ValueOf(t)
|
v := reflect.ValueOf(t)
|
||||||
typeOfT := v.Type()
|
typeOfT := v.Type()
|
||||||
|
|
||||||
re := regexp.MustCompile(`[A-Z]`)
|
|
||||||
|
|
||||||
for field := range typeOfT.NumField() {
|
for field := range typeOfT.NumField() {
|
||||||
if typeOfT.Field(field).Tag.Get("field") != "" {
|
if typeOfT.Field(field).Tag.Get("field") != "" {
|
||||||
keys = append(keys, typeOfT.Field(field).Tag.Get("field"))
|
keys = append(keys, typeOfT.Field(field).Tag.Get("field"))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
keys = append(keys, strcase.KebabCase(typeOfT.Field(field).Name))
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return keys
|
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
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user