Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

feat: FakeData support recursive type by using opt #168

Merged
merged 11 commits into from
Aug 17, 2022
Merged

feat: FakeData support recursive type by using opt #168

merged 11 commits into from
Aug 17, 2022

Conversation

wolf-joe
Copy link
Contributor

original FakeData() will cause stack overflow when it handling recursive type:

type BinaryTreeNode struct {
	Val   int
	Left  *BinaryTreeNode
	Right *BinaryTreeNode
}

func TestFakeData_RecursiveType(t *testing.T) {
	toJSON := func(val interface{}) string {
		data, _ := json.MarshalIndent(val, "", "  ")
		return string(data)
	}
	node1 := BinaryTreeNode{}
	if err := FakeData(&node1); err != nil {
		t.Errorf("%+v", err)
		t.FailNow()
	}
	t.Log("binary tree node:", toJSON(node1))
}
runtime: goroutine stack exceeds 1000000000-byte limit
...
fatal error: stack overflow

advance usage:

func TestFakeData_RecursiveType(t *testing.T) {
	toJSON := func(val interface{}) string {
		data, _ := json.MarshalIndent(val, "", "  ")
		return string(data)
	}
	node1 := BinaryTreeNode{}
	if err := FakeData(&node1, WithRecursionMaxDepth(2)); err != nil {
		t.Errorf("%+v", err)
		t.FailNow()
	}
	t.Log("binary tree node:", toJSON(node1))
}

@bxcodec
Copy link
Owner

bxcodec commented Aug 14, 2022

Hi @wolf-joe

I merged #160, can you help to fix the conflict?

@wolf-joe
Copy link
Contributor Author

Hi @wolf-joe

I merged #160, can you help to fix the conflict?

done

Copy link
Owner

@bxcodec bxcodec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@bxcodec bxcodec merged commit 6a1323d into bxcodec:master Aug 17, 2022
@bxcodec
Copy link
Owner

bxcodec commented Aug 17, 2022

Hi @wolf-joe, thanks for your PR. I'm planning to release a new major version with this option.
the PR can be seen here #169
if you have time, can you help to review? I really appreciate your help

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants