From 7f90f4c50783af94985cffc32685dea829bf46b9 Mon Sep 17 00:00:00 2001 From: m4ex <4exmail@gmail.com> Date: Wed, 25 Sep 2024 00:50:32 +0300 Subject: [PATCH] use functional Options Pattern --- dialect/mysqldialect/dialect.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/dialect/mysqldialect/dialect.go b/dialect/mysqldialect/dialect.go index 959df66ae..881aa7ebf 100644 --- a/dialect/mysqldialect/dialect.go +++ b/dialect/mysqldialect/dialect.go @@ -27,6 +27,8 @@ func init() { } } +type DialectOption func(d *Dialect) + type Dialect struct { schema.BaseDialect @@ -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 | @@ -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) {