From 76cc55a85847e2afc32e152c786f10e8a2d94227 Mon Sep 17 00:00:00 2001 From: tegk Date: Fri, 15 Nov 2019 16:57:44 +0000 Subject: [PATCH 1/7] rewrite if-else to switch statement --- stores/pqdeadlines/pqdeadlines.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/stores/pqdeadlines/pqdeadlines.go b/stores/pqdeadlines/pqdeadlines.go index 8d14be3c..f3c5e419 100644 --- a/stores/pqdeadlines/pqdeadlines.go +++ b/stores/pqdeadlines/pqdeadlines.go @@ -79,23 +79,26 @@ func (t deadlineDriver) Open(connection string) (driver.Conn, error) { return nil, err } } + for _, keyVal := range strings.Fields(connection) { s := strings.Split(keyVal, "=") key := strings.ToLower(s[0]) - if key == "readtimeout" { + switch key { + case "readtimeout": readTimeout, err = time.ParseDuration(s[1]) if err != nil { return nil, fmt.Errorf("unable to parse readTimeout: %v", err) } - } else if key == "writetimeout" { + case "writetimeout": writeTimeout, err = time.ParseDuration(s[1]) if err != nil { return nil, fmt.Errorf("unable to parse writeTimeout: %v", err) } - } else { + default: connKeyVals = append(connKeyVals, keyVal) } } + connStr := strings.Join(connKeyVals, " ") td := deadlineDialer{ readTimeout: readTimeout, From 05d82e383c9f5b0b149c419e0eed53a146f7dc00 Mon Sep 17 00:00:00 2001 From: tegk Date: Fri, 15 Nov 2019 17:03:59 +0000 Subject: [PATCH 2/7] rewrite if-else to switch statement --- server/server.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/server/server.go b/server/server.go index 9ba08030..9d7e6991 100644 --- a/server/server.go +++ b/server/server.go @@ -1411,11 +1411,12 @@ func (s *StanServer) buildServerURLs() ([]string, error) { // use 127.0.0.1 or ::1 for host address (important for // Windows since connect with 0.0.0.0 or :: fails). sport := strconv.Itoa(opts.Port) - if opts.Host == "0.0.0.0" { + switch opts.Host { + case "0.0.0.0": hostport = net.JoinHostPort("127.0.0.1", sport) - } else if opts.Host == "::" || opts.Host == "[::]" { + case "::": hostport = net.JoinHostPort("::1", sport) - } else { + default: hostport = net.JoinHostPort(opts.Host, sport) } } From 828d4402dc8320a1e223db42489fa5076fb58a8c Mon Sep 17 00:00:00 2001 From: tegk Date: Fri, 15 Nov 2019 17:06:16 +0000 Subject: [PATCH 3/7] rewrite single switch statement to if statement --- stores/sqlstore.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stores/sqlstore.go b/stores/sqlstore.go index 1f9339ce..9d43a372 100644 --- a/stores/sqlstore.go +++ b/stores/sqlstore.go @@ -704,8 +704,7 @@ func (s *SQLStore) createPreparedStmts() error { func initSQLStmtsTable(driver string) { // The sqlStmts table is initialized with MySQL statements. // Update the statements for the selected driver. - switch driver { - case driverPostgres: + if driver == driverPostgres { // Replace ? with $1, $2, etc... for i, stmt := range sqlStmts { n := 0 From 6d500e6b7db566009ade30d1422cf52205715c73 Mon Sep 17 00:00:00 2001 From: tegk Date: Fri, 15 Nov 2019 17:42:18 +0000 Subject: [PATCH 4/7] add [::] as case --- server/server.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/server/server.go b/server/server.go index 9d7e6991..8ad41f0b 100644 --- a/server/server.go +++ b/server/server.go @@ -1416,6 +1416,8 @@ func (s *StanServer) buildServerURLs() ([]string, error) { hostport = net.JoinHostPort("127.0.0.1", sport) case "::": hostport = net.JoinHostPort("::1", sport) + case "[::]": + hostport = net.JoinHostPort("::1", sport) default: hostport = net.JoinHostPort(opts.Host, sport) } From d1918477dc190da667c367ddea9c004a0b9390ce Mon Sep 17 00:00:00 2001 From: tegk Date: Fri, 15 Nov 2019 17:42:53 +0000 Subject: [PATCH 5/7] fmt --- server/server.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/server.go b/server/server.go index 8ad41f0b..d32a8801 100644 --- a/server/server.go +++ b/server/server.go @@ -1416,8 +1416,8 @@ func (s *StanServer) buildServerURLs() ([]string, error) { hostport = net.JoinHostPort("127.0.0.1", sport) case "::": hostport = net.JoinHostPort("::1", sport) - case "[::]": - hostport = net.JoinHostPort("::1", sport) + case "[::]": + hostport = net.JoinHostPort("::1", sport) default: hostport = net.JoinHostPort(opts.Host, sport) } From b36222de61f07197ac45ff8c8cfa55b7d7c67fa8 Mon Sep 17 00:00:00 2001 From: tegk Date: Sun, 17 Nov 2019 19:46:02 +0000 Subject: [PATCH 6/7] consolidated into one case --- server/server.go | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/server/server.go b/server/server.go index d32a8801..0793cd43 100644 --- a/server/server.go +++ b/server/server.go @@ -1411,16 +1411,14 @@ func (s *StanServer) buildServerURLs() ([]string, error) { // use 127.0.0.1 or ::1 for host address (important for // Windows since connect with 0.0.0.0 or :: fails). sport := strconv.Itoa(opts.Port) - switch opts.Host { - case "0.0.0.0": - hostport = net.JoinHostPort("127.0.0.1", sport) - case "::": - hostport = net.JoinHostPort("::1", sport) - case "[::]": - hostport = net.JoinHostPort("::1", sport) - default: - hostport = net.JoinHostPort(opts.Host, sport) - } + switch opts.Host { + case "0.0.0.0": + hostport = net.JoinHostPort("127.0.0.1", sport) + case "::", "[::]": + hostport = net.JoinHostPort("::1", sport) + default: + hostport = net.JoinHostPort(opts.Host, sport) + } } return []string{fmt.Sprintf("nats://%s", hostport)}, nil } From 36cbb0e69338212859a7b302f03cea832d2aa6fb Mon Sep 17 00:00:00 2001 From: tegk Date: Mon, 18 Nov 2019 07:51:43 -0500 Subject: [PATCH 7/7] gofmt --- server/server.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/server/server.go b/server/server.go index 0793cd43..32bcd5d6 100644 --- a/server/server.go +++ b/server/server.go @@ -1411,14 +1411,14 @@ func (s *StanServer) buildServerURLs() ([]string, error) { // use 127.0.0.1 or ::1 for host address (important for // Windows since connect with 0.0.0.0 or :: fails). sport := strconv.Itoa(opts.Port) - switch opts.Host { - case "0.0.0.0": - hostport = net.JoinHostPort("127.0.0.1", sport) - case "::", "[::]": - hostport = net.JoinHostPort("::1", sport) - default: - hostport = net.JoinHostPort(opts.Host, sport) - } + switch opts.Host { + case "0.0.0.0": + hostport = net.JoinHostPort("127.0.0.1", sport) + case "::", "[::]": + hostport = net.JoinHostPort("::1", sport) + default: + hostport = net.JoinHostPort(opts.Host, sport) + } } return []string{fmt.Sprintf("nats://%s", hostport)}, nil }