Skip to content

Commit 3955978

Browse files
jmhodgesarnehormann
authored andcommitted
sort the generic config.Params in the DSN (#637)
This sorts the config.Params values as they are placed in the DSN. This makes it easier for other projects to test that they are munging their DSNs correctly and do other similar tasks.
1 parent bf7f34f commit 3955978

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ ICHINOSE Shogo <shogo82148 at gmail.com>
3333
INADA Naoki <songofacandy at gmail.com>
3434
Jacek Szwec <szwec.jacek at gmail.com>
3535
James Harr <james.harr at gmail.com>
36+
Jeff Hodges <jeff at somethingsimilar.com>
3637
Jian Zhen <zhenjl at gmail.com>
3738
Joshua Prunier <joshua.prunier at gmail.com>
3839
Julien Lefevre <julien.lefevr at gmail.com>

dsn.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"fmt"
1616
"net"
1717
"net/url"
18+
"sort"
1819
"strconv"
1920
"strings"
2021
"time"
@@ -257,7 +258,12 @@ func (cfg *Config) FormatDSN() string {
257258

258259
// other params
259260
if cfg.Params != nil {
260-
for param, value := range cfg.Params {
261+
var params []string
262+
for param := range cfg.Params {
263+
params = append(params, param)
264+
}
265+
sort.Strings(params)
266+
for _, param := range params {
261267
if hasParam {
262268
buf.WriteByte('&')
263269
} else {
@@ -267,7 +273,7 @@ func (cfg *Config) FormatDSN() string {
267273

268274
buf.WriteString(param)
269275
buf.WriteByte('=')
270-
buf.WriteString(url.QueryEscape(value))
276+
buf.WriteString(url.QueryEscape(cfg.Params[param]))
271277
}
272278
}
273279

dsn_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,22 @@ func TestDSNUnsafeCollation(t *testing.T) {
220220
}
221221
}
222222

223+
func TestParamsAreSorted(t *testing.T) {
224+
expected := "/dbname?interpolateParams=true&foobar=baz&quux=loo"
225+
dsn := &Config{
226+
DBName: "dbname",
227+
InterpolateParams: true,
228+
Params: map[string]string{
229+
"quux": "loo",
230+
"foobar": "baz",
231+
},
232+
}
233+
actual := dsn.FormatDSN()
234+
if actual != expected {
235+
t.Errorf("generic Config.Params were not sorted: want %#v, got %#v", expected, actual)
236+
}
237+
}
238+
223239
func BenchmarkParseDSN(b *testing.B) {
224240
b.ReportAllocs()
225241

0 commit comments

Comments
 (0)