brianvoe / gofakeit

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

Returning errors from gofakeit.Fakeable #268

Closed jackwilsdon closed 9 months ago

jackwilsdon commented 10 months ago

It'd be useful if errors could be returned from fakeables, in the event that they call other functions which may return an error (e.g. faker.Struct to fake a sub-struct).

A new interface could be introduced for this:

type FakeableWithErr interface{
        // Fake returns a fake value for the type.
    Fake(faker *Faker) (interface{}, error)
}

Which could be used in isFakeable and callFake.

Alternatively, callFake could be updated to add special handling for the error type being returned in place of a value.

brianvoe commented 10 months ago

I 100% agree with you. The concern right now is if i added it, it would be a breaking change. I will think about this and see how "harmful" adding it might be.

jackwilsdon commented 10 months ago

I don't think introducing an additional interface would be a breaking change?

brianvoe commented 10 months ago

If you go from

Fake(faker *Faker) interface{}

to

Fake(faker *Faker) (interface{}, error)

Then yes it would technically be a breaking change.

jackwilsdon commented 10 months ago

Does implementing it like this not make it backwards compatible? https://go.dev/play/p/GxOzKQxjO7D

brianvoe commented 10 months ago

I dont really want to implement and maintain both. I might just pull the trigger and change it.

jackwilsdon commented 10 months ago

I'm happy to open a PR for this if you're happy with the linked implemention (i.e. introducing a new interface). It should only be a couple of lines of code in isFakeable and callFake and have a low maintenance burden. It'd be nice to keep backwards compatibility if possible.

brianvoe commented 10 months ago

It really should just have the error in there.