goext/wmo/decoding.go

60 lines
1.3 KiB
Go
Raw Normal View History

2023-06-10 16:22:14 +02:00
package wmo
import (
"context"
2023-08-21 15:08:35 +02:00
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
2023-06-10 16:22:14 +02:00
)
func (c *Coll[TData]) decodeSingle(ctx context.Context, dec Decodable) (TData, error) {
if c.customDecoder != nil {
2023-08-21 15:08:35 +02:00
res, err := (*c.customDecoder)(ctx, dec)
if err != nil {
return *new(TData), exerr.Wrap(err, "failed to decode single entity with custom-decoder").Type("decoder", *c.customDecoder).Build()
}
return res, nil
2023-06-10 16:22:14 +02:00
} else {
var res TData
err := dec.Decode(&res)
if err != nil {
2023-08-21 15:08:35 +02:00
return *new(TData), exerr.Wrap(err, "failed to decode single entity").Type("target-type", res).Build()
2023-06-10 16:22:14 +02:00
}
return res, nil
}
}
func (c *Coll[TData]) decodeAll(ctx context.Context, cursor Cursorable) ([]TData, error) {
if c.customDecoder != nil {
res := make([]TData, 0, cursor.RemainingBatchLength())
for cursor.Next(ctx) {
entry, err := (*c.customDecoder)(ctx, cursor)
if err != nil {
2023-08-21 15:08:35 +02:00
return nil, exerr.Wrap(err, "failed to decode entity with custom-decoder").Type("decoder", *c.customDecoder).Build()
2023-06-10 16:22:14 +02:00
}
res = append(res, entry)
}
return res, nil
} else {
res := make([]TData, 0, cursor.RemainingBatchLength())
err := cursor.All(ctx, &res)
if err != nil {
2023-08-21 15:08:35 +02:00
return nil, exerr.Wrap(err, "failed to batch-decode entity").Type("target-type", res).Build()
2023-06-10 16:22:14 +02:00
}
return res, nil
}
}