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

27 lines
713 B
Go
Raw Normal View History

2021-12-30 01:12:36 +03:00
package decoders
import (
"errors"
2022-01-20 04:44:42 +03:00
2022-01-14 08:32:19 +03:00
"github.com/wader/fq/pkg/scalar"
2021-12-30 01:46:56 +03:00
2021-12-30 01:12:36 +03:00
"github.com/wader/fq/format/avro/schema"
"github.com/wader/fq/pkg/decode"
)
2022-01-14 08:32:19 +03:00
func decodeFixedFn(schema schema.SimplifiedSchema, sms ...scalar.Mapper) (DecodeFn, error) {
2021-12-30 01:12:36 +03:00
if schema.Size < 0 {
return nil, errors.New("fixed size must be greater than or equal to zero")
}
size := int64(schema.Size)
2022-01-14 08:32:19 +03:00
// Fixed instances are encoded using the number of bytes declared in the schema.
return func(name string, d *decode.D) any {
2022-02-08 02:35:33 +03:00
r := d.FieldRawLen(name, size*8, sms...)
val := make([]byte, size)
if _, err := r.ReadBits(val, size*8); err != nil {
2022-01-14 08:32:19 +03:00
d.Fatalf("failed to read fixed %s value: %v", name, err)
}
return val
2021-12-30 01:12:36 +03:00
}, nil
}