From e45183f512f6bc571e5e7618f309fe8e9785d2fc Mon Sep 17 00:00:00 2001 From: Dillon Streator Date: Wed, 1 Dec 2021 16:09:15 -0600 Subject: [PATCH] add receiver method ToURL to *Options --- options.go | 42 ++++++++++++++++++++++++++++++++++++++++++ options_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/options.go b/options.go index efd634fd..4c67163a 100644 --- a/options.go +++ b/options.go @@ -256,6 +256,48 @@ func ParseURL(sURL string) (*Options, error) { return options, nil } +func (opts *Options) ToURL() string { + parts := "postgres://" + + if len(opts.User) > 0 { + parts += opts.User + + if len(opts.Password) > 0 { + parts += ":" + opts.Password + } + + parts += "@" + } + + if len(opts.Addr) > 0 { + parts += opts.Addr + } else { + parts += "localhost:5432" + } + + parts += "/" + opts.Database + + values := url.Values{} + + 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") + } + + parts += "?" + values.Encode() + + return parts +} + 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) diff --git a/options_test.go b/options_test.go index 12178729..2c4ad347 100644 --- a/options_test.go +++ b/options_test.go @@ -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?connect_timeout=0&sslmode=disable"}, + {"User", &Options{Database: "postgres", User: "postgres"}, "postgres://postgres@localhost:5432/postgres?connect_timeout=0&sslmode=disable"}, + {"UserPass", &Options{Database: "postgres", User: "postgres", Password: "password"}, "postgres://postgres:password@localhost:5432/postgres?connect_timeout=0&sslmode=disable"}, + {"UserPassAddr", &Options{Database: "postgres", User: "postgres", Password: "password", Addr: "somewhere:1234"}, "postgres://postgres:password@somewhere:1234/postgres?connect_timeout=0&sslmode=disable"}, + {"UserPassAddrAppl", &Options{Database: "postgres", User: "postgres", Password: "password", Addr: "somewhere:1234", ApplicationName: "test"}, "postgres://postgres:password@somewhere:1234/postgres?application_name=test&connect_timeout=0&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) + } + }) + } +}