faker icon indicating copy to clipboard operation
faker copied to clipboard

feat: FakeData support recursive type

Open wolf-joe opened this issue 2 years ago • 1 comments

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

wolf-joe avatar Aug 10 '22 14:08 wolf-joe

Hi @wolf-joe

Can we utilize this proposal #160? I'm thinking to re-use the capability to support this -- re: options

Let me know if it's possible, so I can merge that PR cc @unkeep to assist

bxcodec avatar Aug 11 '22 15:08 bxcodec

Hi @wolf-joe

Can we utilize this proposal #160? I'm thinking to re-use the capability to support this -- re: options

Let me know if it's possible, so I can merge that PR cc @unkeep to assist

you mean we can set recursionMaxDepth by option?

wolf-joe avatar Aug 13 '22 02:08 wolf-joe

Actually, this is a critical, can not be an option

bxcodec avatar Aug 13 '22 02:08 bxcodec

But I'm curious if it's possible to set the recursionMaxDepth by using option?

bxcodec avatar Aug 13 '22 02:08 bxcodec

custom recursionMaxDepth is attractive idea for me, const value cannot handle every case. I will try another version base #160 this weekend, you can merge #160 if you want.

wolf-joe avatar Aug 13 '22 05:08 wolf-joe

closing this because of this #168

bxcodec avatar Aug 14 '22 08:08 bxcodec