Documentation
¶
Index ¶
- Constants
- func FromBase64(base64String string) ([]byte, error)
- func Marshal(v any) ([]byte, error)
- func MustMarshal(v any) []byte
- func ToBase64(bytes []byte) string
- func ULEB128Decode[T ULEB128SupportedTypes](r io.Reader) (T, int, error)
- func ULEB128Encode[T ULEB128SupportedTypes](input T) []byte
- func Unmarshal(data []byte, v any) (int, error)
- type Decoder
- type Encoder
- type Enum
- type Marshaler
- type Option
- type ULEB128SupportedTypes
- type Unmarshaler
Constants ¶
const MaxUleb128Length = 10
MaxUleb128Length is the max possible number of bytes for an ULEB128 encoded integer. Go's widest integer is uint64, so the length is 10.
const SuiAddressBytesName = "SuiAddressBytes"
Variables ¶
This section is empty.
Functions ¶
func FromBase64 ¶
func Marshal ¶ added in v1.0.7
Marshal a value into bcs bytes.
Many constructs supported by bcs don't exist in golang or move-lang.
- Enum is used to simulate the effects of rust enum.
- Use tag `optional` to indicate an optional value in rust. the field must be pointer or interface.
- Use tag `-` to ignore fields.
- Unexported fields are ignored.
Note that bcs doesn't have schema, and field names are irrelevant. The fields of struct are serialized in the order that they are defined.
Pointers are serialized as the type they point to. Nil pointers will be serialized as zero value of the type they point to unless it's marked as `optional`.
Arrays are serialized as fixed length vector (or serialize the each object individually without prefixing the length of the array).
Vanilla maps are not supported, however, the code will error if map is encountered to call out they are not supported and either ignore or provide a customized marshal function.
Channels, functions are silently ignored.
During marshalling process, how v is marshalled depends on if v implemented Marshaler or Enum
func MustMarshal ¶ added in v1.0.7
MustMarshal Marshal v, and panics if error.
func ULEB128Decode ¶ added in v1.0.7
func ULEB128Decode[T ULEB128SupportedTypes](r io.Reader) (T, int, error)
ULEB128Decode decodes io.Reader into an integer, returns the resulted value, the number of byte read, and a possible error.
binary.ReadUvarint is not used here because
- it doesn't support returning the number of bytes read.
- it accepts only io.ByteReader, but the recommended way of creating one from bufio.NewReader will read more than 1 byte at the to fill the buffer.
func ULEB128Encode ¶ added in v1.0.7
func ULEB128Encode[T ULEB128SupportedTypes](input T) []byte
ULEB128Encode converts an integer into []byte (see wikipedia and bcs)
This reuses binary.PutUvarint in standard library.
func Unmarshal ¶ added in v1.0.7
Unmarshal unmarshals the bcs serialized data into v.
Refer to notes in Marshal for details how data serialized/deserialized.
During the unmarshalling process
- if Unmarshaler, use "UnmarshalBCS" method.
- if not Unmarshaler but Enum, use the specialization for Enum.
- otherwise standard process.
Types ¶
type Decoder ¶ added in v1.0.7
type Decoder struct {
// contains filtered or unexported fields
}
Decoder takes an io.Reader and decodes value from it.
func NewDecoder ¶ added in v1.0.7
func (*Decoder) Decode ¶ added in v1.0.7
DecodeWithSize decodes a value from the decoder, and returns the number of bytes it consumed from the decoder.
- If the value is Unmarshaler, the corresponding UnmarshalBCS will be called.
- If the value is Enum, it will be special handled for Enum
type Encoder ¶ added in v1.0.7
type Encoder struct {
// contains filtered or unexported fields
}
Encoder takes an io.Writer and encodes value into it.
func NewEncoder ¶ added in v1.0.7
type Enum ¶ added in v1.0.7
type Enum interface {
// IsBcsEnum doesn't do anything. Its function is to indicate this is an enum for bcs de/serialization.
IsBcsEnum()
}
type Option ¶ added in v1.0.7
func (*Option[T]) MarshalBCS ¶ added in v1.0.7
type ULEB128SupportedTypes ¶ added in v1.0.7
type ULEB128SupportedTypes interface {
~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uint | ~int8 | ~int16 | ~int32 | ~int64 | ~int
}
ULEB128SupportedTypes is a constraint interface that limits the input to ULEB128Encode and ULEB128Decode to signed and unsigned integers.
type Unmarshaler ¶ added in v1.0.7
Unmarshaler customizes the unmarshalling behavior for a type.
Compared with other Unmarshalers in golang, the Unmarshaler here takes a io.Reader instead of []byte, since it is difficult to delimit the byte streams without unmarshalling. Method [UnmarshalBCS] returns the number of bytes read, and potentially an error.