Skip to content

Reflection misses circular-referencing #891

@thanh-nguyencong

Description

@thanh-nguyencong

Hi Quii,
While learning on the site, I noticed that the implementation of Walk would cause fatal error: stack overflow with this test

func TestWalk(t *testing.T) {
	t.Run("walk with circular reference", func(t *testing.T) {
		type Person struct {
			Name    string
			Age     int
			Address string
			Fren   *Person
		}
		person := Person{
			Name:    "Alice",
			Age:     30,
			Address: "123 Main St",
		}

		person.Fren = &person

		got := make([]string, 0)
		Walk(person, func(s string) {
			got = append(got, s)
		})

		expected := []string{"Alice", "123 Main St"}
		if !reflect.DeepEqual(got, expected) {
			t.Errorf("Walk() = %v; want %v", got, expected)
		}
	})
}

where person.Fren refers to person itself (person is a go developer).
We should be able to solve it with a visitor implementation but I would like to know the preferable behavior before submitting a PR.
Thanks.

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