Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a way to add faker configuration in options #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion fabricator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sync"

"github.com/go-faker/faker/v4"
"github.com/go-faker/faker/v4/pkg/options"
)

type PersistenceHandler[T any] interface {
Expand All @@ -16,13 +17,15 @@ type PersistenceHandler[T any] interface {
type Options[T any] struct {
PersistenceHandler PersistenceHandler[T]
Defaults map[string]any
FakerOptions []options.OptionFunc
}

type Factory[T any] struct {
mutex sync.Mutex
model T
persistenceHandler PersistenceHandler[T]
defaults map[string]any
fakerOpts []options.OptionFunc
counter int
}

Expand All @@ -37,16 +40,19 @@ func New[T any](model T, opts ...Options[T]) *Factory[T] {

var defaults map[string]any
var handler PersistenceHandler[T]
var fakerOpts []options.OptionFunc

if len(opts) > 0 {
defaults = opts[0].Defaults
handler = opts[0].PersistenceHandler
fakerOpts = opts[0].FakerOptions
}

factory := Factory[T]{
model: model,
defaults: defaults,
persistenceHandler: handler,
fakerOpts: fakerOpts,
}

return &factory
Expand Down Expand Up @@ -85,7 +91,7 @@ func (factory *Factory[T]) SetCounter(value int) {
func (factory *Factory[T]) Build(overrides ...map[string]any) T {
modelType := reflect.TypeOf(factory.model)
model := reflect.Zero(modelType).Interface().(T)
if fakerErr := faker.FakeData(&model); fakerErr != nil {
if fakerErr := faker.FakeData(&model, factory.fakerOpts...); fakerErr != nil {
panic(fmt.Errorf("error generating fake data: %w", fakerErr).Error())
}
for key, value := range factory.defaults {
Expand Down