brianvoe / gofakeit

Random fake data generator written in go
MIT License
4.49k stars 263 forks source link

Stackoverflow on cyclicly linked structures #351

Closed mark-ross closed 6 months ago

mark-ross commented 6 months ago

Hello! I love this tool, but recently encountered an issue that I was hoping to discuss.

Caveat: I've only used this package at surface level, but have not traversed through the codebase, so my suggestions and thoughts might be naive.

In a codebase I work with, I have structs which have a cyclic reference from some Gorm definitions. It's a many-to-many case where each of the structs have slices of the other struct. This is leading to a stack overflow when trying to generate the data directly from either of the structures.

Example:

type Blog struct {
  // ...
  Tags []*Tag `gorm:"many-to-many"`
}

type Tag struct {
  // ...
  Blogs []*Blog `gorm:"many-to-many"`
}

When calling gofakeit.Struct(&blog) that triggers the stack overflow since the fake generator goes through each structure, which populates the other, which populates the other, etc. To further throw a wrench in my problem, I have to consider that the field tags cannot be modified on the base structure.

So my initial thought was if we could add an optional argument into the gofakeit.Struct function wherein we could specify a certain recursion depth or whether or not to populate any/all structs that are in the given structure. Thoughts?

Thanks for your time!

brianvoe commented 6 months ago

So your issue I dont think would even be solved even if you put a limit of 1 on it cause as soon as you run the one it calls the other once and keeps cyclicly calling just once. I wouldnt use Gofakeit for a struct like that. Just fill it up yourself with what you want. Not everything needs to be solved in a one liner.