Skip to content
This repository has been archived by the owner on Jan 17, 2022. It is now read-only.

Check for byte buffer on decode #3

Merged
merged 2 commits into from
Aug 28, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions coding.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,25 @@ import (
"github.com/ipld/go-ipld-prime/schema"
)

// byteAccessor is a reader interface that can access underlying bytes
type byteAccesor interface {
Bytes() []byte
}

// DecodeDagProto is a fast path decoding to protobuf
// from PBNode__NodeBuilders
func (nb _PBNode__NodeBuilder) DecodeDagProto(r io.Reader) error {
var pbn merkledag_pb.PBNode
encoded, err := ioutil.ReadAll(r)
if err != nil {
return fmt.Errorf("io error during unmarshal. %v", err)
var encoded []byte
var err error
byteBuf, ok := r.(byteAccesor)
if ok {
encoded = byteBuf.Bytes()
} else {
encoded, err = ioutil.ReadAll(r)
if err != nil {
return fmt.Errorf("io error during unmarshal. %v", err)
}
}
if err := pbn.Unmarshal(encoded); err != nil {
return fmt.Errorf("unmarshal failed. %v", err)
Expand Down Expand Up @@ -94,6 +106,11 @@ func (nd PBNode) EncodeDagProto(w io.Writer) error {
// DecodeDagRaw is a fast path decoding to protobuf
// from RawNode__NodeBuilders
func (nb _RawNode__NodeBuilder) DecodeDagRaw(r io.Reader) error {
byteBuf, ok := r.(byteAccesor)
if ok {
nb.nd.x = byteBuf.Bytes()
return nil
}
data, err := ioutil.ReadAll(r)
if err != nil {
return fmt.Errorf("io error during unmarshal. %v", err)
Expand Down