Skip to content

Commit

Permalink
make Config.TimeTruncate a functional option
Browse files Browse the repository at this point in the history
  • Loading branch information
methane committed Feb 17, 2024
1 parent 0c5e07e commit 5c9a97b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
33 changes: 29 additions & 4 deletions dsn.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,44 @@ type Config struct {

// private fields. new options should be come here

pubKey *rsa.PublicKey // Server public key
timeTruncate time.Duration // Truncate time.Time values to the specified duration
pubKey *rsa.PublicKey // Server public key
timeTruncate time.Duration // Truncate time.Time values to the specified duration
}

// Functional Options Pattern
// https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis
type option func(*Config) error

// NewConfig creates a new Config and sets default values.
func NewConfig() *Config {
return &Config{
func NewConfig(opts ...option) *Config {
cfg := &Config{
Loc: time.UTC,
MaxAllowedPacket: defaultMaxAllowedPacket,
Logger: defaultLogger,
AllowNativePasswords: true,
CheckConnLiveness: true,
}

cfg.SetOptions(opts...)
return cfg
}

func (c *Config) SetOptions(opts ...option) error {
for _, opt := range opts {
err := opt(c)
if err != nil {
return err
}
}
}

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / lint

missing return (compile)

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / lint

missing return (compile)

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.21, 8.0)

missing return

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.20, 8.1)

missing return

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.21, mariadb-10.6)

missing return

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.21, mariadb-10.4)

missing return

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.21, 5.6)

missing return

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.21, mariadb-10.3)

missing return

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.21, 8.1)

missing return

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.21, mariadb-10.5)

missing return

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.21, mariadb-10.6)

missing return

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.21, mariadb-10.11)

missing return

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.21, 5.7)

missing return

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.21, 8.0)

missing return

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.21, 5.7)

missing return

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.21, 5.6)

missing return

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.21, 8.1)

missing return

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.21, 5.7)

missing return

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.21, 5.6)

missing return

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.21, mariadb-10.4)

missing return

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.21, mariadb-10.3)

missing return

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.21, mariadb-10.5)

missing return

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.21, mariadb-10.3)

missing return

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.21, mariadb-10.6)

missing return

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.21, mariadb-10.11)

missing return

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.21, 8.0)

missing return

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.21, mariadb-10.4)

missing return

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.21, 8.1)

missing return

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / test (windows-latest, 1.21, mariadb-10.11)

missing return

Check failure on line 103 in dsn.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.21, mariadb-10.5)

missing return

// TimeTruncate sets the time duration to truncate time.Time values in
// query parameters.
func TimeTruncate(d time.Duration) option {
return func(cfg *Config) error {
cfg.timeTruncate = d
return nil
}
}

func (cfg *Config) Clone() *Config {
Expand Down
5 changes: 2 additions & 3 deletions result.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ import "database/sql/driver"
// This is accessible by executing statements using sql.Conn.Raw() and
// downcasting the returned result:
//
// res, err := rawConn.Exec(...)
// res.(mysql.Result).AllRowsAffected()
//
// res, err := rawConn.Exec(...)
// res.(mysql.Result).AllRowsAffected()
type Result interface {
driver.Result
// AllRowsAffected returns a slice containing the affected rows for each
Expand Down

0 comments on commit 5c9a97b

Please sign in to comment.