FormatDSN formats the given Config into a DSN string which can be passed to the driver.
()
| 57 | |
| 58 | // FormatDSN formats the given Config into a DSN string which can be passed to the driver. |
| 59 | func (cfg *Config) FormatDSN() string { |
| 60 | // build query from arguments |
| 61 | |
| 62 | u := &url.URL{ |
| 63 | Scheme: DBScheme, |
| 64 | Host: cfg.DatabaseID, |
| 65 | } |
| 66 | |
| 67 | newQuery := u.Query() |
| 68 | if cfg.UseFollower { |
| 69 | newQuery.Add(paramUseFollower, strconv.FormatBool(cfg.UseFollower)) |
| 70 | |
| 71 | if cfg.UseLeader { |
| 72 | newQuery.Add(paramUseLeader, strconv.FormatBool(cfg.UseLeader)) |
| 73 | } |
| 74 | } |
| 75 | if cfg.Mirror != "" { |
| 76 | newQuery.Add(paramMirror, cfg.Mirror) |
| 77 | } |
| 78 | if cfg.UseDirectRPC { |
| 79 | newQuery.Add(paramUseDirectRPC, strconv.FormatBool(cfg.UseDirectRPC)) |
| 80 | } |
| 81 | u.RawQuery = newQuery.Encode() |
| 82 | |
| 83 | return u.String() |
| 84 | } |
| 85 | |
| 86 | // ParseDSN parse the DSN string to a Config. |
| 87 | func ParseDSN(dsn string) (cfg *Config, err error) { |