Add region (state) to address

This commit is contained in:
Eli Ribble 2026-03-03 20:27:12 +00:00
parent 0ff493cd53
commit 6aa7fa60b4
No known key found for this signature in database
6 changed files with 97 additions and 9 deletions

View file

@ -46,6 +46,7 @@ type AddressTemplate struct {
PostalCode func() string
Street func() string
Unit func() string
Region func() string
r addressR
f *Factory
@ -160,6 +161,10 @@ func (o AddressTemplate) BuildSetter() *models.AddressSetter {
val := o.Unit()
m.Unit = omit.From(val)
}
if o.Region != nil {
val := o.Region()
m.Region = omit.From(val)
}
return m
}
@ -212,6 +217,9 @@ func (o AddressTemplate) Build() *models.Address {
if o.Unit != nil {
m.Unit = o.Unit()
}
if o.Region != nil {
m.Region = o.Region()
}
o.setModelRels(m)
@ -268,6 +276,10 @@ func ensureCreatableAddress(m *models.AddressSetter) {
val := random_string(nil)
m.Unit = omit.From(val)
}
if !(m.Region.IsValue()) {
val := random_string(nil)
m.Region = omit.From(val)
}
}
// insertOptRels creates and inserts any optional the relationships on *models.Address
@ -437,6 +449,7 @@ func (m addressMods) RandomizeAllColumns(f *faker.Faker) AddressMod {
AddressMods.RandomPostalCode(f),
AddressMods.RandomStreet(f),
AddressMods.RandomUnit(f),
AddressMods.RandomRegion(f),
}
}
@ -750,6 +763,37 @@ func (m addressMods) RandomUnit(f *faker.Faker) AddressMod {
})
}
// Set the model columns to this value
func (m addressMods) Region(val string) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Region = func() string { return val }
})
}
// Set the Column from the function
func (m addressMods) RegionFunc(f func() string) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Region = f
})
}
// Clear any values for the column
func (m addressMods) UnsetRegion() AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Region = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m addressMods) RandomRegion(f *faker.Faker) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Region = func() string {
return random_string(f)
}
})
}
func (m addressMods) WithParentsCascading() AddressMod {
return AddressModFunc(func(ctx context.Context, o *AddressTemplate) {
if isDone, _ := addressWithParentsCascadingCtx.Value(ctx); isDone {

View file

@ -146,6 +146,7 @@ func (f *Factory) FromExistingAddress(m *models.Address) *AddressTemplate {
o.PostalCode = func() string { return m.PostalCode }
o.Street = func() string { return m.Street }
o.Unit = func() string { return m.Unit }
o.Region = func() string { return m.Region }
ctx := context.Background()
if len(m.R.Mailers) > 0 {