From f8d4ccd0bcb4d6dd79315f9038a74c1adf47350e Mon Sep 17 00:00:00 2001 From: Shubhendra Singh Chauhan Date: Mon, 15 Mar 2021 17:28:40 +0530 Subject: [PATCH] fix: tidyup code quality warnings (#1887) - Merge variable declaration with assignment - Simplify `switch` cases with type assertion - Remove unnecessary wrapping of function call - Fix unnecessary typecasting on `bytes.Buffer` - Remove unnecessary guard around map - Fix check for empty string - Fix Yoda conditions --- admin.go | 6 +----- alter_partition_reassignments_response_test.go | 4 +--- client.go | 8 ++++---- client_test.go | 2 +- examples/http_server/http_server_test.go | 2 +- examples/sasl_scram_client/scram_client.go | 5 ++--- list_partition_reassignments_request_test.go | 4 +--- list_partition_reassignments_response_test.go | 4 +--- 8 files changed, 12 insertions(+), 23 deletions(-) diff --git a/admin.go b/admin.go index 9dea0255f..e0b102034 100644 --- a/admin.go +++ b/admin.go @@ -549,11 +549,7 @@ func (ca *clusterAdmin) DeleteRecords(topic string, partitionOffsets map[int32]i if err != nil { return err } - if _, ok := partitionPerBroker[broker]; ok { - partitionPerBroker[broker] = append(partitionPerBroker[broker], partition) - } else { - partitionPerBroker[broker] = []int32{partition} - } + partitionPerBroker[broker] = append(partitionPerBroker[broker], partition) } errs := make([]error, 0) for broker, partitions := range partitionPerBroker { diff --git a/alter_partition_reassignments_response_test.go b/alter_partition_reassignments_response_test.go index 614b571b3..25f1fd792 100644 --- a/alter_partition_reassignments_response_test.go +++ b/alter_partition_reassignments_response_test.go @@ -26,9 +26,7 @@ var ( ) func TestAlterPartitionReassignmentResponse(t *testing.T) { - var response *AlterPartitionReassignmentsResponse - - response = &AlterPartitionReassignmentsResponse{ + var response *AlterPartitionReassignmentsResponse = &AlterPartitionReassignmentsResponse{ ThrottleTimeMs: int32(10000), Version: int16(0), } diff --git a/client.go b/client.go index 6127ed780..85180a413 100644 --- a/client.go +++ b/client.go @@ -475,7 +475,7 @@ func (client *client) RefreshMetadata(topics ...string) error { // error. This handles the case by returning an error instead of sending it // off to Kafka. See: https://github.com/Shopify/sarama/pull/38#issuecomment-26362310 for _, topic := range topics { - if len(topic) == 0 { + if topic == "" { return ErrInvalidTopic // this is the error that 0.8.2 and later correctly return } } @@ -892,7 +892,7 @@ func (client *client) tryRefreshMetadata(topics []string, attemptsRemaining int, req.Version = 1 } response, err := broker.GetMetadata(req) - switch err.(type) { + switch err := err.(type) { case nil: allKnownMetaData := len(topics) == 0 // valid response, use it @@ -909,12 +909,12 @@ func (client *client) tryRefreshMetadata(topics []string, attemptsRemaining int, case KError: // if SASL auth error return as this _should_ be a non retryable err for all brokers - if err.(KError) == ErrSASLAuthenticationFailed { + if err == ErrSASLAuthenticationFailed { Logger.Println("client/metadata failed SASL authentication") return err } - if err.(KError) == ErrTopicAuthorizationFailed { + if err == ErrTopicAuthorizationFailed { Logger.Println("client is not authorized to access this topic. The topics were: ", topics) return err } diff --git a/client_test.go b/client_test.go index 512b0949a..272ae8430 100644 --- a/client_test.go +++ b/client_test.go @@ -61,7 +61,7 @@ func TestCachedPartitions(t *testing.T) { // Verify we actually use the cache at all! tmp[allPartitions] = []int32{1, 2, 3, 4} client.cachedPartitionsResults["my_topic"] = tmp - if 4 != len(client.cachedPartitions("my_topic", allPartitions)) { + if len(client.cachedPartitions("my_topic", allPartitions)) != 4 { t.Fatal("Not using the cache!") } diff --git a/examples/http_server/http_server_test.go b/examples/http_server/http_server_test.go index 7b2451e28..ac3ba4d07 100644 --- a/examples/http_server/http_server_test.go +++ b/examples/http_server/http_server_test.go @@ -42,7 +42,7 @@ func TestCollectSuccessfully(t *testing.T) { t.Errorf("Expected HTTP status 200, found %d", res.Code) } - if string(res.Body.Bytes()) != "Your data is stored with unique identifier important/0/1" { + if res.Body.String() != "Your data is stored with unique identifier important/0/1" { t.Error("Unexpected response body", res.Body) } } diff --git a/examples/sasl_scram_client/scram_client.go b/examples/sasl_scram_client/scram_client.go index 6f6228176..551f0fefd 100644 --- a/examples/sasl_scram_client/scram_client.go +++ b/examples/sasl_scram_client/scram_client.go @@ -3,13 +3,12 @@ package main import ( "crypto/sha256" "crypto/sha512" - "hash" "github.com/xdg/scram" ) -var SHA256 scram.HashGeneratorFcn = func() hash.Hash { return sha256.New() } -var SHA512 scram.HashGeneratorFcn = func() hash.Hash { return sha512.New() } +var SHA256 scram.HashGeneratorFcn = sha256.New +var SHA512 scram.HashGeneratorFcn = sha512.New type XDGSCRAMClient struct { *scram.Client diff --git a/list_partition_reassignments_request_test.go b/list_partition_reassignments_request_test.go index 41a5b9cf1..b47480ece 100644 --- a/list_partition_reassignments_request_test.go +++ b/list_partition_reassignments_request_test.go @@ -14,9 +14,7 @@ var ( ) func TestListPartitionReassignmentRequest(t *testing.T) { - var request *ListPartitionReassignmentsRequest - - request = &ListPartitionReassignmentsRequest{ + var request *ListPartitionReassignmentsRequest = &ListPartitionReassignmentsRequest{ TimeoutMs: int32(10000), Version: int16(0), } diff --git a/list_partition_reassignments_response_test.go b/list_partition_reassignments_response_test.go index ba6ca5c5b..7b27b064e 100644 --- a/list_partition_reassignments_response_test.go +++ b/list_partition_reassignments_response_test.go @@ -19,9 +19,7 @@ var ( ) func TestListPartitionReassignmentResponse(t *testing.T) { - var response *ListPartitionReassignmentsResponse - - response = &ListPartitionReassignmentsResponse{ + var response *ListPartitionReassignmentsResponse = &ListPartitionReassignmentsResponse{ ThrottleTimeMs: int32(10000), Version: int16(0), }