dataconv

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2025 License: GPL-3.0 Imports: 9 Imported by: 0

README

dataconv

Helpers to convert data to and from binary format. Example:

// we want to encode some data into binary format
b := NewBinaryEncoder()
testutils.Equal(t, b.Encode(uint8(1)), nil) // convert uint8: 1 to binary, with offset 0, return new offset
testutils.Equal(t, b.Encode(uint16(1245)), nil) // convert uin16: 1235 to binary by given offset, and return new offset
testutils.Equal(t, b.Encode(uint32(1245678)), nil)
testutils.Equal(t, b.Encode(uint64(124567891011)), nil)
testutils.Equal(t, b.Encode([]byte("test")), nil)
testutils.Equal(t, b.Encode(12.56), nil)
data := b.Bytes() // here we should have filled data with different values

// that how we can take data back:
d := NewBinaryDecoder(data)
var tuint8 uint8
err := d.Decode(&tuint8)
testutils.Equal(t, err, nil)
testutils.Equal(t, tuint8, uint8(1))
var tuint16 uint16
err = d.Decode(&tuint16)
testutils.Equal(t, err, nil)
testutils.Equal(t, tuint16, uint16(1245))
var tuint32 uint32
err = d.Decode(&tuint32)
testutils.Equal(t, err, nil)
testutils.Equal(t, tuint32, uint32(1245678))
var tuint64 uint64
err = d.Decode(&tuint64)
testutils.Equal(t, err, nil)
testutils.Equal(t, tuint64, uint64(124567891011))
var tbytes []byte
err = d.Decode(&tbytes)
testutils.Equal(t, err, nil)
testutils.Equal(t, tbytes, []byte("test"))
var tfloat64 float64
err = d.Decode(&tfloat64)
testutils.Equal(t, err, nil)
testutils.Equal(t, tfloat64, float64(12.56))

Documentation

Index

Constants

View Source
const (
	ArrayLenForIP = 8
	LowPosition   = 8
	HighPosition  = 16

	IPV6Prefix = "::ffff:"

	IPBytesCut = 15 // 15 bytes (120 bits)
)

Variables

View Source
var (
	ErrWrongEncodeType = errors.New("error wrong encode type")
	ErrWrongDecodeType = errors.New("error wrong decode type")
)

All kind of errors

View Source
var (
	ErrInvalidIPAddress = errors.New("invalid ip address")
	ErrNotIPv4Address   = errors.New("not an IPv4 address")
	ErrNotIPv6Address   = errors.New("not an IPv6 address")
)

Functions

func BigIntToIPv6

func BigIntToIPv6(ipaddr big.Int) net.IP

BigIntToIPv6 converts IP address of version 6 from big integer to net.IP representation.

func CutIP

func CutIP(ip string) string

func DecodeBool

func DecodeBool(buffer *bytes.Buffer) bool

DecodeBool decode to bool

func DecodeBuffer

func DecodeBuffer(buffer *bytes.Buffer) *bytes.Buffer

DecodeBuffer decode to buffer

func DecodeBytes

func DecodeBytes(buffer *bytes.Buffer) []byte

DecodeBytes decode to bytes

func DecodeDecodable

func DecodeDecodable(decodable Decodable, buffer *bytes.Buffer) any

DecodeDecodable decode specific structure

func DecodeFloat32

func DecodeFloat32(buffer *bytes.Buffer) float32

DecodeFloat32 decode to float32

func DecodeFloat64

func DecodeFloat64(buffer *bytes.Buffer) float64

DecodeFloat64 decode to float64

func DecodeInt

func DecodeInt(buffer *bytes.Buffer) int

DecodeInt decode to int

func DecodeInt16

func DecodeInt16(buffer *bytes.Buffer) int16

DecodeInt16 decode to int16

func DecodeInt32

func DecodeInt32(buffer *bytes.Buffer) int32

DecodeInt32 decode to int32

func DecodeInt64

func DecodeInt64(buffer *bytes.Buffer) int64

DecodeInt64 decode to int64

func DecodeInt8

func DecodeInt8(buffer *bytes.Buffer) int8

DecodeInt8 decode to int8

func DecodeString

func DecodeString(buffer *bytes.Buffer) string

DecodeString decode to string

func DecodeUint16

func DecodeUint16(buffer *bytes.Buffer) uint16

DecodeUint16 decode to uint16

func DecodeUint32

func DecodeUint32(buffer *bytes.Buffer) uint32

DecodeUint32 decode to uint32

func DecodeUint64

func DecodeUint64(buffer *bytes.Buffer) uint64

DecodeUint64 decode to uint64

func DecodeUint8

func DecodeUint8(buffer *bytes.Buffer) uint8

DecodeUint8 decode to uint8

func EncodeBool

