rwutil

package
v1.0.0-rc.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 21, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BytesTest

func BytesTest[T interface{ Bytes() []byte }](t *testing.T, obj1 T, fromBytes func(data []byte) (T, error)) T

func ReadFromBytes

func ReadFromBytes[T IoReader](data []byte, obj T) (T, error)

ReadFromBytes is a wrapper that uses an object's Read() function to marshal the object from data bytes. It's typically used to implement a one-line <Type>FromBytes() function and returns the expected type and error.

func ReadFromFunc

func ReadFromFunc[T any](rr *Reader, fromBytes func([]byte) (T, error)) (ret T)

ReadFromFunc allows a reader to use any <Type>FromBytes()-like function as a source. It will read the next group of bytes and pass it to the specified function and returns the correct type of object

func ReadN

func ReadN(r io.Reader, data []byte) error

func ReadWriteTest

func ReadWriteTest[T IoReadWriter](t *testing.T, obj1 T, newObj T) T

ReadWriteTest can be used with any object that implements IoReader and IoWriter to test whether the Read() and Write() functions complement each other correctly. You pass in an object that has all fields that need serialization set, plus a new, empty object that will receive the deserialized data. The function will Write, Read, and Write again and compare the objects for equality and both serialized versions as well. It will return the deserialized object in case the user wants to perform more tests with it.

func StringTest

func StringTest[T interface{ String() string }](t *testing.T, obj1 T, fromString func(data string) (T, error)) T

func WriteN

func WriteN(w io.Writer, data []byte) error

func WriteToBytes

func WriteToBytes(obj IoWriter) []byte

WriteToBytes is a wrapper that uses an object's Write() function to marshal the object to data bytes. It's typically used to implement a one-line Bytes() function for the object.

Types

type Buffer

type Buffer []byte

Buffer implements a hyper-simple and efficient in-memory read/write stream. It will read from the start of the buffer and write to the end of the buffer.

func (*Buffer) Read

func (buf *Buffer) Read(data []byte) (int, error)

func (*Buffer) Write

func (buf *Buffer) Write(data []byte) (int, error)

type Counter

type Counter struct {
	// contains filtered or unexported fields
}

Counter implements a read/write stream that can wrap another stream. It will count the total number of bytes read/written from/to the wrapped stream.

func NewReadCounter

func NewReadCounter(rr *Reader) (ret *Counter)

func NewWriteCounter

func NewWriteCounter(ww *Writer) (ret *Counter)

func (*Counter) Close

func (counter *Counter) Close()

func (*Counter) Count

func (counter *Counter) Count() int

func (*Counter) Read

func (counter *Counter) Read(data []byte) (int, error)

func (*Counter) Write

func (counter *Counter) Write(data []byte) (int, error)

type IoReadWriter

type IoReadWriter interface {
	IoReader
	IoWriter
}

type IoReader

type IoReader interface {
	Read(r io.Reader) error
}

type IoWriter

type IoWriter interface {
	Write(w io.Writer) error
}

type Kind

type Kind byte

type Must

type Must struct {
	// contains filtered or unexported fields
}

Must will wrap a reader stream and will panic whenever an error occurs on that stream.

func (*Must) Read

func (must *Must) Read(data []byte) (int, error)

type PushBack

type PushBack struct {
	// contains filtered or unexported fields
}

PushBack implements a pushback wrapper for any read stream. It uses an in-memory buffer that allows you to write data back to the stream. It will read this data first, and then resume reading from the wrapped stream. The pushback Writer is only valid for this Reader until it resumes the stream. See accounts.getNFTData() and accounts.saveNFTData() for an example of how Pushback and Skipper work in conjunction.

func (*PushBack) Read

func (push *PushBack) Read(data []byte) (int, error)

func (*PushBack) Write

func (push *PushBack) Write(data []byte) (int, error)

type Reader

type Reader struct {
	Err error
	// contains filtered or unexported fields
}

func NewBytesReader

func NewBytesReader(data []byte) *Reader

func NewReader

func NewReader(r io.Reader) *Reader

func (*Reader) Bytes

func (rr *Reader) Bytes() []byte

Bytes will return the remaining bytes in the Reader buffer. It is asserted that the read stream is a Buffer, and that the Reader error state is nil.

func (*Reader) CheckAvailable

func (rr *Reader) CheckAvailable(nrOfBytes int) int

func (*Reader) Close

func (rr *Reader) Close()

