protox

package module
v0.1.2-beta Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2025 License: ISC Imports: 5 Imported by: 0

README

Protox

Go library for (de-)serializing custom protocols

Usage

Use protox.New().<...>.Build() to define a processor that uses a specific format for writing/reading. You can call Read or Write methods on the resulting *protox.Processor to (de-)serialize data.

Here's an example using a struct, it uses all allowed variable formats:

import "github.com/bbfh-dev/protox"

type Example struct {
    Byte        byte
    BytesDelim  []byte
    String      string
    StringDelim string
    Int         int64
    IntDelim    int64
    FloatDelim  float64
    StringMap   map[string]string
    StringArr   []string
}

func (example *Example) Protox() *protox.Processor {
    return protox.New().
        ThenByte(&example.Byte).
        ThenBytesDelim(&example.BytesDelim, '\x00').
        ThenString(&example.String, 3).
        ThenStringDelim(&example.StringDelim, '\x00').
        ThenInt(&example.Int).
        ThenIntDelim(&example.IntDelim, '\x00').
        ThenFloatDelim(&example.FloatDelim, '\x00').
        ThenStringMap(example.StringMap, '=', '\x00', '\x00').
        ThenStringArray(&example.StringArr, ',', ';').
        Build()
}

func write(buffer *bytes.Buffer) {
    err := example.Protox().Write(buffer)
    fmt.Println(buffer.String()) // "gabc\x00hi!Hello World!\x009\x05\x00\x00\x00\x00\x00\x0042069\x00420.69\x00a=(1)\x00b=(2)\x00c=(3)\x00\x00"
}

func read(buffer *bytes.Buffer) {
    instance := &Example{}
    err := instance.Protox().Read(bufio.NewReader(buffer))
    fmt.Prinln(instance) // &protox_test.Example{Byte:0x67, BytesDelim:[]uint8{0x61, 0x62, 0x63}, String:"hi!", StringDelim:"Hello World!", Int:1337, IntDelim:42069, FloatDelim:420.69, StringMap:map[string]string{"a":"(1)", "b":"(2)", "c":"(3)"}}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

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

func New

func New() *Builder

func (*Builder) Build

func (build *Builder) Build() *Processor

Build the Protox processor

func (*Builder) ThenByte

func (builder *Builder) ThenByte(ref *byte) *Builder

func (*Builder) ThenBytesDelim

func (builder *Builder) ThenBytesDelim(value *[]byte, delim byte) *Builder

func (*Builder) ThenFloatDelim

func (builder *Builder) ThenFloatDelim(ref *float64, delim byte) *Builder

func (*Builder) ThenInt

func (builder *Builder) ThenInt(ref *int64) *Builder

func (*Builder) ThenIntDelim

func (builder *Builder) ThenIntDelim(ref *int64, delim byte) *Builder

func (*Builder) ThenString

func (builder *Builder) ThenString(ref *string, size int) *Builder

func (*Builder) ThenStringArray

func (builder *Builder) ThenStringArray(value *[]string, comma, delim byte) *Builder

func (*Builder) ThenStringDelim

func (builder *Builder) ThenStringDelim(ref *string, delim byte) *Builder

func (*Builder) ThenStringMap

func (builder *Builder) ThenStringMap(value map[string]string, sep, comma, delim byte) *Builder

type Processor

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

func (*Processor) ForceRead

func (processor *Processor) ForceRead(reader *bufio.Reader) error

Continues to read even if an error is encountered, returns all errors combined

func (*Processor) ForceWrite

func (processor *Processor) ForceWrite(writer io.Writer) error

Continues to write even if an error is encountered, returns all errors combined

func (*Processor) Read

func (processor *Processor) Read(reader *bufio.Reader) error

Returns on the first error encountered

func (*Processor) Write

func (processor *Processor) Write(writer io.Writer) error

Returns on the first error encountered

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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