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

nil slices, maps, and pointers are initialized to non-nil values #5

Closed
ansel1 opened this issue Feb 11, 2018 · 1 comment · Fixed by #6
Closed

nil slices, maps, and pointers are initialized to non-nil values #5

ansel1 opened this issue Feb 11, 2018 · 1 comment · Fixed by #6

Comments

@ansel1
Copy link

ansel1 commented Feb 11, 2018

If a struct has map, slice, or pointer fields which have an initial value of nil, Set() will set them to empty (zero) values, even if the field has no "default" tag. See this test case:

type TT struct{}

type SS struct {
	Ptr *TT
	Slice []string
	Map map[string]interface{}
}

func TestEmptySlice(t *testing.T) {
	s := SS{}
	err := Set(&s)
	if err != nil {
		t.Fatalf("Set() returned error: %#v", err)
	}

	if s.Ptr != nil {
		t.Error("Set() initialized nil pointer")
	}

	if s.Slice != nil {
		t.Error("Set() initialized nil slice")
	}

	if s.Map != nil {
		t.Error("Set() initialized nil map")
	}
}

Seems like, if the field has no "default" tag, and there is nothing to recurse into (because the value is nil), it should be left unchanged.

I think this was a side effect of the last change for recursing into nested values with no "default" tag.

@creasty
Copy link
Owner

creasty commented Feb 17, 2018

Thanks again.
Addressed in #6 and released https://github.com/creasty/defaults/releases/tag/v1.2.0.

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

Successfully merging a pull request may close this issue.

2 participants