1
1
mirror of https://github.com/wader/fq.git synced 2024-12-27 15:42:07 +03:00
fq/format/avro/decoders/bytes.go
Mattias Wadman 9b81d4d3ab decode: More type safe API and split scalar into multiple types
Preparation to make decoder use less memory and API more type safe.
Now each scalar type has it's own struct type so it can store different
things and enables to have a scalar interface.
Also own types will enable experimenting with decode DLS designs like
using chained methods that are type aware.
2022-12-14 16:23:58 +01:00

29 lines
679 B
Go

package decoders
import (
"github.com/wader/fq/pkg/bitio"
"github.com/wader/fq/pkg/decode"
"github.com/wader/fq/pkg/scalar"
)
type BytesCodec struct{}
func decodeBytesFn(sms ...scalar.BitBufMapper) (DecodeFn, error) {
// Bytes are encoded as a long followed by that many bytes of data.
return func(name string, d *decode.D) any {
var val []byte
d.FieldStruct(name, func(d *decode.D) {
length := d.FieldSintFn("length", VarZigZag)
br := d.FieldRawLen("data", length*8, sms...)
val = make([]byte, length)
if _, err := bitio.ReadFull(br, val, length*8); err != nil {
d.Fatalf("failed to read %s bytes: %v", name, err)
}
})
return val
}, nil
}