Documentation ¶
Overview ¶
Canoto provides common functionality required for reading and writing the canoto format.
Index ¶
- Constants
- Variables
- func Append[T Bytes](w *Writer, v T)
- func AppendBool[T ~bool](w *Writer, b T)
- func AppendBytes[T Bytes](w *Writer, v T)
- func AppendFint32[T Int32](w *Writer, v T)
- func AppendFint64[T Int64](w *Writer, v T)
- func AppendInt[T Int](w *Writer, v T)
- func AppendSint[T Sint](w *Writer, v T)
- func CountBytes(bytes []byte, tag string) (int, error)
- func CountInts(bytes []byte) int
- func HasNext(r *Reader) bool
- func HasPrefix(bytes []byte, prefix string) bool
- func IsZero[T comparable](v T) bool
- func MakePointer[T any](_ *T) *T
- func MakeSlice[T any](_ []T, length int) []T
- func ReadBool[T ~bool](r *Reader, v *T) error
- func ReadBytes[T ~[]byte](r *Reader, v *T) error
- func ReadFint32[T Int32](r *Reader, v *T) error
- func ReadFint64[T Int64](r *Reader, v *T) error
- func ReadInt[T Int](r *Reader, v *T) error
- func ReadSint[T Sint](r *Reader, v *T) error
- func ReadString[T ~string](r *Reader, v *T) error
- func SizeBytes[T Bytes](v T) int
- func SizeInt[T Int](v T) int
- func SizeSint[T Sint](v T) int
- func Tag(fieldNumber uint32, wireType WireType) []byte
- func Zero[T any](_ T) (_ T)
- type Bytes
- type Field
- type FieldMaker
- type FieldPointer
- type Int
- type Int32
- type Int64
- type Message
- type Reader
- type Sint
- type Uint
- type WireType
- type Writer
Constants ¶
const ( Varint WireType = iota I64 Len I32 // SizeFint32 is the size of a 32-bit fixed size integer in bytes. SizeFint32 = 4 // SizeFint64 is the size of a 64-bit fixed size integer in bytes. SizeFint64 = 8 // SizeBool is the size of a boolean in bytes. SizeBool = 1 // MaxFieldNumber is the maximum field number allowed to be used in a Tag. MaxFieldNumber = 1<<29 - 1 // Version is the current version of the canoto library. Version = "v0.10.0" )
Variables ¶
var ( // Code is the actual golang code for this library; including this comment. // // This variable is not used internally, so the compiler is smart enough to // omit this value from the binary if the user of this library does not // utilize this variable; at least at the time of writing. // // This can be used during codegen to generate this library. // //go:embed canoto.go Code string ErrInvalidFieldOrder = errors.New("invalid field order") ErrUnexpectedWireType = errors.New("unexpected wire type") ErrDuplicateOneOf = errors.New("duplicate oneof field") ErrInvalidLength = errors.New("decoded length is invalid") ErrZeroValue = errors.New("zero value") ErrUnknownField = errors.New("unknown field") ErrPaddedZeroes = errors.New("padded zeroes") ErrOverflow = errors.New("overflow") ErrInvalidWireType = errors.New("invalid wire type") ErrInvalidBool = errors.New("decoded bool is neither true nor false") ErrStringNotUTF8 = errors.New("decoded string is not UTF-8") )
Functions ¶
func AppendBool ¶
func AppendBool[T ~bool](w *Writer, b T)
AppendBool writes a boolean to the writer.
func AppendBytes ¶
func AppendBytes[T Bytes](w *Writer, v T)
AppendBytes writes a length-prefixed byte slice to the writer.
func AppendFint32 ¶
func AppendFint32[T Int32](w *Writer, v T)
AppendFint32 writes a 32-bit fixed size integer to the writer.
func AppendFint64 ¶
func AppendFint64[T Int64](w *Writer, v T)
AppendFint64 writes a 64-bit fixed size integer to the writer.
func AppendInt ¶
func AppendInt[T Int](w *Writer, v T)
AppendInt writes an integer to the writer as a varint.
func AppendSint ¶
func AppendSint[T Sint](w *Writer, v T)
AppendSint writes an integer to the writer as a zigzag encoded varint.
func CountBytes ¶
CountBytes counts the consecutive number of length-prefixed fields with the given tag.
func IsZero ¶
func IsZero[T comparable](v T) bool
IsZero returns true if the value is the zero value for its type.
func MakePointer ¶ added in v0.5.0
func MakePointer[T any](_ *T) *T
MakePointer creates a new pointer. It is equivalent to `new(T)`.
This function is useful to use in auto-generated code, when the type of a variable is unknown. For example, if we have a variable `v` which we know to be a pointer, but we do not know the type of the pointer, we can use this function to leverage golang's type inference to create the new pointer.
func MakeSlice ¶
MakeSlice creates a new slice with the given length. It is equivalent to `make([]T, length)`.
This function is useful to use in auto-generated code, when the type of a variable is unknown. For example, if we have a variable `v` which we know to be a slice, but we do not know the type of the elements, we can use this function to leverage golang's type inference to create the new slice.
func ReadFint32 ¶
ReadFint32 reads a 32-bit fixed size integer from the reader.
func ReadFint64 ¶
ReadFint64 reads a 64-bit fixed size integer from the reader.
func ReadString ¶
ReadString reads a string from the reader. The string is verified to be valid UTF-8.
Types ¶
type Field ¶
type Field interface { // MarshalCanotoInto writes the field into a canoto.Writer and returns // the resulting canoto.Writer. // // It is assumed that CalculateCanotoCache has been called since the // last modification to this field. // // It is assumed that this field is ValidCanoto. MarshalCanotoInto(w Writer) Writer // CalculateCanotoCache populates internal caches based on the current // values in the struct. CalculateCanotoCache() // CachedCanotoSize returns the previously calculated size of the Canoto // representation from CalculateCanotoCache. // // If CalculateCanotoCache has not yet been called, or the field has // been modified since the last call to CalculateCanotoCache, the // returned size may be incorrect. CachedCanotoSize() int // UnmarshalCanotoFrom populates the field from a canoto.Reader. UnmarshalCanotoFrom(r Reader) error // ValidCanoto validates that the field can be correctly marshaled into // the Canoto format. ValidCanoto() bool }
Field defines a type that can be included inside of a Canoto message.
type FieldMaker ¶ added in v0.7.0
FieldMaker is a Field that can create a new value of type T.
The returned value must be able to be unmarshaled into.
This type can be used when implementing a generic Field. However, if T is an interface, it is possible for generated code to compile and panic at runtime.
type FieldPointer ¶ added in v0.4.0
FieldPointer is a pointer to a concrete Field value T.
This type must be used when implementing a value for a generic Field.
type Message ¶
type Message interface { Field // MarshalCanoto returns the Canoto representation of this message. // // It is assumed that this message is ValidCanoto. MarshalCanoto() []byte // UnmarshalCanoto unmarshals a Canoto-encoded byte slice into the message. UnmarshalCanoto(bytes []byte) error }
Message defines a type that can be a stand-alone Canoto message.
type Reader ¶
Reader contains all the state needed to unmarshal a Canoto type.
The functions in this package are not methods on the Reader type to enable the usage of generics.
Directories ¶
Path | Synopsis |
---|---|
Canoto is command to generate code for reading and writing the canoto format.
|
Canoto is command to generate code for reading and writing the canoto format. |
cli
module
|
|
Generate exposes functionality to generate code for reading and writing the canoto format.
|
Generate exposes functionality to generate code for reading and writing the canoto format. |
canoto
Canoto provides common functionality required for reading and writing the canoto format.
|
Canoto provides common functionality required for reading and writing the canoto format. |