https://github.com/markwinter/unpacket
Golang struct tags to pack and unpack bytes to structs
https://github.com/markwinter/unpacket
Last synced: 10 months ago
JSON representation
Golang struct tags to pack and unpack bytes to structs
- Host: GitHub
- URL: https://github.com/markwinter/unpacket
- Owner: markwinter
- License: mit
- Created: 2024-10-21T23:14:13.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-30T21:12:35.000Z (over 1 year ago)
- Last Synced: 2024-12-06T20:24:35.708Z (over 1 year ago)
- Language: Go
- Size: 8.79 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Unpacket
`Unpacket` provides functionality to unpack and pack byte slices into structs using an `unpacket` struct tag. This was mostly written to simplify
network protocol parsing e.g. https://github.com/markwinter/go-finproto
This currently only supports fixed-size packets/fields. Support for the final field to be variable length is incoming.
## Features
- Parse a byte array into a struct.
- Ability to pack a struct back into a byte array.
## Installation
To use `Unpacket`
```bash
go get github.com/markwinter/unpacket
```
## Usage
Add an `unpack` struct tag to your struct fields with `offset` and `length` tag fields.
The supported fields are variants of `int` `uint` `bool` `time.Duration` `string`
### Unpack a byte slice
```go
package main
import (
"fmt"
"log"
"github.com/markwinter/unpacket"
)
type EventCode uint8
type SystemEvent struct {
Timestamp time.Duration `unpack:"offset=5,length=6"`
StockLocate uint16 `unpack:"offset=1,length=2"`
TrackingNumber uint16 `unpack:"offset=3,length=2"`
EventCode EventCode `unpack:"offset=11,length=1"`
}
func main() {
data := []byte{83,0,0,0,0,0,162,215,245,12,16,83}
systemEvent := &SystemEvent{}
err := unpacket.Unpack(data, binary.BigEndian, systemEvent)
if err != nil {
log.Fatalf("failed unpacking data: %w", err)
}
fmt.Printf("%+v", systemEvent)
// &{Timestamp:11m39.4078628s StockLocate:0 TrackingNumber:0 EventCode:83}
}
```
### Pack a struct into a byte slice
```go
package main
import (
"fmt"
"log"
"github.com/markwinter/unpacket"
)
type EventCode uint8
type SystemEvent struct {
Timestamp time.Duration `unpack:"offset=5,length=6"`
StockLocate uint16 `unpack:"offset=1,length=2"`
TrackingNumber uint16 `unpack:"offset=3,length=2"`
EventCode EventCode `unpack:"offset=11,length=1"`
}
func main() {
systemEvent := &SystemEvent{
// ...
}
data, err := unpacket.Pack(binary.BigEndian, systemEvent)
if err != nil {
log.Fatalf("failed packing data: %w", err)
}
fmt.Printf("%+v", data)
// [0 0 0 0 0 0 162 215 245 12 16 83]
}
```