brianvoe / gofakeit

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

Avoid circular dependencies without changing struct definition (for generated proto types)? #281

Closed MichaelSnowden closed 8 months ago

MichaelSnowden commented 8 months ago

We have a self-referential Failure type defined here. The actual struct is more complicated, but it looks like the one in the below script:

package main

import (
    "github.com/brianvoe/gofakeit/v6"
)

// Failure is a generated struct from a proto definition.
type Failure struct {
    Cause *Failure // Can't add fakeit ignore tag here because this is a generated field.
}

func main() {
    var obj Failure
    _ = gofakeit.Struct(&obj) // stack overflow
}

This will cause a stack overflow due to the circular dependency. However, I can't easily ignore the field because I can't edit the struct definitions since they are generated. There may be some ways, but I'd rather avoid it if possible since it will likely add a new step to our proto generation process.

Is there a way I can state during the invocation of gofakeit.Struct that this specific field should be ignored so that I don't need to modify the struct definition itself?

Thank you!

brianvoe commented 8 months ago

Nope. Looks like you should probably rethink the need for a circular struct.