func EncodeBool(v bool, buffer *bytes.Buffer)

EncodeBool encode bool

func EncodeBuffer

func EncodeBuffer(v *bytes.Buffer, buffer *bytes.Buffer)

EncodeBuffer encode buffer

func EncodeBytes

func EncodeBytes(v []byte, buffer *bytes.Buffer)

EncodeBytes encode bytes

func EncodeEncodeable

func EncodeEncodeable(v Encodeable, buffer *bytes.Buffer)

EncodeEncodeable encode Encodeable

func EncodeFloat32

func EncodeFloat32(v float32, buffer *bytes.Buffer)

EncodeFloat32 encode float32

func EncodeFloat64

func EncodeFloat64(v float64, buffer *bytes.Buffer)

EncodeFloat64 encode float64

func EncodeInt

func EncodeInt(v int, buffer *bytes.Buffer)

EncodeInt encode int

func EncodeInt16

func EncodeInt16(v int16, buffer *bytes.Buffer)

EncodeInt16 encode int16

func EncodeInt32

func EncodeInt32(v int32, buffer *bytes.Buffer)

EncodeInt32 encode int32

func EncodeInt64

func EncodeInt64(v int64, buffer *bytes.Buffer)

EncodeInt64 encode int64

func EncodeInt8

func EncodeInt8(v int8, buffer *bytes.Buffer)

EncodeInt8 encode int8

func EncodeString

func EncodeString(v string, buffer *bytes.Buffer)

EncodeString encode string

func EncodeUint16

func EncodeUint16(v uint16, buffer *bytes.Buffer)

EncodeUint16 encode uint16

func EncodeUint32

func EncodeUint32(v uint32, buffer *bytes.Buffer)

EncodeUint32 encode uint32

func EncodeUint64

func EncodeUint64(v uint64, buffer *bytes.Buffer)

EncodeUint64 encode uint64

func EncodeUint8

func EncodeUint8(v uint8, buffer *bytes.Buffer)

EncodeUint8 encode uint8

func IP2Int

func IP2Int(ip net.IP) *big.Int

IP2Int convert any net.IP to uint64

func IPV4ToIPV6

func IPV4ToIPV6(ipv4 string) string

func IPV6ToString

func IPV6ToString(ipv6 *big.Int) string

func IPv4ToInt

func IPv4ToInt(ipaddr net.IP) (uint32, error)

IPv4ToInt converts IP address of version 4 from net.IP to uint32 representation.

func IPv6ToBigInt

func IPv6ToBigInt(ipaddr net.IP) *big.Int

IPv6ToBigInt converts IP address of version 6 from net.IP to math big integer representation.

func IPv6ToInt

func IPv6ToInt(ipaddr net.IP) ([2]uint64, error)

IPv6ToInt converts IP address of version 6 from net.IP to uint64 array representation. Return value contains high integer value on the first place and low integer value on second place.

func IntToIPv4

func IntToIPv4(ipaddr uint32) net.IP

IntToIPv4 converts IP address of version 4 from integer to net.IP representation.

func IntToIPv6

func IntToIPv6(high, low uint64) net.IP

IntToIPv6 converts IP address of version 6 from integer (high and low value) to net.IP representation.

func MergeStruct

func MergeStruct(record1 interface{}, record2 interface{}) error

MergeStruct help to fill all existing fields of record1 by record2

func ParseIP

func ParseIP(s string) (net.IP, int, error)

ParseIP implements extension of net.ParseIP. It returns additional information about IP address bytes length. In general, it works typically as standard net.ParseIP. So if IP is not valid, nil is returned.

Types

type BinaryDecoder

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

BinaryDecoder contains buffer and convert binary to value

func NewBinaryDecoder

func NewBinaryDecoder(data []byte) *BinaryDecoder

NewBinaryDecoder return new decoder

func (*BinaryDecoder) Bytes

func (b *BinaryDecoder) Bytes() []byte

Bytes return bytes

func (*BinaryDecoder) Decode

func (b *BinaryDecoder) Decode(value any) error

Decode read value to bytes

type BinaryEncoder

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

BinaryEncoder contains buffer and convert values to binary

func NewBinaryEncoder

func NewBinaryEncoder() *BinaryEncoder

NewBinaryEncoder return new encoder

func (*BinaryEncoder) Bytes

func (b *BinaryEncoder) Bytes() []byte

Bytes return bytes

func (*BinaryEncoder) Encode

func (b *BinaryEncoder) Encode(value any) error

Encode add value to bytes

type Decodable

type Decodable interface {
	Decode(uint8, []byte) any
}

Decodable describe decodable interface

type Encodeable

type Encodeable interface {
	Encode() (uint8, []byte)
}

Encodeable describe encodeable interface

Jump to

Keyboard shortcuts

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