bytecodec

package module
v0.0.0-...-9d31c74 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2024 License: Apache-2.0 Imports: 9 Imported by: 5

README

Shimmering Bee: Byte Codec

license standard-readme compliant Actions Status

Implementation of a byte codec to Marshal/Unmarshal structs to []byte, compatible with Zigbee types, written in Go.

Table of Contents

Background

bytecodec is a library to marshal and unmarshal Go structs to a []byte, for transmission on the wire. Compatible with Zigbee types, such as 24 bit integers.

Install

Add an import and most IDEs will go get automatically, if it doesn't go build will fetch.

import "github.com/shimmeringbee/bytecodec"

Usage

This libraries API is unstable and should not yet be relied upon.

Marshalling

Marshalling assumes integers should be expressed as little endian, unless overridden.

Currently supports:

  • uint8, uint16, uint32, uint64
  • struct
  • array/slice
  • string (null terminated and length prefixed)
type StructToMarshal struct {
    ByteField              byte
    ArrayOfUint16BigEndian []uint16 `bcendian:"big"`
    Uint16LittleEndian     uint16
}

data := &StructToMarshal{
    ByteField:              0x55,
    ArrayOfUint16BigEndian: []uint16{0x8001, 0x1234},
    Uint16LittleEndian:     0x2233,
}

bytes, err := bytecodec.Marshal(data)

if err != nil {
    // Handle Error
}

// bytes = []byte{0x55,0x80,0x01,0x12,0x34,0x33,0x22}

Maintainers

@pwood

Contributing

Feel free to dive in! Open an issue or submit PRs.

All Shimmering Bee projects follow the Contributor Covenant Code of Conduct.

License

Copyright 2019-2020 Shimmering Bee Contributors

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Documentation

Index

Constants

View Source
const (
	Prefix StringTermination = 0
	Null   StringTermination = 1

	TagEndian      = "bcendian"
	TagSlicePrefix = "bcsliceprefix"
	TagStringType  = "bcstringtype"
	TagIncludeIf   = "bcincludeif"
	TagFieldWidth  = "bcfieldwidth"

	BigEndianKeyword       = "big"
	NullTerminationKeyword = "null"
)

Variables

View Source
var ErrUnsupportedType = errors.New("unsupported type")
View Source
var IncludeIfRegex = regexp.MustCompile(`^([a-zA-Z0-9.]+)(!=|==)?(.*)$`)

Functions

func Marshal

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

func MarshalToBitBuffer

func MarshalToBitBuffer(bb *bitbuffer.BitBuffer, v interface{}) error

func Unmarshal

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

func UnmarshalFromBitBuffer

func UnmarshalFromBitBuffer(bb *bitbuffer.BitBuffer, v interface{}) (err error)

Types

type Context

type Context struct {
	Root         reflect.Value
	CurrentIndex int
}

type FieldWidthTag

type FieldWidthTag struct {
	Default  bool
	BitWidth int
}

func (FieldWidthTag) Width

func (t FieldWidthTag) Width(defaultWidth int) int

type IncludeIfOperation

type IncludeIfOperation uint8
const (
	Equal    IncludeIfOperation = 0x00
	NotEqual IncludeIfOperation = 0x01
)

type IncludeIfTag

type IncludeIfTag struct {
	Relative  bool
	FieldPath []string

	Operation IncludeIfOperation

	Value string
}

func (IncludeIfTag) HasIncludeIf

func (i IncludeIfTag) HasIncludeIf() bool

type Marshaler

type Marshaler interface {
	Marshal(*bitbuffer.BitBuffer, Context) error
}

type SlicePrefixTag

type SlicePrefixTag struct {
	Size   uint8
	Endian bitbuffer.Endian
}

func (SlicePrefixTag) HasPrefix

func (l SlicePrefixTag) HasPrefix() bool

type StringTermination

type StringTermination uint8

type StringTypeTag

type StringTypeTag struct {
	Termination StringTermination
	Size        uint8
	Endian      bitbuffer.Endian
}

type Unmarshaler

type Unmarshaler interface {
	Unmarshal(*bitbuffer.BitBuffer, Context) error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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