bidv

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2024 License: MIT Imports: 2 Imported by: 0

README

benc idv

The Benc ID Validation (Benc IDV) provides a suite of methods for prefixing Benc standard's raw size, marshaling, and unmarshaling with an ID. When referring to "prefixing with ID," it means that the marshaled Go type is prefixed with a provided ID of any size. Upon unmarshaling, this ID is then checked against the deserialized ID.

Installation

go get github.com/deneonet/benc/idv

Tests

The code coverage of bidv.go is 100% (~97% with uncalled panics).

Usage

Benc IDV provides four primary functions:

  • Skip: Skips the ID (+ validates it) and the requested type.
  • Size: Adds the needed size for id and returns s plus the calculated ID size.
  • Marshal: Marshals id into the buffer at a given offset n.
  • Unmarshal: Unmarshals and validates the deserialized ID, then unmarshals the requested type.

Example

Marshaling and Unmarshalling a string with the ID of 1:
See bstd examples

package main

import (
	"fmt"
	"github.com/deneonet/benc/idv"
	"github.com/deneonet/benc/std"
)

func main() {
	var id uint64 = 1
	mystr := "My string"

	// Calculate the size needed
	s := bidv.Size(id, bstd.SizeString(mystr))

	// Create buffer
	buf := make([]byte, s)

	// Marshal ID into buffer
	n := bidv.Marshal(0, buf, id)

	// Marshal string into buffer
	_ = bstd.MarshalString(n, buf, mystr)

	// Unmarshal ID and string
	_, deserMyStr, err := bidv.Unmarshal[string](0, buf, id, bstd.UnmarshalString)
	if err != nil {
		panic(err)
	}
	if mystr != deserMyStr {
		panic("no match")
	}

	// Success
	fmt.Println("Marshaling and unmarshaling successful:", deserMyStr)
}

Documentation

Index

Constants

View Source
const (
	Int16 uint = iota + 2
	Int32
	Int64
	UInt16
	UInt32
	UInt64
	Float32
	Float64
	Bool
	Byte
	String
	Slice
	Map
	ByteSlice
)
View Source
const AllowedStartId = 16

Variables

View Source
var GetIdNickname = GetDefaultIdNickname

Functions

func GetDefaultIdNickname

func GetDefaultIdNickname(id uint) string

Returns the nickname for the standard IDs for all data types

func Marshal

func Marshal(n int, b []byte, id uint) int

func Size

func Size(id uint, s int) int

func Skip

func Skip(n int, b []byte, id uint, skipper SkipFunc) (int, error)

func Unmarshal

func Unmarshal[T any](tn int, b []byte, id uint, unmarshaler any) (n int, t T, err error)

Types

type MarshalFunc

type MarshalFunc[T any] func(n int, b []byte, t T) int

type SkipFunc

type SkipFunc func(n int, b []byte) (int, error)

Jump to

Keyboard shortcuts

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