Skip to content

Commit

Permalink
fix(pgdriver): rename DriverOption to Option
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Oct 12, 2021
1 parent d3a9899 commit 51c1702
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
35 changes: 19 additions & 16 deletions driver/pgdriver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,12 @@ func newDefaultConfig() *Config {
return cfg
}

type DriverOption func(cfg *Config)
type Option func(cfg *Config)

func WithNetwork(network string) DriverOption {
// Deprecated. Use Option instead.
type DriverOption = Option

func WithNetwork(network string) Option {
if network == "" {
panic("network is empty")
}
Expand All @@ -80,7 +83,7 @@ func WithNetwork(network string) DriverOption {
}
}

func WithAddr(addr string) DriverOption {
func WithAddr(addr string) Option {
if addr == "" {
panic("addr is empty")
}
Expand All @@ -89,13 +92,13 @@ func WithAddr(addr string) DriverOption {
}
}

func WithTLSConfig(tlsConfig *tls.Config) DriverOption {
func WithTLSConfig(tlsConfig *tls.Config) Option {
return func(cfg *Config) {
cfg.TLSConfig = tlsConfig
}
}

func WithUser(user string) DriverOption {
func WithUser(user string) Option {
if user == "" {
panic("user is empty")
}
Expand All @@ -104,13 +107,13 @@ func WithUser(user string) DriverOption {
}
}

func WithPassword(password string) DriverOption {
func WithPassword(password string) Option {
return func(cfg *Config) {
cfg.Password = password
}
}

func WithDatabase(database string) DriverOption {
func WithDatabase(database string) Option {
if database == "" {
panic("database is empty")
}
Expand All @@ -119,45 +122,45 @@ func WithDatabase(database string) DriverOption {
}
}

func WithApplicationName(appName string) DriverOption {
func WithApplicationName(appName string) Option {
return func(cfg *Config) {
cfg.AppName = appName
}
}

func WithConnParams(params map[string]interface{}) DriverOption {
func WithConnParams(params map[string]interface{}) Option {
return func(cfg *Config) {
cfg.ConnParams = params
}
}

func WithTimeout(timeout time.Duration) DriverOption {
func WithTimeout(timeout time.Duration) Option {
return func(cfg *Config) {
cfg.DialTimeout = timeout
cfg.ReadTimeout = timeout
cfg.WriteTimeout = timeout
}
}

func WithDialTimeout(dialTimeout time.Duration) DriverOption {
func WithDialTimeout(dialTimeout time.Duration) Option {
return func(cfg *Config) {
cfg.DialTimeout = dialTimeout
}
}

func WithReadTimeout(readTimeout time.Duration) DriverOption {
func WithReadTimeout(readTimeout time.Duration) Option {
return func(cfg *Config) {
cfg.ReadTimeout = readTimeout
}
}

func WithWriteTimeout(writeTimeout time.Duration) DriverOption {
func WithWriteTimeout(writeTimeout time.Duration) Option {
return func(cfg *Config) {
cfg.WriteTimeout = writeTimeout
}
}

func WithDSN(dsn string) DriverOption {
func WithDSN(dsn string) Option {
return func(cfg *Config) {
opts, err := parseDSN(dsn)
if err != nil {
Expand All @@ -178,7 +181,7 @@ func env(key, defValue string) string {

//------------------------------------------------------------------------------

func parseDSN(dsn string) ([]DriverOption, error) {
func parseDSN(dsn string) ([]Option, error) {
u, err := url.Parse(dsn)
if err != nil {
return nil, err
Expand All @@ -188,7 +191,7 @@ func parseDSN(dsn string) ([]DriverOption, error) {
return nil, errors.New("pgdriver: invalid scheme: " + u.Scheme)
}

var opts []DriverOption
var opts []Option

if u.Host != "" {
addr := u.Host
Expand Down
2 changes: 1 addition & 1 deletion driver/pgdriver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type Connector struct {
cfg *Config
}

func NewConnector(opts ...DriverOption) *Connector {
func NewConnector(opts ...Option) *Connector {
c := &Connector{cfg: newDefaultConfig()}
for _, opt := range opts {
opt(c.cfg)
Expand Down

0 comments on commit 51c1702

Please sign in to comment.