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 on Nested Structs #49

Open
hundredwatt opened this issue Jun 12, 2020 · 6 comments
Open

Default Values on Nested Structs #49

hundredwatt opened this issue Jun 12, 2020 · 6 comments
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed

Comments

@hundredwatt
Copy link

I am attempting to use cleanenv with a nested struct and the env-default tag is not being applied on the child struct.

Is this something cleanenv could support?

Here's a test case replicating the issue:

func TestNestedStructsParseFileEnv(t *testing.T) {
	type child struct {
		Number        int64 `yaml:"number"`
		DefaultNumber int64 `yaml:"default_number" env-defult:"-1"`
	}

	type parent struct {
		Children map[string]child `yaml:"children"`
	}

	children := make(map[string]child)
	children["first"] = child{1, -1}
	wantConfig := parent{children}

	tests := []struct {
		name    string
		file    string
		ext     string
		want    *parent
		wantErr bool
	}{
		{
			name: "yaml",
			file: `
children: 
  first:
    number: 1`,
			ext:     "yaml",
			want:    &wantConfig,
			wantErr: false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			tmpFile, err := ioutil.TempFile(os.TempDir(), fmt.Sprintf("*.%s", tt.ext))
			if err != nil {
				t.Fatal("cannot create temporary file:", err)
			}
			defer os.Remove(tmpFile.Name())

			text := []byte(tt.file)
			if _, err = tmpFile.Write(text); err != nil {
				t.Fatal("failed to write to temporary file:", err)
			}

			var prnt parent
			if err = ReadConfig(tmpFile.Name(), &prnt); (err != nil) != tt.wantErr {
				t.Errorf("wrong error behavior %v, wantErr %v", err, tt.wantErr)
			}
			if err == nil && !reflect.DeepEqual(&prnt, tt.want) {
				t.Errorf("wrong data %v, want %v", &prnt, tt.want)
			}
		})
	}

}
@ilyakaznacheev ilyakaznacheev added the bug Something isn't working label Jun 14, 2020
@ilyakaznacheev
Copy link
Owner

It seems that the default processing logic for nested maps has not yet been implemented. Thank you for finding this error.

@ilyakaznacheev ilyakaznacheev added the help wanted Extra attention is needed label Jul 6, 2020
@abakermi
Copy link

@ilyakaznacheev do u have time to implement this or you can assign me

@ilyakaznacheev
Copy link
Owner

@abakermi go ahead, if you have ideas how to fix that, please send a PR :)

@abakermi
Copy link

sure, thanks !

@ilyakaznacheev
Copy link
Owner

Unassigned due to inactivity

@ilyakaznacheev ilyakaznacheev added the good first issue Good for newcomers label Oct 19, 2020
@Dombo
Copy link

Dombo commented Jan 25, 2021

This applies for all tags as far as I can tell, for example env-required does not validate on nested structs either. Logical but worth pointing out I felt, love the library by the way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants