-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Make TimeTruncate functional option #1552
Changes from 5 commits
0c5e07e
5c9a97b
f4c212e
5f2654f
e42533e
86075b6
5b603c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,8 @@ var ( | |
// If a new Config is created instead of being parsed from a DSN string, | ||
// the NewConfig function should be used, which sets default values. | ||
type Config struct { | ||
// non boolean fields | ||
|
||
User string // Username | ||
Passwd string // Password (requires User) | ||
Net string // Network (e.g. "tcp", "tcp6", "unix". default: "tcp") | ||
|
@@ -45,15 +47,15 @@ type Config struct { | |
Loc *time.Location // Location for time.Time values | ||
MaxAllowedPacket int // Max packet size allowed | ||
ServerPubKey string // Server public key name | ||
pubKey *rsa.PublicKey // Server public key | ||
TLSConfig string // TLS configuration name | ||
TLS *tls.Config // TLS configuration, its priority is higher than TLSConfig | ||
TimeTruncate time.Duration // Truncate time.Time values to the specified duration | ||
Timeout time.Duration // Dial timeout | ||
ReadTimeout time.Duration // I/O read timeout | ||
WriteTimeout time.Duration // I/O write timeout | ||
Logger Logger // Logger | ||
|
||
// boolean fields | ||
|
||
AllowAllFiles bool // Allow all files to be used with LOAD DATA LOCAL INFILE | ||
AllowCleartextPasswords bool // Allows the cleartext client side plugin | ||
AllowFallbackToPlaintext bool // Allows fallback to unencrypted connection if server does not support TLS | ||
|
@@ -66,17 +68,47 @@ type Config struct { | |
MultiStatements bool // Allow multiple statements in one query | ||
ParseTime bool // Parse time values to time.Time | ||
RejectReadOnly bool // Reject read-only connections | ||
|
||
// 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 | ||
} | ||
|
||
// 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{ | ||
cfg := &Config{ | ||
Loc: time.UTC, | ||
MaxAllowedPacket: defaultMaxAllowedPacket, | ||
Logger: defaultLogger, | ||
AllowNativePasswords: true, | ||
CheckConnLiveness: true, | ||
} | ||
|
||
return cfg | ||
} | ||
|
||
func (c *Config) Apply(opts ...Option) error { | ||
methane marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for _, opt := range opts { | ||
err := opt(c) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// 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 | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The introduction of the Would you like me to help with generating tests for the |
||
} | ||
|
||
func (cfg *Config) Clone() *Config { | ||
|
@@ -263,8 +295,8 @@ func (cfg *Config) FormatDSN() string { | |
writeDSNParam(&buf, &hasParam, "parseTime", "true") | ||
} | ||
|
||
if cfg.TimeTruncate > 0 { | ||
writeDSNParam(&buf, &hasParam, "timeTruncate", cfg.TimeTruncate.String()) | ||
if cfg.timeTruncate > 0 { | ||
writeDSNParam(&buf, &hasParam, "timeTruncate", cfg.timeTruncate.String()) | ||
} | ||
|
||
if cfg.ReadTimeout > 0 { | ||
|
@@ -509,9 +541,9 @@ func parseDSNParams(cfg *Config, params string) (err error) { | |
|
||
// time.Time truncation | ||
case "timeTruncate": | ||
cfg.TimeTruncate, err = time.ParseDuration(value) | ||
cfg.timeTruncate, err = time.ParseDuration(value) | ||
methane marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return | ||
return fmt.Errorf("invalid timeTruncate value: %v, error: %w", value, err) | ||
} | ||
|
||
// I/O read Timeout | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private -> unexpected - let's use Go naming