Skip to content

Commit

Permalink
use functional Options Pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
m4ex committed Sep 24, 2024
1 parent d0b9534 commit 7f90f4c
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions dialect/mysqldialect/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func init() {
}
}

type DialectOption func(d *Dialect)

type Dialect struct {
schema.BaseDialect

Expand All @@ -35,7 +37,7 @@ type Dialect struct {
loc *time.Location
}

func New() *Dialect {
func New(opts ...DialectOption) *Dialect {
d := new(Dialect)
d.tables = schema.NewTables(d)
d.features = feature.AutoIncrement |
Expand All @@ -48,16 +50,22 @@ func New() *Dialect {
feature.InsertOnDuplicateKey |
feature.SelectExists |
feature.CompositeIn

for _, opt := range opts {
opt(d)
}

return d
}

func (d *Dialect) WithTimeLocation(loc string) *Dialect {
location, err := time.LoadLocation(loc)
if err != nil {
panic(fmt.Errorf("mysqldialect can't load provided location %s: %s", loc, err))
func WithTimeLocation(loc string) DialectOption {
return func(d *Dialect) {
location, err := time.LoadLocation(loc)
if err != nil {
panic(fmt.Errorf("mysqldialect can't load provided location %s: %s", loc, err))
}
d.loc = location
}
d.loc = location
return d
}

func (d *Dialect) Init(db *sql.DB) {
Expand Down

0 comments on commit 7f90f4c

Please sign in to comment.