Close indicates the end of reading from the bytes buffer. If any unread bytes are remaining in the buffer an error will be returned.

func (*Reader) Must

func (rr *Reader) Must() *Reader

Must will wrap the reader stream in a stream that will panic whenever an error occurs.

func (*Reader) PushBack

func (rr *Reader) PushBack() *Writer

PushBack returns a pushback writer that allows you to insert data before the stream. The Reader will read this data first, and then resume reading from the stream. The pushback Writer is only valid for this Reader until it resumes the stream.

func (*Reader) Read

func (rr *Reader) Read(obj IoReader)

func (*Reader) ReadAmount16

func (rr *Reader) ReadAmount16() (ret uint16)

ReadAmount16 reads a variable-length encoded amount.

func (*Reader) ReadAmount32

func (rr *Reader) ReadAmount32() (ret uint32)

ReadAmount32 reads a variable-length encoded amount.

func (*Reader) ReadAmount64

func (rr *Reader) ReadAmount64() (ret uint64)

ReadAmount64 reads a variable-length encoded amount.

func (*Reader) ReadBool

func (rr *Reader) ReadBool() bool

func (*Reader) ReadByte

func (rr *Reader) ReadByte() byte

func (*Reader) ReadBytes

func (rr *Reader) ReadBytes() []byte

func (*Reader) ReadDuration

func (rr *Reader) ReadDuration() (ret time.Duration)

func (*Reader) ReadFromFunc

func (rr *Reader) ReadFromFunc(read func(w io.Reader) (int, error))

func (*Reader) ReadGas64

func (rr *Reader) ReadGas64() (ret uint64)

ReadGas64 reads a variable-length encoded amount of gas. Note that the amount was incremented before storing so that the math.MaxUint64 gas limit will wrap to zero and only takes 1 byte.

func (*Reader) ReadInt16

func (rr *Reader) ReadInt16() (ret int16)

func (*Reader) ReadInt32

func (rr *Reader) ReadInt32() (ret int32)

func (*Reader) ReadInt64

func (rr *Reader) ReadInt64() (ret int64)

func (*Reader) ReadInt8

func (rr *Reader) ReadInt8() (ret int8)

func (*Reader) ReadKind

func (rr *Reader) ReadKind() Kind

func (*Reader) ReadKindAndVerify

func (rr *Reader) ReadKindAndVerify(expectedKind Kind)

func (*Reader) ReadN

func (rr *Reader) ReadN(ret []byte)

func (*Reader) ReadSerialized

func (rr *Reader) ReadSerialized(obj deserializable, sizes ...int)

ReadSerialized reads the deserializable object from the stream. If no sizes are present a 16-bit size is read from the stream. The first size indicates a different limit for the size read from the stream. The second size indicates the expected size and does not read it from the stream.

func (*Reader) ReadSize16

func (rr *Reader) ReadSize16() (size int)

ReadSize16 reads a 16-bit size from the stream. We expect this size to indicate how many items we are about to read from the stream. Therefore, if we can determine that there are not at least this amount of bytes available in the stream we raise an error and return zero for the size.

func (*Reader) ReadSize32

func (rr *Reader) ReadSize32() (size int)

ReadSize32 reads a 32-bit size from the stream. We expect this size to indicate how many items we are about to read from the stream. Therefore, if we can determine that there are not at least this amount of bytes available in the stream we raise an error and return zero for the size.

func (*Reader) ReadSizeWithLimit

func (rr *Reader) ReadSizeWithLimit(limit uint32) int

ReadSizeWithLimit reads an int size from the stream, and verifies that it does not exceed the specified limit. By limiting the size we can better detect malformed input data. The size returned will always be zero if an error occurred.

func (*Reader) ReadString

func (rr *Reader) ReadString() (ret string)

func (*Reader) ReadUint16

func (rr *Reader) ReadUint16() (ret uint16)

func (*Reader) ReadUint256

func (rr *Reader) ReadUint256() (ret *big.Int)

func (*Reader) ReadUint32

func (rr *Reader) ReadUint32() (ret uint32)

func (*Reader) ReadUint64

func (rr *Reader) ReadUint64() (ret uint64)

func (*Reader) ReadUint8

func (rr *Reader) ReadUint8() (ret uint8)

type Skipper

type Skipper struct {
	// contains filtered or unexported fields
}

