Skip to content

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

@ansel1

Description

@ansel1

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions