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

default not supported in slice and map #67

Open
hulucc opened this issue May 26, 2021 · 1 comment
Open

default not supported in slice and map #67

hulucc opened this issue May 26, 2021 · 1 comment
Assignees

Comments

@hulucc
Copy link

hulucc commented May 26, 2021

Reproducible Example

type Config struct {
	Foo struct {
		Servers []struct {
			Name string
			Port int
			Host string `default:"0.0.0.0"`
		}
		Testing map[string]struct {
			Name string `default:"default"`
		}
	}
}

foo:
  servers:
    - name: dev
      port: 4000
    - name: alpha
      port: 5000
  testing:
    data1:

Description

The field in slice and map won't get default value when empty.

@moycat
Copy link

moycat commented Jul 15, 2021

The default values are applied before the config is loaded. So the later loaded structs in the slice don't have default values.

configor/utils.go

Lines 375 to 391 in 1095c48

configor.processDefaults(config)
for _, file := range configFiles {
if configor.Config.Debug || configor.Config.Verbose {
fmt.Printf("Loading configurations from file '%v'...\n", file)
}
if err = processFile(config, file, configor.GetErrorOnUnmatchedKeys()); err != nil {
return err, true
}
}
configor.configModTimes = configModTimeMap
if prefix := configor.getENVPrefix(config); prefix == "-" {
err = configor.processTags(config)
} else {
err = configor.processTags(config, prefix)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants