nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: twmb/franz-go
Pull request #553 (2023-08-31 14:28:57)

sr/serde: Export Confluent header.
This PR makes ConfluentHeader an exported type. It provides 3 useful methods when working with the Confluent wire format.

No changes in logic were made.


Patch:
diff --git a/pkg/sr/serde.go b/pkg/sr/serde.go
index 8de7b5eb..909701f8 100644
--- a/pkg/sr/serde.go
+++ b/pkg/sr/serde.go
@@ -373,18 +373,18 @@ type SerdeHeader interface {
 	DecodeIndex(in []byte, maxLength int) (index []int, out []byte, err error)
 }
 
-var defaultSerdeHeader = new(confluentHeader)
+var defaultSerdeHeader = new(ConfluentHeader)
 
-// confluentHeader is a SerdeHeader that produces the Confluent wire format. It
+// ConfluentHeader is a SerdeHeader that produces the Confluent wire format. It
 // starts with 0, then big endian uint32 of the ID, then index (only protobuf),
 // then the encoded message.
 //
 // https://docs.confluent.io/platform/current/schema-registry/serdes-develop/index.html#wire-format
-type confluentHeader struct{}
+type ConfluentHeader struct{}
 
 // AppendEncode appends an encoded header to b according to the Confluent wire
 // format and returns it. Error is always nil.
-func (*confluentHeader) AppendEncode(b []byte, id int, index []int) ([]byte, error) {
+func (*ConfluentHeader) AppendEncode(b []byte, id int, index []int) ([]byte, error) {
 	b = append(
 		b,
 		0,
@@ -411,7 +411,7 @@ func (*confluentHeader) AppendEncode(b []byte, id int, index []int) ([]byte, err
 // DecodeID strips and decodes the schema ID from b. It returns the ID alongside
 // the unread bytes. If the header does not contain the magic byte or b contains
 // less than 5 bytes it returns ErrBadHeader.
-func (*confluentHeader) DecodeID(b []byte) (int, []byte, error) {
+func (*ConfluentHeader) DecodeID(b []byte) (int, []byte, error) {
 	if len(b) < 5 || b[0] != 0 {
 		return 0, nil, ErrBadHeader
 	}
@@ -424,7 +424,7 @@ func (*confluentHeader) DecodeID(b []byte) (int, []byte, error) {
 // ID should already be stripped away). If maxLength is greater than 0 and the
 // encoded data contains more indices than maxLength the function returns
 // ErrNotRegistered.
-func (*confluentHeader) DecodeIndex(b []byte, maxLength int) ([]int, []byte, error) {
+func (*ConfluentHeader) DecodeIndex(b []byte, maxLength int) ([]int, []byte, error) {
 	r := bReader{b}
 	br := io.ByteReader(&r)
 	l, err := binary.ReadVarint(br)


Test patch:
diff --git a/pkg/sr/serde_test.go b/pkg/sr/serde_test.go
index 45287c30..4b591fa0 100644
--- a/pkg/sr/serde_test.go
+++ b/pkg/sr/serde_test.go
@@ -144,7 +144,7 @@ func TestSerde(t *testing.T) {
 }
 
 func TestConfluentHeader(t *testing.T) {
-	var h confluentHeader
+	var h ConfluentHeader
 
 	for i, test := range []struct {
 		id     int
Repository: twmb/franz-go
Pull request #745 (2024-05-27 09:46:44)

SR: prevent panic when no decode function is registered
A small fix for a possible panic when a type is registered without a decode function. The test kinda hides this possibility, because `Serde` has a default `DecodeFn` option. This case is more likely to hit when `Serde` is instantiated without that option.

I removed the decode test case for schema ID 100, as it was the same as the one for ID 99, instead I added one with ID 101 which has a registered type, but no decode function.


Patch:
diff --git a/pkg/sr/serde.go b/pkg/sr/serde.go
index d4cb1634..10ebad5d 100644
--- a/pkg/sr/serde.go
+++ b/pkg/sr/serde.go
@@ -387,7 +387,7 @@ func (s *Serde) decodeFind(b []byte) ([]byte, tserde, error) {
 			t = t.subindex[idx]
 		}
 	}
-	if !t.exists {
+	if !t.exists || t.decode == nil {
 		return nil, tserde{}, ErrNotRegistered
 	}
 	return b, t, nil


Test patch:
diff --git a/pkg/sr/serde_test.go b/pkg/sr/serde_test.go
index 486e76c6..92c6ef44 100644
--- a/pkg/sr/serde_test.go
+++ b/pkg/sr/serde_test.go
@@ -53,6 +53,7 @@ func TestSerde(t *testing.T) {
 	serde.Register(3, idx4{}, Index(0, 0, 1))
 	serde.Register(3, idx3{}, Index(0, 0))
 	serde.Register(5, oneidx{}, Index(0), GenerateFn(func() any { return &oneidx{Foo: "defoo", Bar: "debar"} }))
+	serde.Register(101, struct{}{}, DecodeFn(nil))
 
 	for i, test := range []struct {
 		enc    any
@@ -154,8 +155,8 @@ func TestSerde(t *testing.T) {
 	if _, err := serde.DecodeNew([]byte{0, 0, 0, 0, 99}); err != ErrNotRegistered {
 		t.Errorf("got %v != exp ErrNotRegistered", err)
 	}
-	if _, err := serde.DecodeNew([]byte{0, 0, 0, 0, 100, 0}); err != ErrNotRegistered {
-		// schema is registered but type is unknown
+	if _, err := serde.DecodeNew([]byte{0, 0, 0, 0, 101, 0}); err != ErrNotRegistered {
+		// schema is registered but no decode function
 		t.Errorf("got %v != exp ErrNotRegistered", err)
 	}
 }