Skip to content
This repository was archived by the owner on Jan 21, 2022. It is now read-only.

Commit 7226b99

Browse files
jmhodgesDiego
authored and
Diego
committed
sort the generic config.Params in the DSN (go-sql-driver#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 1984511 commit 7226b99

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
@@ -34,6 +34,7 @@ ICHINOSE Shogo <shogo82148 at gmail.com>
3434
INADA Naoki <songofacandy at gmail.com>
3535
Jacek Szwec <szwec.jacek at gmail.com>
3636
James Harr <james.harr at gmail.com>
37+
Jeff Hodges <jeff at somethingsimilar.com>
3738
Jian Zhen <zhenjl at gmail.com>
3839
John Shahid <jvshahid at gmail.com>
3940
Joshua Prunier <joshua.prunier 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)