github.com/ipld/go-ipld-prime@v0.21.0/node/basicnode/bytes_stream.go (about) 1 package basicnode 2 3 import ( 4 "io" 5 6 "github.com/ipld/go-ipld-prime/datamodel" 7 "github.com/ipld/go-ipld-prime/node/mixins" 8 ) 9 10 var ( 11 _ datamodel.Node = streamBytes{nil} 12 _ datamodel.NodePrototype = Prototype__Bytes{} 13 _ datamodel.NodeBuilder = &plainBytes__Builder{} 14 _ datamodel.NodeAssembler = &plainBytes__Assembler{} 15 ) 16 17 func NewBytesFromReader(rs io.ReadSeeker) datamodel.Node { 18 return streamBytes{rs} 19 } 20 21 // streamBytes is a boxed reader that complies with datamodel.Node. 22 type streamBytes struct { 23 io.ReadSeeker 24 } 25 26 // -- Node interface methods --> 27 28 func (streamBytes) Kind() datamodel.Kind { 29 return datamodel.Kind_Bytes 30 } 31 func (streamBytes) LookupByString(string) (datamodel.Node, error) { 32 return mixins.Bytes{TypeName: "bytes"}.LookupByString("") 33 } 34 func (streamBytes) LookupByNode(key datamodel.Node) (datamodel.Node, error) { 35 return mixins.Bytes{TypeName: "bytes"}.LookupByNode(nil) 36 } 37 func (streamBytes) LookupByIndex(idx int64) (datamodel.Node, error) { 38 return mixins.Bytes{TypeName: "bytes"}.LookupByIndex(0) 39 } 40 func (streamBytes) LookupBySegment(seg datamodel.PathSegment) (datamodel.Node, error) { 41 return mixins.Bytes{TypeName: "bytes"}.LookupBySegment(seg) 42 } 43 func (streamBytes) MapIterator() datamodel.MapIterator { 44 return nil 45 } 46 func (streamBytes) ListIterator() datamodel.ListIterator { 47 return nil 48 } 49 func (streamBytes) Length() int64 { 50 return -1 51 } 52 func (streamBytes) IsAbsent() bool { 53 return false 54 } 55 func (streamBytes) IsNull() bool { 56 return false 57 } 58 func (streamBytes) AsBool() (bool, error) { 59 return mixins.Bytes{TypeName: "bytes"}.AsBool() 60 } 61 func (streamBytes) AsInt() (int64, error) { 62 return mixins.Bytes{TypeName: "bytes"}.AsInt() 63 } 64 func (streamBytes) AsFloat() (float64, error) { 65 return mixins.Bytes{TypeName: "bytes"}.AsFloat() 66 } 67 func (streamBytes) AsString() (string, error) { 68 return mixins.Bytes{TypeName: "bytes"}.AsString() 69 } 70 func (n streamBytes) AsBytes() ([]byte, error) { 71 return io.ReadAll(n) 72 } 73 func (streamBytes) AsLink() (datamodel.Link, error) { 74 return mixins.Bytes{TypeName: "bytes"}.AsLink() 75 } 76 func (streamBytes) Prototype() datamodel.NodePrototype { 77 return Prototype__Bytes{} 78 } 79 func (n streamBytes) AsLargeBytes() (io.ReadSeeker, error) { 80 return n.ReadSeeker, nil 81 }