Skip to content

Commit

Permalink
add receiver method ToURL to *Options
Browse files Browse the repository at this point in the history
  • Loading branch information
dillonstreator committed Dec 1, 2021
1 parent 691def1 commit 03b7807
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
47 changes: 47 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,53 @@ func ParseURL(sURL string) (*Options, error) {
return options, nil
}

func (opts *Options) ToURL() string {
dsn := "postgres://"

if len(opts.User) > 0 {
dsn += opts.User

if len(opts.Password) > 0 {
dsn += ":" + opts.Password
}

dsn += "@"
}

if len(opts.Addr) > 0 {
dsn += opts.Addr
} else {
dsn += "localhost:5432"
}

dsn += "/" + opts.Database

values := url.Values{}

if opts.DialTimeout > 0 {
values.Add("connect_timeout", strconv.Itoa(int(opts.DialTimeout)/int(time.Second)))
}

if len(opts.ApplicationName) > 0 {
values.Add("application_name", opts.ApplicationName)
}

if opts.TLSConfig == nil {
values.Add("sslmode", "disable")
} else if opts.TLSConfig.InsecureSkipVerify {
values.Add("sslmode", "allow")
} else if !opts.TLSConfig.InsecureSkipVerify {
values.Add("sslmode", "verify-ca")
}

encoded := values.Encode()
if len(encoded) > 0 {
dsn += "?" + encoded
}

return dsn
}

func (opt *Options) getDialer() func(context.Context) (net.Conn, error) {
return func(ctx context.Context) (net.Conn, error) {
return opt.Dialer(ctx, opt.Network, opt.Addr)
Expand Down
26 changes: 26 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,29 @@ func TestParseURL(t *testing.T) {
})
}
}

var hello struct {
Test string
}

func TestOptions_ToURL(t *testing.T) {
tests := []struct {
name string
opts *Options
expected string
}{
{"Empty", &Options{Database: "postgres"}, "postgres://localhost:5432/postgres?sslmode=disable"},
{"User", &Options{Database: "postgres", User: "postgres"}, "postgres://postgres@localhost:5432/postgres?sslmode=disable"},
{"UserPass", &Options{Database: "postgres", User: "postgres", Password: "password"}, "postgres://postgres:password@localhost:5432/postgres?sslmode=disable"},
{"UserPassAddr", &Options{Database: "postgres", User: "postgres", Password: "password", Addr: "somewhere:1234"}, "postgres://postgres:password@somewhere:1234/postgres?sslmode=disable"},
{"UserPassAddrAppl", &Options{Database: "postgres", User: "postgres", Password: "password", Addr: "somewhere:1234", ApplicationName: "test"}, "postgres://postgres:password@somewhere:1234/postgres?application_name=test&sslmode=disable"},
{"UserPassAddrApplTimeout", &Options{Database: "postgres", User: "postgres", Password: "password", Addr: "somewhere:1234", ApplicationName: "test", DialTimeout: time.Second}, "postgres://postgres:password@somewhere:1234/postgres?application_name=test&connect_timeout=1&sslmode=disable"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.opts.ToURL(); got != tt.expected {
t.Errorf("Options.ToURL() = %v, expected %v", got, tt.expected)
}
})
}
}

0 comments on commit 03b7807

Please sign in to comment.