From 8667b6c2ebd7553d3d55e9270a793a519a8419fd Mon Sep 17 00:00:00 2001 From: Julien Schmidt Date: Sat, 7 Oct 2017 00:56:55 +0200 Subject: [PATCH 1/4] dsn: export NewConfig --- dsn.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/dsn.go b/dsn.go index 6ce5cc020..9e8deab60 100644 --- a/dsn.go +++ b/dsn.go @@ -57,6 +57,13 @@ type Config struct { RejectReadOnly bool // Reject read-only connections } +func NewConfig() *Config { + return &Config{ + Collation: defaultCollation, + Loc: time.UTC, + AllowNativePasswords: true, + } +} // FormatDSN formats the given Config into a DSN string which can be passed to // the driver. func (cfg *Config) FormatDSN() string { @@ -273,11 +280,7 @@ func (cfg *Config) FormatDSN() string { // ParseDSN parses the DSN string to a Config func ParseDSN(dsn string) (cfg *Config, err error) { // New config with some default values - cfg = &Config{ - Loc: time.UTC, - Collation: defaultCollation, - AllowNativePasswords: true, - } + cfg = NewConfig() // [user[:password]@][net[(addr)]]/dbname[?param1=value1¶mN=valueN] // Find the last '/' (since the password or the net addr might contain a '/') From 4c4549192a6923c5a3dfab89ae6f80fecd82fde6 Mon Sep 17 00:00:00 2001 From: Julien Schmidt Date: Sat, 7 Oct 2017 00:57:32 +0200 Subject: [PATCH 2/4] dsn: move DSN normalization to separate func --- dsn.go | 55 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/dsn.go b/dsn.go index 9e8deab60..8b42fd57a 100644 --- a/dsn.go +++ b/dsn.go @@ -64,6 +64,35 @@ func NewConfig() *Config { AllowNativePasswords: true, } } + +func (cfg *Config) normalize() error { + if cfg.InterpolateParams && unsafeCollations[cfg.Collation] { + return errInvalidDSNUnsafeCollation + } + + // Set default network if empty + if cfg.Net == "" { + cfg.Net = "tcp" + } + + // Set default address if empty + if cfg.Addr == "" { + switch cfg.Net { + case "tcp": + cfg.Addr = "127.0.0.1:3306" + case "unix": + cfg.Addr = "/tmp/mysql.sock" + default: + errors.New("default addr for network '" + cfg.Net + "' unknown") + } + + } else if cfg.Net == "tcp" { + cfg.Addr = ensureHavePort(cfg.Addr) + } + + return nil +} + // FormatDSN formats the given Config into a DSN string which can be passed to // the driver. func (cfg *Config) FormatDSN() string { @@ -348,31 +377,9 @@ func ParseDSN(dsn string) (cfg *Config, err error) { return nil, errInvalidDSNNoSlash } - if cfg.InterpolateParams && unsafeCollations[cfg.Collation] { - return nil, errInvalidDSNUnsafeCollation - } - - // Set default network if empty - if cfg.Net == "" { - cfg.Net = "tcp" - } - - // Set default address if empty - if cfg.Addr == "" { - switch cfg.Net { - case "tcp": - cfg.Addr = "127.0.0.1:3306" - case "unix": - cfg.Addr = "/tmp/mysql.sock" - default: - return nil, errors.New("default addr for network '" + cfg.Net + "' unknown") - } - + if err = cfg.normalize(); err != nil { + return nil, err } - if cfg.Net == "tcp" { - cfg.Addr = ensureHavePort(cfg.Addr) - } - return } From a7e960dd9acc67462c5a8f9add02b6e6428da967 Mon Sep 17 00:00:00 2001 From: Julien Schmidt Date: Sat, 7 Oct 2017 01:06:00 +0200 Subject: [PATCH 3/4] dsn: add godoc --- dsn.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dsn.go b/dsn.go index 8b42fd57a..be17c455d 100644 --- a/dsn.go +++ b/dsn.go @@ -28,7 +28,9 @@ var ( errInvalidDSNUnsafeCollation = errors.New("invalid DSN: interpolateParams can not be used with unsafe collations") ) -// Config is a configuration parsed from a DSN string +// Config is a configuration parsed from a DSN string. +// 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 { User string // Username Passwd string // Password (requires User) @@ -57,6 +59,7 @@ type Config struct { RejectReadOnly bool // Reject read-only connections } +// NewConfig creates a new Config and sets default values. func NewConfig() *Config { return &Config{ Collation: defaultCollation, From 8d7c8b2e0fd06cbab6bae3d4d2e86fc1f00d284f Mon Sep 17 00:00:00 2001 From: Julien Schmidt Date: Sat, 7 Oct 2017 01:43:51 +0200 Subject: [PATCH 4/4] dsn: add missing return --- dsn.go | 2 +- dsn_test.go | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/dsn.go b/dsn.go index be17c455d..af3dfa303 100644 --- a/dsn.go +++ b/dsn.go @@ -86,7 +86,7 @@ func (cfg *Config) normalize() error { case "unix": cfg.Addr = "/tmp/mysql.sock" default: - errors.New("default addr for network '" + cfg.Net + "' unknown") + return errors.New("default addr for network '" + cfg.Net + "' unknown") } } else if cfg.Net == "tcp" { diff --git a/dsn_test.go b/dsn_test.go index 01b57212f..af28da351 100644 --- a/dsn_test.go +++ b/dsn_test.go @@ -98,6 +98,7 @@ func TestDSNParserInvalid(t *testing.T) { "(/", // no closing brace "net(addr)//", // unescaped "User:pass@tcp(1.2.3.4:3306)", // no trailing slash + "net()/", // unknown default addr //"/dbname?arg=/some/unescaped/path", }