1
1
mirror of https://github.com/wader/fq.git synced 2024-11-28 03:02:55 +03:00
fq/format/avro/decoders/fixed.go
Mattias Wadman e9d9f8aef9 fq: Use go 1.18
Rename s/interface{}/any/g
Preparation for using generics in decode API and native jq funcations etc
Remove some unused linter ignores as linter has been fixed
2022-05-20 15:23:16 +02:00

27 lines
713 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.Mapper) (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
}