Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug that can't load default values for slice element #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 53 additions & 0 deletions configor_test.go
Expand Up @@ -681,3 +681,56 @@ func TestLoadNestedConfig(t *testing.T) {
adminConfig := MenuList{}
New(&Config{Verbose: true}).Load(&adminConfig, "admin.yml")
}

type SliceDefaultValueConfig struct {
Options []struct {
Name string `yaml:"name" default:"xylonx" required:"true"`
Number int `yaml:"number" default:"12" required:"true"`
} `yaml:"options"`
}

func TestSliceDefaultValue(t *testing.T) {
file, err := ioutil.TempFile("/tmp", "configor-test-default-value-for-slice")
if err != nil {
t.Fatal(err)
}

config := new(SliceDefaultValueConfig)
err = Load(config, file.Name())
if err != nil {
t.Logf("load config from yaml error: %v", err)
}

rightConfig := SliceDefaultValueConfig{
Options: []struct {
Name string `yaml:"name" default:"xylonx" required:"true"`
Number int `yaml:"number" default:"12" required:"true"`
}{
{
Name: "alice",
Number: 1,
},
{
Name: "bob",
Number: 12,
},
{
Name: "xylonx",
Number: 3,
},
},
}

if !reflect.DeepEqual(rightConfig, config) {
t.Logf("load value: \n%v\nbut the right value is \n%v", *config, rightConfig)
t.Failed()
}
}

var sliceDefaultValueConfig = `
options:
- name: alice
number: 1
- name: bob
- number: 3
`
6 changes: 3 additions & 3 deletions utils.go
Expand Up @@ -371,9 +371,6 @@ func (configor *Configor) load(config interface{}, watchMode bool, files ...stri
}
}

// process defaults
configor.processDefaults(config)

for _, file := range configFiles {
if configor.Config.Debug || configor.Config.Verbose {
fmt.Printf("Loading configurations from file '%v'...\n", file)
Expand All @@ -384,6 +381,9 @@ func (configor *Configor) load(config interface{}, watchMode bool, files ...stri
}
configor.configModTimes = configModTimeMap

// process defaults
configor.processDefaults(config)

if prefix := configor.getENVPrefix(config); prefix == "-" {
err = configor.processTags(config)
} else {
Expand Down