stephenafamo / bob

SQL query builder and ORM/Factory generator for Go with support for PostgreSQL, MySQL and SQLite
https://bob.stephenafamo.com
MIT License
794 stars 41 forks source link

Fix panic for generated factories. Affecting all nullable columns #179

Closed jacobmolby closed 9 months ago

jacobmolby commented 9 months ago

If you have a null column, the code generated in the factories, will cause a panic:

// in models/factory/users.go

func ensureCreatableUser(m *models.UserSetter) {
    if m.Name.IsUnset() {
        m.Name = omitnull.FromNull(randomNull[string](nil))
    }
    if m.Email.IsUnset() {
        m.Email = omit.From(random[string](nil))
    }
    if m.Password.IsUnset() {
        m.Password = omit.From(random[string](nil))
// etc...

Since it will call randomNull without a faker instance it will panic.

// randomNull is like [Random], but for null types
// it will often also generate a null value
func randomNull[T any](f *faker.Faker) null.Val[T] {
    return null.FromCond(random[T](f), f.BoolWithChance(50)) // panic when f.BoolWithChance(50) is called
}

The code was generated with go run github.com/stephenafamo/bob/gen/bobgen-mysql@v0.25.0

stephenafamo commented 9 months ago

Thank you 🙏🏾