This project is licensed under AGPL.
Encode- Takes aninterface{}and writes toio.Writer, returns an error if value is invalid.getType- Takes the type from areflect.Value, only used for interfaced values.structs- Takes areflect.Valueand a boolean if kind is required to write intoio.Writer, returns an error if value is either invalid or tag is not a number.
Decode- Takes aninterface{}and decodes fromio.Reader, returs an error if value is invalid or value is not settable orio.Readerread an invalid VarUint.getType- Decodes the type and heads towards decoding it, only used for interfaced values.ReadByte- Returns a byte and anio.EOFifio.Readis done.structs- Takes areflect.Valueand decodes each struct field, might return an error as the same forDecode.
Map- Returns a map representing the struct.Get- Returns the key and a status.As- Takes aninterface{}and sets what theinterface{}has, it will do nothing if the interface is not a struct.Sub- Takes a tag and aninterface{}and sets the interface with the value from the tag.maps- Takes a map and ranges through its values returning a readable map.fields- Takes a struct and map its fields by their tag, returnsmap[tag]fieldranges- Takesfieldsmap and check the values if possible to set, converting if required, if not continue.convert- Safe conversion forreflect.Value.ptr- Matches thereflect.Valueof the field.
Value- Takes aninterface{}and returnsreflect.Value, if interface is already return it back, else get the absolute value of theinterface{}.Zero- Initializes areflect.Value.Abs[T]- Gets absolutereflect.Typeorreflect.Value.
Marshal- Takesinterface{}and returns bytes, returns error as the same as Encoder.Unmarshal[T]- Takes[]byteand decodes into T, returns error as the same as Decoder.UnmarshalAs[T]- CombinesUnmarshal[T]andAs[T]calls.
Interface- Takes aninterface{}and returnsreflect.Value.interfaces- Takes areflect.Valueand switches tointerface{}whatever is needed, returns itself.As[T]- Takes aninterface{}and tries to decode into T, if so returns it.as2- Takes areflect.Typeandinterface{}being the head behindAs[T].KeyElem- Takes areflect.Valueexpecting arrays, slices and maps and returns its key and element types.
Integer- Integer is an interface with all integer types of Go also allowing other types with integers underlying it.VarIntIn[Integer]- Takes int and uint ranges and anio.Writerand returns bytes, error ifio.Writeris done.VarIntOut[Integer]- Must disclosure the type and takes anio.ByteReader, returns the number and an error ifio.Readerof Decoder is done.
depth- Takes areflect.Valuekind must be eitherreflect.Arrayorreflect.Sliceand calculates depth, mixed state and depth sizes.fromDepth- Builds a type from depth.isMixed` Check if all arrays are an arrays or slices are all slices, if so returns true.
package main
import (
"fmt"
"github.com/Dviih/bin"
"reflect"
)
type Example struct {
Twenty int `bin:"20"`
Fifty []int `bin:"50"`
Hundred string `bin:"100"`
}
func main() {
example := &Example{
Twenty: 20,
Fifty: []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))
}