Skip to content

Commit eeb9d56

Browse files
authored
protocol: Use standardized result type checking
1 parent 512b856 commit eeb9d56

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

protocol/describeconfigs/describeconfigs.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package describeconfigs
22

33
import (
4-
"fmt"
54
"strconv"
65

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

9089
for _, result := range results {
91-
switch v := result.(type) {
92-
case *Response:
93-
response.Resources = append(
94-
response.Resources,
95-
v.Resources...,
96-
)
97-
98-
case error:
99-
return nil, v
100-
101-
default:
102-
panic(fmt.Sprintf("unknown result type in Merge: %T", result))
90+
m, err := protocol.Result(result)
91+
if err != nil {
92+
return nil, err
10393
}
94+
response.Resources = append(
95+
response.Resources,
96+
m.(*Response).Resources...,
97+
)
10498
}
10599

106100
return response, nil

protocol/describeconfigs/describeconfigs_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package describeconfigs
22

33
import (
44
"errors"
5+
"fmt"
56
"io"
67
"reflect"
78
"testing"
89

910
"github.com/segmentio/kafka-go/protocol"
11+
"github.com/stretchr/testify/require"
1012
)
1113

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

protocol/describegroups/describegroups.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ func (r *Response) Merge(requests []protocol.Message, results []interface{}) (
7474
response := &Response{}
7575

7676
for _, result := range results {
77-
response.Groups = append(response.Groups, result.(*Response).Groups...)
77+
m, err := protocol.Result(result)
78+
if err != nil {
79+
return nil, err
80+
}
81+
response.Groups = append(response.Groups, m.(*Response).Groups...)
7882
}
7983

8084
return response, nil

0 commit comments

Comments
 (0)