Explorar o código

add streaming decoders to the API

Matt Bolt hai 1 mes
pai
achega
558f88ac0a
Modificáronse 1 ficheiros con 20 adicións e 2 borrados
  1. 20 2
      core/pkg/exporter/decoder.go

+ 20 - 2
core/pkg/exporter/decoder.go

@@ -12,14 +12,22 @@ import (
 )
 
 type Decoder[T any] func([]byte) (*T, error)
+type StreamingDecoder[T any] func(io.Reader) (*T, error)
 
-// BinaryMarshalerPtr[T] is a generic constraint to ensure types passed to the encoder implement
-// encoding.BinaryMarshaler and are pointers to T.
+// BinaryMarshalerPtr[T] is a generic constraint to ensure types passed to the decoder implement
+// encoding.BinaryUnmarshaler and are pointers to T.
 type BinaryUnmarshalerPtr[T any] interface {
 	encoding.BinaryUnmarshaler
 	*T
 }
 
+// BinaryUnmarshalWithReaderPtr[T] is a generic constraint to ensure types passed to the decoder
+// implement an unmarshal from io.Reader method.
+type BinaryUnmarshalWithReaderPtr[T any] interface {
+	UnmarshalBinaryFromReader(reader io.Reader) error
+	*T
+}
+
 func BingenDecoder[T any, U BinaryUnmarshalerPtr[T]](data []byte) (*T, error) {
 	var set U = new(T)
 
@@ -31,6 +39,16 @@ func BingenDecoder[T any, U BinaryUnmarshalerPtr[T]](data []byte) (*T, error) {
 	return set, nil
 }
 
+func StreamingBingenDecoder[T any, U BinaryUnmarshalWithReaderPtr[T]](reader io.Reader) (*T, error) {
+	var set U = new(T)
+
+	err := set.UnmarshalBinaryFromReader(reader)
+	if err != nil {
+		return nil, fmt.Errorf("failed to decode bingen: %w", err)
+	}
+	return set, nil
+}
+
 func JSONDecoder[T any](data []byte) (*T, error) {
 	var instance = new(T)
 	err := json.Unmarshal(data, instance)