Skipper implements a skip wrapper for any writer stream. It works kind of the opposite of the PushBack wrapper. It allows you to dummy-read data from the stream and counts the bytes read. It will dummy-write these bytes first, and then resume writing to the wrapped stream The skip Reader is only valid for this Writer until it resumes the stream. See accounts.getNFTData() and accounts.saveNFTData() for an example of how Pushback and Skipper work in conjunction.

func (*Skipper) Read

func (skip *Skipper) Read(data []byte) (n int, err error)

func (*Skipper) Write

func (skip *Skipper) Write(data []byte) (nSkipped int, err error)

type Writer

type Writer struct {
	Err error
	// contains filtered or unexported fields
}

func NewBytesWriter

func NewBytesWriter() *Writer

func NewWriter

func NewWriter(w io.Writer) *Writer

func (*Writer) Bytes

func (ww *Writer) Bytes() []byte

Bytes will return the accumulated bytes in the Writer buffer. It is asserted that the write stream is a Buffer, and that the Writer error state is nil.

func (*Writer) Skip

func (ww *Writer) Skip() *Reader

func (*Writer) Write

func (ww *Writer) Write(obj IoWriter) *Writer

func (*Writer) WriteAmount16

func (ww *Writer) WriteAmount16(val uint16) *Writer

WriteAmount16 writes a variable-length encoded amount.

func (*Writer) WriteAmount32

func (ww *Writer) WriteAmount32(val uint32) *Writer

WriteAmount32 writes a variable-length encoded amount.

func (*Writer) WriteAmount64

func (ww *Writer) WriteAmount64(val uint64) *Writer

WriteAmount64 writes a variable-length encoded amount.

func (*Writer) WriteBool

func (ww *Writer) WriteBool(val bool) *Writer

func (*Writer) WriteByte

func (ww *Writer) WriteByte(val byte) *Writer

func (*Writer) WriteBytes

func (ww *Writer) WriteBytes(data []byte) *Writer

func (*Writer) WriteDuration

func (ww *Writer) WriteDuration(val time.Duration) *Writer

func (*Writer) WriteFromBytes

func (ww *Writer) WriteFromBytes(obj interface{ Bytes() []byte }) *Writer

func (*Writer) WriteFromFunc

func (ww *Writer) WriteFromFunc(write func(w io.Writer) (int, error)) *Writer

func (*Writer) WriteGas64

func (ww *Writer) WriteGas64(val uint64) *Writer

WriteGas64 writes a variable-length encoded amount of gas. Note that the amount is incremented before storing so that the math.MaxUint64 gas limit will wrap to zero and only takes 1 byte.

func (*Writer) WriteInt16

func (ww *Writer) WriteInt16(val int16) *Writer

func (*Writer) WriteInt32

func (ww *Writer) WriteInt32(val int32) *Writer

func (*Writer) WriteInt64

func (ww *Writer) WriteInt64(val int64) *Writer

func (*Writer) WriteInt8

func (ww *Writer) WriteInt8(val int8) *Writer

func (*Writer) WriteKind

func (ww *Writer) WriteKind(msgType Kind) *Writer

func (*Writer) WriteN

func (ww *Writer) WriteN(val []byte) *Writer

func (*Writer) WriteSerialized

func (ww *Writer) WriteSerialized(obj serializable, sizes ...int) *Writer

WriteSerialized writes the serializable object to the stream. If no sizes are present a 16-bit size is written to the stream. The first size indicates a different limit for the size written to the stream. The second size indicates the expected size and does not write it to the stream, but verifies that the serialized size is equal to the expected size..

func (*Writer) WriteSize16

func (ww *Writer) WriteSize16(val int) *Writer

func (*Writer) WriteSize32

func (ww *Writer) WriteSize32(val int) *Writer

func (*Writer) WriteSizeWithLimit

func (ww *Writer) WriteSizeWithLimit(val int, limit uint32) *Writer

func (*Writer) WriteString

func (ww *Writer) WriteString(val string) *Writer

func (*Writer) WriteTokens

func (ww *Writer) WriteTokens(val uint64) *Writer

func (*Writer) WriteUint16

func (ww *Writer) WriteUint16(val uint16) *Writer

func (*Writer) WriteUint256

func (ww *Writer) WriteUint256(val *big.Int) *Writer

func (*Writer) WriteUint32

func (ww *Writer) WriteUint32(val uint32) *Writer

func (*Writer) WriteUint64

func (ww *Writer) WriteUint64(val uint64) *Writer

func (*Writer) WriteUint8

func (ww *Writer) WriteUint8(val uint8) *Writer

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL