Skip to content

Commit

Permalink
Fix ParseTime and ParseEnv interaction (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
nacho692 committed Jun 3, 2023
1 parent 8cc28fa commit 3f23d7b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
34 changes: 34 additions & 0 deletions issues_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config_test

import (
"os"
"testing"
"time"

Expand Down Expand Up @@ -307,3 +308,36 @@ func TestIssues_114(t *testing.T) {
assert.Eq(t, []string{"val1"}, cc.Value)
// dump.Println(cc)
}

// https://github.com/gookit/config/issues/146
func TestIssues_146(t *testing.T) {
c := config.NewWithOptions("test",
config.ParseDefault,
config.ParseEnv,
config.ParseTime,
)

type conf struct {
Env time.Duration
DefaultEnv time.Duration
NoEnv time.Duration
}

err := os.Setenv("ENV", "5s")
assert.NoError(t, err)

err = c.LoadStrings(config.JSON, `{
"env": "${ENV}",
"defaultEnv": "${DEFAULT_ENV| 10s}",
"noEnv": "15s"
}`)
assert.NoErr(t, err)

var cc conf
err = c.Decode(&cc)
assert.NoErr(t, err)

assert.Eq(t, 5*time.Second, cc.Env)
assert.Eq(t, 10*time.Second, cc.DefaultEnv)
assert.Eq(t, 15*time.Second, cc.NoEnv)
}
6 changes: 3 additions & 3 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ func ValDecodeHookFunc(parseEnv, parseTime bool) mapstructure.DecodeHookFunc {
}

str := data.(string)
if parseEnv {
str = envutil.ParseEnvValue(str)
}
if len(str) < 2 {
return str, nil
}
Expand All @@ -32,10 +35,7 @@ func ValDecodeHookFunc(parseEnv, parseTime bool) mapstructure.DecodeHookFunc {
return dur, nil
}
}
} else if parseEnv { // parse ENV value
str = envutil.ParseEnvValue(str)
}

return str, nil
}
}
Expand Down

0 comments on commit 3f23d7b

Please sign in to comment.