Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add receiver method ToURL to *Options #1934

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
elliotcourant marked this conversation as resolved.
Show resolved Hide resolved

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) {
dillonstreator marked this conversation as resolved.
Show resolved Hide resolved
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)
}