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 authored and elliotcourant committed Nov 8, 2023
1 parent b93c0c8 commit d2221a3
Show file tree
Hide file tree
Showing 2 changed files with 100 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
53 changes: 53 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"errors"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestParseURL(t *testing.T) {
Expand Down Expand Up @@ -261,3 +263,54 @@ func TestParseURL(t *testing.T) {
})
}
}

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) {
assert := assert.New(t)

actual := tt.opts.ToURL()
assert.Equal(tt.expected, actual)

_, err := ParseURL(actual)
assert.NoError(err)
})
}
}

func TestOptions_ToURL_reparsable(t *testing.T) {
assert := assert.New(t)

opts := &Options{
Database: "postgres",
User: "postgres",
Password: "password",
Addr: "somewhere:1234",
ApplicationName: "test",
DialTimeout: time.Second,
}

url := opts.ToURL()

actual, err := ParseURL(url)
assert.NoError(err)

assert.Equal(opts.Database, actual.Database)
assert.Equal(opts.User, actual.User)
assert.Equal(opts.Password, actual.Password)
assert.Equal(opts.Addr, actual.Addr)
assert.Equal(opts.ApplicationName, actual.ApplicationName)
assert.Equal(opts.DialTimeout, actual.DialTimeout)
}

0 comments on commit d2221a3

Please sign in to comment.