diff --git a/config/config.go b/config/config.go index b50a576..25953f0 100644 --- a/config/config.go +++ b/config/config.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "path/filepath" + "reflect" "sort" "strings" @@ -46,6 +47,21 @@ func LoadFromPath[T any](val *T, cfgPath string) { return err }) + valType := reflect.TypeOf(val) + for { + if valType.Kind() != reflect.Ptr { + break + } + + valType = valType.Elem() + } + if valType.Kind() != reflect.Struct { + log.Panic(). + Str("config_path", cfgPath). + Str("type", fmt.Sprintf("%#v", val)). + Msg("config type not correct") + } + parentDir := filepath.Dir(cfgPath) configBytes := result.Of(os.ReadFile(cfgPath)).Expect("failed to read config data: %s", cfgPath) configBytes = result.Of(envsubst.Bytes(configBytes)).Expect("failed to handler config env data: %s", cfgPath) @@ -140,7 +156,12 @@ func LoadFromPath[T any](val *T, cfgPath string) { assert.Exit(Merge(val, cfgList...), "failed to merge config") } -func Load[T any]() T { +type Cfg[T any] struct { + T T + P *T +} + +func Load[T any]() Cfg[T] { if configPath != "" { configDir = filepath.Dir(configPath) } else { @@ -149,5 +170,5 @@ func Load[T any]() T { var cfg T LoadFromPath(&cfg, configPath) - return cfg + return Cfg[T]{T: cfg, P: &cfg} } diff --git a/config/util.go b/config/util.go index f56dcda..3e3a6b8 100644 --- a/config/util.go +++ b/config/util.go @@ -59,7 +59,7 @@ func getConfigPath(name, typ string, configDir ...string) (string, string) { } } - log.Fatal().Msgf("config not found in: %v", notFoundPath) + log.Panic().Msgf("config not found in: %v", notFoundPath) return "", "" } diff --git a/env/util.go b/env/util.go index 42dd3bb..d5ed792 100644 --- a/env/util.go +++ b/env/util.go @@ -2,13 +2,23 @@ package env import ( "strings" + + strcase "github.com/ettle/strcase" ) +var replacer = strcase.NewCaser( + true, + map[string]bool{"SSL": true, "HTML": false}, + strcase.NewSplitFn( + []rune{'*', '.', ',', '-', '/'}, + strcase.SplitCase, + strcase.SplitAcronym, + strcase.PreserveNumberFormatting, + )) var trim = strings.TrimSpace -var replacer = strings.NewReplacer("-", "_", ".", "_", "/", "_") func KeyHandler(key string) string { - return strings.ToUpper(trim(strings.ReplaceAll(replacer.Replace(key), "__", "_"))) + return strings.ToUpper(trim(strings.ReplaceAll(replacer.ToSNAKE(key), "__", "_"))) } // Normalize a-b=>a_b, a.b=>a_b, a/b=>a_b diff --git a/env/util_test.go b/env/util_test.go index beb375b..1846b4d 100644 --- a/env/util_test.go +++ b/env/util_test.go @@ -9,5 +9,5 @@ import ( func TestNormalize(t *testing.T) { k, ok := Normalize("aA-bS3_AK/c.d") assert.True(t, ok) - assert.Equal(t, k, "AA_BS3_AK_C_D") + assert.Equal(t, k, "A_A_B_S3_AK_C_D") } diff --git a/go.mod b/go.mod index 7cdf315..029416d 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( github.com/dave/jennifer v1.7.0 github.com/deckarep/golang-set/v2 v2.6.0 github.com/dustin/go-humanize v1.0.0 + github.com/ettle/strcase v0.2.0 github.com/expr-lang/expr v1.16.9 github.com/flosch/pongo2/v6 v6.0.0 github.com/goccy/go-json v0.10.2 diff --git a/go.sum b/go.sum index 55193b1..ff6d823 100644 --- a/go.sum +++ b/go.sum @@ -73,6 +73,8 @@ github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4 github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= +github.com/ettle/strcase v0.2.0 h1:fGNiVF21fHXpX1niBgk0aROov1LagYsOwV/xqKDKR/Q= +github.com/ettle/strcase v0.2.0/go.mod h1:DajmHElDSaX76ITe3/VHVyMin4LWSJN5Z909Wp+ED1A= github.com/expr-lang/expr v1.16.9 h1:WUAzmR0JNI9JCiF0/ewwHB1gmcGw5wW7nWt8gc6PpCI= github.com/expr-lang/expr v1.16.9/go.mod h1:8/vRC7+7HBzESEqt5kKpYXxrxkr31SaO8r40VO/1IT4= github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=