Skip to content

Commit

Permalink
fix: generic read of fixed (hamba#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
nrwiersma authored Aug 16, 2023
1 parent 69c5ebe commit db52104
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 10 additions & 1 deletion reader_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package avro

import (
"fmt"
"reflect"
"time"
)

Expand Down Expand Up @@ -118,7 +119,7 @@ func (r *Reader) ReadNext(schema Schema) any {
dec := ls.(*DecimalLogicalSchema)
return ratFromBytes(obj, dec.Scale())
}
return obj
return byteSliceToArray(obj, size)
default:
r.ReportError("Read", fmt.Sprintf("unexpected schema type: %v", schema.Type()))
return nil
Expand Down Expand Up @@ -152,3 +153,11 @@ func (r *Reader) ReadMapCB(fn func(*Reader, string) bool) {
}
}
}

var byteType = reflect.TypeOf((*byte)(nil)).Elem()

func byteSliceToArray(b []byte, size int) any {
vArr := reflect.New(reflect.ArrayOf(size, byteType)).Elem()
reflect.Copy(vArr, reflect.ValueOf(b))
return vArr.Interface()
}
2 changes: 1 addition & 1 deletion reader_generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func TestReader_ReadNext(t *testing.T) {
name: "Fixed",
data: []byte{0x66, 0x6F, 0x6F, 0x66, 0x6F, 0x6F},
schema: `{"type":"fixed", "name": "test", "size": 6}`,
want: []byte{'f', 'o', 'o', 'f', 'o', 'o'},
want: [6]byte{'f', 'o', 'o', 'f', 'o', 'o'},
wantErr: require.NoError,
},
{
Expand Down

0 comments on commit db52104

Please sign in to comment.