Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protocol: Use standardized result type checking #942

Merged
merged 5 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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