Skip to content

Commit

Permalink
protocol: Use standardized result type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
nlsun authored Jul 8, 2022
1 parent 512b856 commit eeb9d56
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
20 changes: 7 additions & 13 deletions protocol/describeconfigs/describeconfigs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package describeconfigs

import (
"fmt"
"strconv"

"github.com/segmentio/kafka-go/protocol"
Expand Down Expand Up @@ -88,19 +87,14 @@ func (r *Response) Merge(requests []protocol.Message, results []interface{}) (
response := &Response{}

for _, result := range results {
switch v := result.(type) {
case *Response:
response.Resources = append(
response.Resources,
v.Resources...,
)

case error:
return nil, v

default:
panic(fmt.Sprintf("unknown result type in Merge: %T", result))
m, err := protocol.Result(result)
if err != nil {
return nil, err
}
response.Resources = append(
response.Resources,
m.(*Response).Resources...,
)
}

return response, nil
Expand Down
6 changes: 3 additions & 3 deletions protocol/describeconfigs/describeconfigs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package describeconfigs

import (
"errors"
"fmt"
"io"
"reflect"
"testing"

"github.com/segmentio/kafka-go/protocol"
"github.com/stretchr/testify/require"
)

func TestResponse_Merge(t *testing.T) {
Expand Down Expand Up @@ -59,9 +61,7 @@ func TestResponse_Merge(t *testing.T) {
t.Run("panic with unexpected type", func(t *testing.T) {
defer func() {
msg := recover()
if msg != "unknown result type in Merge: string" {
t.Fatal("unexpected panic", msg)
}
require.Equal(t, "BUG: result must be a message or an error but not string", fmt.Sprintf("%s", msg))
}()
r := &Response{}

Expand Down
6 changes: 5 additions & 1 deletion protocol/describegroups/describegroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ func (r *Response) Merge(requests []protocol.Message, results []interface{}) (
response := &Response{}

for _, result := range results {
response.Groups = append(response.Groups, result.(*Response).Groups...)
m, err := protocol.Result(result)
if err != nil {
return nil, err
}
response.Groups = append(response.Groups, m.(*Response).Groups...)
}

return response, nil
Expand Down

0 comments on commit eeb9d56

Please sign in to comment.