go: Fix things staticcheck complains about
Client: go
Staticcheck is the recommended replacement of the frozen and deprecated
official golint linter [1].
Fix the things it complained about (or add lint:ignore directive) in:
- lib/go/thrift
- lib/go/test/tests
- tutorial/go/src
- test/go/src
- compiler generated code
The majority of the fixes are in the following categories:
- Use of deprecated function (mainly the TConfiguration related ones)
- Redundant break in switch cases
- Unused and unexported variables/fields/functions
Also in the same spirit as fb539ae, remove the error return from
NewTSSLSocket as it can never be non-nil.
This change will be cherry-picked into 0.15.0 branch after merged.
[1]: https://groups.google.com/g/golang-nuts/c/rCP70Aq_tBc
diff --git a/test/go/src/bin/stress/main.go b/test/go/src/bin/stress/main.go
index 9f32676..3273d1b 100644
--- a/test/go/src/bin/stress/main.go
+++ b/test/go/src/bin/stress/main.go
@@ -103,14 +103,14 @@
var transportFactory thrift.TTransportFactory
if *compact {
- protocolFactory = thrift.NewTCompactProtocolFactory()
+ protocolFactory = thrift.NewTCompactProtocolFactoryConf(nil)
} else {
- protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
+ protocolFactory = thrift.NewTBinaryProtocolFactoryConf(nil)
}
if *framed {
transportFactory = thrift.NewTTransportFactory()
- transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
+ transportFactory = thrift.NewTFramedTransportFactoryConf(transportFactory, nil)
} else {
transportFactory = thrift.NewTBufferedTransportFactory(8192)
}
diff --git a/test/go/src/bin/testclient/main.go b/test/go/src/bin/testclient/main.go
index 39ee95b..f0ce052 100644
--- a/test/go/src/bin/testclient/main.go
+++ b/test/go/src/bin/testclient/main.go
@@ -36,7 +36,6 @@
var transport = flag.String("transport", "buffered", "Transport: buffered, framed, http, zlib")
var protocol = flag.String("protocol", "binary", "Protocol: binary, compact, json")
var ssl = flag.Bool("ssl", false, "Encrypted Transport using SSL")
-var zlib = flag.Bool("zlib", false, "Wrapped Transport using Zlib")
var testloops = flag.Int("testloops", 1, "Number of Tests")
func main() {
@@ -131,6 +130,9 @@
binout[i] = byte(i)
}
bin, err := client.TestBinary(defaultCtx, binout)
+ if err != nil {
+ t.Fatalf("TestBinary failed with %v", err)
+ }
for i := 0; i < 256; i++ {
if binout[i] != bin[i] {
t.Fatalf("Unexpected TestBinary() result expected %d, got %d ", binout[i], bin[i])
diff --git a/test/go/src/bin/testserver/main.go b/test/go/src/bin/testserver/main.go
index d4bd8b4..011a71d 100644
--- a/test/go/src/bin/testserver/main.go
+++ b/test/go/src/bin/testserver/main.go
@@ -35,7 +35,6 @@
var transport = flag.String("transport", "buffered", "Transport: buffered, framed, http, zlib")
var protocol = flag.String("protocol", "binary", "Protocol: binary, compact, json, header")
var ssl = flag.Bool("ssl", false, "Encrypted Transport using SSL")
-var zlib = flag.Bool("zlib", false, "Wrapped Transport using Zlib")
var certPath = flag.String("certPath", "keys", "Directory that contains SSL certificates")
func main() {
diff --git a/test/go/src/common/client.go b/test/go/src/common/client.go
index 2015035..f9dfcaf 100644
--- a/test/go/src/common/client.go
+++ b/test/go/src/common/client.go
@@ -42,32 +42,35 @@
domain_socket string,
transport string,
protocol string,
- ssl bool) (client *thrifttest.ThriftTestClient, trans thrift.TTransport, err error) {
-
+ ssl bool,
+) (client *thrifttest.ThriftTestClient, trans thrift.TTransport, err error) {
hostPort := fmt.Sprintf("%s:%d", host, port)
+ cfg := &thrift.TConfiguration{
+ TLSConfig: &tls.Config{
+ InsecureSkipVerify: true,
+ },
+ }
var protocolFactory thrift.TProtocolFactory
switch protocol {
case "compact":
- protocolFactory = thrift.NewTCompactProtocolFactory()
+ protocolFactory = thrift.NewTCompactProtocolFactoryConf(cfg)
case "simplejson":
- protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
+ protocolFactory = thrift.NewTSimpleJSONProtocolFactoryConf(cfg)
case "json":
protocolFactory = thrift.NewTJSONProtocolFactory()
case "binary":
- protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
+ protocolFactory = thrift.NewTBinaryProtocolFactoryConf(cfg)
case "header":
- protocolFactory = thrift.NewTHeaderProtocolFactory()
+ protocolFactory = thrift.NewTHeaderProtocolFactoryConf(cfg)
default:
- return nil, nil, fmt.Errorf("Invalid protocol specified %s", protocol)
+ return nil, nil, fmt.Errorf("invalid protocol specified %s", protocol)
}
if debugClientProtocol {
protocolFactory = thrift.NewTDebugProtocolFactoryWithLogger(protocolFactory, "client:", thrift.StdLogger(nil))
}
if ssl {
- trans, err = thrift.NewTSSLSocketConf(hostPort, &thrift.TConfiguration{
- TLSConfig: &tls.Config{InsecureSkipVerify: true},
- })
+ trans = thrift.NewTSSLSocketConf(hostPort, cfg)
} else {
if domain_socket != "" {
trans = thrift.NewTSocketConf(domain_socket, nil)
@@ -85,21 +88,21 @@
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
- trans, err = thrift.NewTHttpPostClientWithOptions(fmt.Sprintf("https://%s/", hostPort), thrift.THttpClientOptions{Client: client})
+ trans, err = thrift.NewTHttpClientWithOptions(fmt.Sprintf("https://%s/", hostPort), thrift.THttpClientOptions{Client: client})
fmt.Println(hostPort)
} else {
- trans, err = thrift.NewTHttpPostClient(fmt.Sprintf("http://%s/", hostPort))
+ trans, err = thrift.NewTHttpClient(fmt.Sprintf("http://%s/", hostPort))
}
case "framed":
- trans = thrift.NewTFramedTransport(trans)
+ trans = thrift.NewTFramedTransportConf(trans, cfg)
case "buffered":
trans = thrift.NewTBufferedTransport(trans, 8192)
case "zlib":
trans, err = thrift.NewTZlibTransport(trans, zlib.BestCompression)
case "":
- trans = trans
+ // Do nothing
default:
- return nil, nil, fmt.Errorf("Invalid transport specified %s", transport)
+ return nil, nil, fmt.Errorf("invalid transport specified %s", transport)
}
if err != nil {
return nil, nil, err
diff --git a/test/go/src/common/clientserver_test.go b/test/go/src/common/clientserver_test.go
index d5e3c43..609086b 100644
--- a/test/go/src/common/clientserver_test.go
+++ b/test/go/src/common/clientserver_test.go
@@ -49,7 +49,6 @@
}
func TestAllConnection(t *testing.T) {
- certPath = "../../../keys"
wg := &sync.WaitGroup{}
wg.Add(len(units))
for _, unit := range units {
@@ -67,6 +66,9 @@
handler := NewMockThriftTest(ctrl)
processor, serverTransport, transportFactory, protocolFactory, err := GetServerParams(unit.host, unit.port, unit.domain_socket, unit.transport, unit.protocol, unit.ssl, "../../../keys", handler)
+ if err != nil {
+ t.Errorf("GetServerParams failed: %v", err)
+ }
server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
if err = server.Listen(); err != nil {
diff --git a/test/go/src/common/context_test.go b/test/go/src/common/context_test.go
index 3e21a54..c6cbad8 100644
--- a/test/go/src/common/context_test.go
+++ b/test/go/src/common/context_test.go
@@ -40,8 +40,6 @@
}
func TestHttpContextTimeout(t *testing.T) {
- certPath = "../../../keys"
-
unit := test_unit{"127.0.0.1", 9096, "", "http", "binary", false}
server := &http.Server{Addr: unit.host + fmt.Sprintf(":%d", unit.port), Handler: slowHttpHandler{}}
@@ -56,6 +54,7 @@
unwrapErr := func(err error) error {
for {
+ //lint:ignore S1034 type switch is more appropriate here.
switch err.(type) {
case thrift.TTransportException:
err = err.(thrift.TTransportException).Err()
diff --git a/test/go/src/common/printing_handler.go b/test/go/src/common/printing_handler.go
index d91dde4..b726373 100644
--- a/test/go/src/common/printing_handler.go
+++ b/test/go/src/common/printing_handler.go
@@ -26,6 +26,7 @@
"fmt"
"time"
+ //lint:ignore ST1001 allow dot import here
. "github.com/apache/thrift/test/go/src/gen/thrifttest"
)
@@ -333,6 +334,7 @@
e.Message = arg
return e
case "TException":
+ //lint:ignore ST1005 To be consistent with other language libraries.
return errors.New("Just TException")
}
return
diff --git a/test/go/src/common/server.go b/test/go/src/common/server.go
index 6e3a5d3..f48389d 100644
--- a/test/go/src/common/server.go
+++ b/test/go/src/common/server.go
@@ -31,7 +31,6 @@
var (
debugServerProtocol bool
- certPath string
)
func init() {
@@ -46,28 +45,30 @@
protocol string,
ssl bool,
certPath string,
- handler thrifttest.ThriftTest) (thrift.TProcessor, thrift.TServerTransport, thrift.TTransportFactory, thrift.TProtocolFactory, error) {
+ handler thrifttest.ThriftTest,
+) (thrift.TProcessor, thrift.TServerTransport, thrift.TTransportFactory, thrift.TProtocolFactory, error) {
var err error
hostPort := fmt.Sprintf("%s:%d", host, port)
+ var cfg *thrift.TConfiguration = nil
var protocolFactory thrift.TProtocolFactory
switch protocol {
case "compact":
- protocolFactory = thrift.NewTCompactProtocolFactory()
+ protocolFactory = thrift.NewTCompactProtocolFactoryConf(cfg)
case "simplejson":
- protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
+ protocolFactory = thrift.NewTSimpleJSONProtocolFactoryConf(cfg)
case "json":
protocolFactory = thrift.NewTJSONProtocolFactory()
case "binary":
- protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
+ protocolFactory = thrift.NewTBinaryProtocolFactoryConf(nil)
case "header":
- protocolFactory = thrift.NewTHeaderProtocolFactory()
+ protocolFactory = thrift.NewTHeaderProtocolFactoryConf(nil)
default:
- return nil, nil, nil, nil, fmt.Errorf("Invalid protocol specified %s", protocol)
+ return nil, nil, nil, nil, fmt.Errorf("invalid protocol specified %s", protocol)
}
if debugServerProtocol {
- protocolFactory = thrift.NewTDebugProtocolFactory(protocolFactory, "server:")
+ protocolFactory = thrift.NewTDebugProtocolFactoryWithLogger(protocolFactory, "server:", thrift.StdLogger(nil))
}
var serverTransport thrift.TServerTransport
@@ -98,7 +99,7 @@
transportFactory = nil
case "framed":
transportFactory = thrift.NewTTransportFactory()
- transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
+ transportFactory = thrift.NewTFramedTransportFactoryConf(transportFactory, nil)
case "buffered":
transportFactory = thrift.NewTBufferedTransportFactory(8192)
case "zlib":
@@ -106,7 +107,7 @@
case "":
transportFactory = thrift.NewTTransportFactory()
default:
- return nil, nil, nil, nil, fmt.Errorf("Invalid transport specified %s", transport)
+ return nil, nil, nil, nil, fmt.Errorf("invalid transport specified %s", transport)
}
processor := thrifttest.NewThriftTestProcessor(handler)
diff --git a/test/go/src/common/simple_handler.go b/test/go/src/common/simple_handler.go
index 971f17e..fb95457 100644
--- a/test/go/src/common/simple_handler.go
+++ b/test/go/src/common/simple_handler.go
@@ -23,6 +23,7 @@
"errors"
"time"
+ //lint:ignore ST1001 allow dot import here
. "github.com/apache/thrift/test/go/src/gen/thrifttest"
)
@@ -104,6 +105,7 @@
}
func (p *simpleHandler) TestInsanity(argument *Insanity) (r map[UserId]map[Numberz]*Insanity, err error) {
+ //lint:ignore ST1005 To be consistent with other language libraries.
return nil, errors.New("No Insanity")
}
@@ -125,6 +127,7 @@
e.Message = arg
return e
case "TException":
+ //lint:ignore ST1005 To be consistent with other language libraries.
return errors.New("Just TException")
}
return