Skip to content

Commit

Permalink
fix: tidyup code quality warnings (#1887)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
withshubh authored Mar 15, 2021
1 parent 7285d6c commit f8d4ccd
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 23 deletions.
6 changes: 1 addition & 5 deletions admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions alter_partition_reassignments_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down
8 changes: 4 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
}

Expand Down
2 changes: 1 addition & 1 deletion examples/http_server/http_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
5 changes: 2 additions & 3 deletions examples/sasl_scram_client/scram_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions list_partition_reassignments_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down
4 changes: 1 addition & 3 deletions list_partition_reassignments_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down

0 comments on commit f8d4ccd

Please sign in to comment.