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 values for a field within an array doesn't work #354

Open
JivusAyrus opened this issue Dec 23, 2024 · 0 comments
Open

Default values for a field within an array doesn't work #354

JivusAyrus opened this issue Dec 23, 2024 · 0 comments

Comments

@JivusAyrus
Copy link

JivusAyrus commented Dec 23, 2024

Version: v11.1.0

The default values for attributes within an array of objects are not being applied as expected.

Reproduction Steps

  1. Define a field with type as an array of struct, setting default values for some fields in that struct using the envDefault tag.
  2. Parse the configuration using env.Parse.
type TestMetricsOTLP struct {
	RouterRuntime bool                      `yaml:"router_runtime" envDefault:"true" env:"METRICS_OTLP_ROUTER_RUNTIME"`
	Exporters     []TestMetricsOTLPExporter `yaml:"exporters"`
}

type TestMetricsOTLPExporter struct {
	Value           string `yaml:"value"`
	Exporter    string `yaml:"exporter" envDefault:"http"`
	Temporality string `yaml:"temporality" envDefault:"cumulative"`
}

func Parse() {
	config := TestMetricsOTLP{
		Exporters: []TestMetricsOTLPExporter{
			{Value: "A"},
		},
	}

	err := env.Parse(&config)
         if err != nil {
		fmt.Printf("Failed to parse: %v", err)
	}
}

func main() {
	Parse()
}

Expected Behavior

The envDefault values for Temporality and Exporter should be applied.

  • config.Exporters[0].Temporality should equal "cumulative".
  • config.Exporters[0].Exporter should equal "http".

Actual Behavior

Both Temporality and Exporter are equal to "".

Would a pull request for the fix be accepted?

Thanks

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

1 participant