|
28 | 28 | errInvalidDSNUnsafeCollation = errors.New("invalid DSN: interpolateParams can not be used with unsafe collations")
|
29 | 29 | )
|
30 | 30 |
|
31 |
| -// Config is a configuration parsed from a DSN string |
| 31 | +// Config is a configuration parsed from a DSN string. |
| 32 | +// If a new Config is created instead of being parsed from a DSN string, |
| 33 | +// the NewConfig function should be used, which sets default values. |
32 | 34 | type Config struct {
|
33 | 35 | User string // Username
|
34 | 36 | Passwd string // Password (requires User)
|
@@ -57,6 +59,43 @@ type Config struct {
|
57 | 59 | RejectReadOnly bool // Reject read-only connections
|
58 | 60 | }
|
59 | 61 |
|
| 62 | +// NewConfig creates a new Config and sets default values. |
| 63 | +func NewConfig() *Config { |
| 64 | + return &Config{ |
| 65 | + Collation: defaultCollation, |
| 66 | + Loc: time.UTC, |
| 67 | + AllowNativePasswords: true, |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func (cfg *Config) normalize() error { |
| 72 | + if cfg.InterpolateParams && unsafeCollations[cfg.Collation] { |
| 73 | + return errInvalidDSNUnsafeCollation |
| 74 | + } |
| 75 | + |
| 76 | + // Set default network if empty |
| 77 | + if cfg.Net == "" { |
| 78 | + cfg.Net = "tcp" |
| 79 | + } |
| 80 | + |
| 81 | + // Set default address if empty |
| 82 | + if cfg.Addr == "" { |
| 83 | + switch cfg.Net { |
| 84 | + case "tcp": |
| 85 | + cfg.Addr = "127.0.0.1:3306" |
| 86 | + case "unix": |
| 87 | + cfg.Addr = "/tmp/mysql.sock" |
| 88 | + default: |
| 89 | + return errors.New("default addr for network '" + cfg.Net + "' unknown") |
| 90 | + } |
| 91 | + |
| 92 | + } else if cfg.Net == "tcp" { |
| 93 | + cfg.Addr = ensureHavePort(cfg.Addr) |
| 94 | + } |
| 95 | + |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
60 | 99 | // FormatDSN formats the given Config into a DSN string which can be passed to
|
61 | 100 | // the driver.
|
62 | 101 | func (cfg *Config) FormatDSN() string {
|
@@ -273,11 +312,7 @@ func (cfg *Config) FormatDSN() string {
|
273 | 312 | // ParseDSN parses the DSN string to a Config
|
274 | 313 | func ParseDSN(dsn string) (cfg *Config, err error) {
|
275 | 314 | // New config with some default values
|
276 |
| - cfg = &Config{ |
277 |
| - Loc: time.UTC, |
278 |
| - Collation: defaultCollation, |
279 |
| - AllowNativePasswords: true, |
280 |
| - } |
| 315 | + cfg = NewConfig() |
281 | 316 |
|
282 | 317 | // [user[:password]@][net[(addr)]]/dbname[?param1=value1¶mN=valueN]
|
283 | 318 | // Find the last '/' (since the password or the net addr might contain a '/')
|
@@ -345,31 +380,9 @@ func ParseDSN(dsn string) (cfg *Config, err error) {
|
345 | 380 | return nil, errInvalidDSNNoSlash
|
346 | 381 | }
|
347 | 382 |
|
348 |
| - if cfg.InterpolateParams && unsafeCollations[cfg.Collation] { |
349 |
| - return nil, errInvalidDSNUnsafeCollation |
350 |
| - } |
351 |
| - |
352 |
| - // Set default network if empty |
353 |
| - if cfg.Net == "" { |
354 |
| - cfg.Net = "tcp" |
| 383 | + if err = cfg.normalize(); err != nil { |
| 384 | + return nil, err |
355 | 385 | }
|
356 |
| - |
357 |
| - // Set default address if empty |
358 |
| - if cfg.Addr == "" { |
359 |
| - switch cfg.Net { |
360 |
| - case "tcp": |
361 |
| - cfg.Addr = "127.0.0.1:3306" |
362 |
| - case "unix": |
363 |
| - cfg.Addr = "/tmp/mysql.sock" |
364 |
| - default: |
365 |
| - return nil, errors.New("default addr for network '" + cfg.Net + "' unknown") |
366 |
| - } |
367 |
| - |
368 |
| - } |
369 |
| - if cfg.Net == "tcp" { |
370 |
| - cfg.Addr = ensureHavePort(cfg.Addr) |
371 |
| - } |
372 |
| - |
373 | 386 | return
|
374 | 387 | }
|
375 | 388 |
|
|
0 commit comments