Skip to content

Commit

Permalink
fix: barry 2024-12-21 20:19:30
Browse files Browse the repository at this point in the history
  • Loading branch information
kooksee committed Dec 21, 2024
1 parent b78c008 commit 0e83e9c
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
25 changes: 23 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"reflect"
"sort"
"strings"

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -149,5 +170,5 @@ func Load[T any]() T {

var cfg T
LoadFromPath(&cfg, configPath)
return cfg
return Cfg[T]{T: cfg, P: &cfg}
}
2 changes: 1 addition & 1 deletion config/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 "", ""
}
Expand Down
14 changes: 12 additions & 2 deletions env/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion env/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 0e83e9c

Please sign in to comment.