Skip to content

Commit

Permalink
👔 up: enhance support slice with ParseDefault and ParseEnv. fix: #114
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Feb 11, 2023
1 parent 31d3867 commit 08b995e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
25 changes: 25 additions & 0 deletions issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,28 @@ func TestIssues_96(t *testing.T) {
is.NotEmpty(c.Data())
is.Eq([]string{"Test1", "Test2"}, c.Get("parent.child"))
}

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

type conf struct {
Name string `mapstructure:"name" default:"${NAME | Bob}"`
Value []string `mapstructure:"value" default:"${VAL | val1}"`
}

err := c.LoadExists("")
assert.NoErr(t, err)

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

assert.Eq(t, "Bob", cc.Name)
assert.Eq(t, []string{"val1"}, cc.Value)
// dump.Println(cc)
}
10 changes: 7 additions & 3 deletions load.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"flag"
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -42,6 +42,10 @@ func LoadExists(sourceFiles ...string) error { return dc.LoadExists(sourceFiles.
// LoadExists load and parse config files, but will ignore not exists file.
func (c *Config) LoadExists(sourceFiles ...string) (err error) {
for _, file := range sourceFiles {
if file == "" {
continue
}

if err = c.loadFile(file, true, ""); err != nil {
return
}
Expand Down Expand Up @@ -72,7 +76,7 @@ func (c *Config) LoadRemote(format, url string) (err error) {
}

// read response content
bts, err := ioutil.ReadAll(resp.Body)
bts, err := io.ReadAll(resp.Body)
if err == nil {
if err = c.parseSourceCode(format, bts); err != nil {
return
Expand Down Expand Up @@ -386,7 +390,7 @@ func (c *Config) loadFile(file string, loadExist bool, format string) (err error
defer fd.Close()

// read file content
bts, err := ioutil.ReadAll(fd)
bts, err := io.ReadAll(fd)
if err == nil {
// get format for file ext
if format == "" {
Expand Down

0 comments on commit 08b995e

Please sign in to comment.