bin

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2024 License: AGPL-3.0 Imports: 7 Imported by: 0

README

Bin

A format for binary data, like protobuf


Encoder

This implements encoding and takes an io.Writer.

Decoder

This implements decoding and takes an io.Reader.

Struct

A map where it can translate into a struct with the help of the tag.

Usage

Encoder

  • Encode - Takes an interface{} and writes to io.Writer, returns an error if value is invalid.
  • _struct - Takes a reflect.Value and a boolean if kind is required to write into io.Writer, returns an error if value is either invalid or tag is not a number.

Decoder

  • Decode - Takes an interface{} and decodes from io.Reader, returs an error if value is invalid or value is not settable or io.Reader read an invalid VarUint.
  • ReadByte - Returns a byte and an io.EOF if io.Read is done.
  • _struct - Takes a reflect.Value and decodes each struct field, might return an error as the same for Decode.

Struct

  • Map - Returns a map representing the struct.
  • Get - Returns the key and a status.
  • As - Takes an interface{} and sets what the interface{} has, it will do nothing if the interface is not a struct.
  • Sub - Takes a tag and an interface{} and sets the interface with the value from the tag.
  • _map - Takes an interface{} of a map and deference it, returns map[interface{}]interface{}
  • fields - Takes a struct and map its fields by their tag, returns map[tag]field
  • rangeFields - Takes fields map and check the values if possible to set, converting if required, if not continue.
  • ptr - Matches the reflect.Value of the field.

Utilities

  • Value - Takes an interface{} and returns reflect.Value, if interface is already return it back, else get the absolute value of the interface{}.
  • Zero - Initializes a reflect.Value.
  • Abs[T] - Gets absolute reflect.Type or reflect.Value.
  • Marshal - Takes interface{} and returns bytes, returns error as the same as Encoder.
  • Unmarshal[T] - Takes []byte and decodes into T, returns error as the same as Decoder.
  • Interface - Takes an interface{} and returns reflect.Value.
  • VarIntIn[T] - Takes int and uint ranges and an io.Writer and returns bytes, error if io.Writer is done.
  • VarIntOut - Must disclosure the type and takes an io.ByteReader, returns the number and an error if io.Reader of Decoder is done.
  • typeFromKind - Important for decoding, if not a basic type returns nil.
  • _interface - Takes a reflect.Value and switches to interface{} whatever is needed, returns itself.

Example

package main

import (
	"fmt"
	"github.com/Dviih/bin"
	"reflect"
)

type Example struct {
	Twenty  int    `bin:"20"`
	Fifth   []int  `bin:"50"`
	Hundred string `bin:"100"`
}

func main() {
	example := &Example{
		Twenty:  20,
		Fifth:   []int{5, 0},
		Hundred: "A hundred",
	}

	data, err := bin.Marshal(example)
	if err != nil {
		panic(err)
	}

	fmt.Println(data)

	example2, err := bin.Unmarshal[*Example](data)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%+v\n", example2)
	fmt.Println("Equal", reflect.DeepEqual(example, example2))
}
More can be found at bin_test.go, encoder_test.go and decoder_test.go

Made for Gophers by @Dviih

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Invalid = errors.New("invalid value")
	CantSet = errors.New("can't set")
)

Functions

func Abs

func Abs[T interface{}](t interface{}) T

func Interface

func Interface(v interface{}) reflect.Value

func Marshal

func Marshal(v interface{}) ([]byte, error)

func Unmarshal

func Unmarshal[T interface{}](data []byte) (T, error)

func Value

func Value(v interface{}) reflect.Value

func VarIntIn

func VarIntIn[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64](writer io.Writer, t T) error

func VarIntOut

func VarIntOut[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64](reader io.ByteReader) (T, error)

func Zero

func Zero(value reflect.Value)

Types

type Decoder

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

func NewDecoder

func NewDecoder(reader io.Reader) *Decoder

func (*Decoder) Decode

func (decoder *Decoder) Decode(v interface{}) error

func (*Decoder) ReadByte

func (decoder *Decoder) ReadByte() (byte, error)

type Encoder

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

func NewEncoder

func NewEncoder(writer io.Writer) *Encoder

func (*Encoder) Encode

func (encoder *Encoder) Encode(v interface{}) error

type Struct

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

Struct represents any struct.

func (*Struct) As

func (_struct *Struct) As(v interface{})

func (*Struct) Get

func (_struct *Struct) Get(i int) (interface{}, bool)

func (*Struct) Map

func (_struct *Struct) Map() map[interface{}]interface{}

func (*Struct) Sub

func (_struct *Struct) Sub(i int, v interface{})

Jump to

Keyboard shortcuts

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