1
1
mirror of https://github.com/wader/fq.git synced 2024-12-20 11:51:58 +03:00
fq/format/avro/decoders/fixed.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

27 lines
719 B
Go

package decoders
import (
"errors"
"github.com/wader/fq/pkg/scalar"
"github.com/wader/fq/format/avro/schema"
"github.com/wader/fq/pkg/decode"
)
func decodeFixedFn(schema schema.SimplifiedSchema, sms ...scalar.BitBufMapper) (DecodeFn, error) {
if schema.Size < 0 {
return nil, errors.New("fixed size must be greater than or equal to zero")
}
size := int64(schema.Size)
// Fixed instances are encoded using the number of bytes declared in the schema.
return func(name string, d *decode.D) any {
r := d.FieldRawLen(name, size*8, sms...)
val := make([]byte, size)
if _, err := r.ReadBits(val, size*8); err != nil {
d.Fatalf("failed to read fixed %s value: %v", name, err)
}
return val
}, nil
}