go-faker / faker

Go (Golang) Fake Data Generator for Struct, previously https://github.com/bxcodec/faker
https://pkg.go.dev/github.com/go-faker/faker/v4
MIT License
614 stars 30 forks source link

panic when field of type map[string]any is in the struct #30

Closed lev112 closed 9 months ago

lev112 commented 1 year ago

given this struct

type MyStruct struct {
    A string
    B map[string]any
}

the following code will fail

var base MyStruct
err := faker.FakeData(&base) 

with an error:

interface{} not allowed

(this is how I would expect it to behave)

if I try to ignore the fields that include an interface:

var base MyStruct
err := faker.FakeData(&base,
    options.WithIgnoreInterface(true),
)

I would expect the faking to work, and leave the B field empty, but the code will panic with

panic: runtime error: invalid memory address or nil pointer dereference


adding

options.WithFieldsToIgnore("B")

will solve that problem, but the panic is very confusing, and there is no hint of what is the problem

gegaryfa commented 11 months ago

Hi @lev112 , which version are you using? I have a similar issue after upgrading to 4.1.1 which includes the changes of this PR #25. Up to 4.0.0 works fine without panic

lev112 commented 11 months ago

@gegaryfa I'm using the latest (4.1.1)

gegaryfa commented 10 months ago

@bxcodec is this intended behaviour?

bxcodec commented 9 months ago

Weird, I will try to debug. Sorry for the slow/late response, I was packed for the past weeks. Hope I can solve it before the end of the week

bxcodec commented 9 months ago

Hi @lev112 and @gegaryfa I fixed it in #36 Will release a new version soon, please help to check again with your use case, thanks. *feel free to re-open this issue if it's still happening