https://github.com/yomorun/y3
Golang implementation of Y3 Codec, a fast than real-time TLV based binary codec with low CPU usage
https://github.com/yomorun/y3
codec encoder-decoder golang realtime y3 yomo
Last synced: about 1 year ago
JSON representation
Golang implementation of Y3 Codec, a fast than real-time TLV based binary codec with low CPU usage
- Host: GitHub
- URL: https://github.com/yomorun/y3
- Owner: yomorun
- License: mit
- Created: 2021-08-06T13:18:23.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2025-02-24T03:21:58.000Z (over 1 year ago)
- Last Synced: 2025-04-27T11:44:19.248Z (about 1 year ago)
- Topics: codec, encoder-decoder, golang, realtime, y3, yomo
- Language: Go
- Homepage:
- Size: 28.3 KB
- Stars: 3
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
> 📚 VERSION: draft-01
> ⛳️ STATE: v1.0.0
# Y3
[](https://app.fossa.com/projects/git%2Bgithub.com%2Fyomorun%2Fy3?ref=badge_shield)
Y3 is the golang implementation of [Y3 Codec](https://github.com/yomorun/y3-codec), which describe a fast and low CPU binding data encoder/decoder focus on edge computing and streaming processing.
# Advantage
- Super fast encode/decode for streaming data
- Low CPU consumption when decoding large data
- Random access
- Tuned for QUIC protocol
- Designed for global communication at high frequency
## Y3 Codec Specification
See [Y3 Codec SPEC](https://github.com/yomorun/y3-codec)
## Test
`make test`
## Use
`go get -u github.com/yomorun/y3`
## Examples
### Encode examples
```go
package main
import (
"fmt"
y3 "github.com/yomorun/y3"
)
func main() {
// if we want to repesent `var obj = &foo{ID: -1, bar: &bar{Name: "C"}}`
// in Y3-Codec:
// 0x81 -> node
var foo = y3.NewNodePacketEncoder(0x01)
// 0x02 -> foo.ID=-11
var yp1 = y3.NewPrimitivePacketEncoder(0x02)
yp1.SetInt32Value(-1)
foo.AddPrimitivePacket(yp1)
// 0x83 -> &bar{}
var bar = y3.NewNodePacketEncoder(0x03)
// 0x04 -> bar.Name="C"
var yp2 = y3.NewPrimitivePacketEncoder(0x04)
yp2.SetStringValue("C")
bar.AddPrimitivePacket(yp2)
// -> foo.bar=&bar
foo.AddNodePacket(bar)
fmt.Printf("res=%#v", foo.Encode()) // res=[]byte{0x81, 0x08, 0x02, 0x01, 0x7F, 0x83, 0x03, 0x04, 0x01, 0x43}
}
```
### Decode examples 1: decode a primitive packet
```go
package main
import (
"fmt"
y3 "github.com/yomorun/y3"
)
func main() {
fmt.Println(">> Parsing [0x0A, 0x01, 0x7F], which like Key-Value format = 0x0A: 127")
buf := []byte{0x0A, 0x01, 0x7F}
res, _, err := y3.DecodePrimitivePacket(buf)
v1, err := res.ToUInt32()
if err != nil {
panic(err)
}
fmt.Printf("Tag Key=[%#X], Value=%v\n", res.SeqID(), v1)
}
```
### Decode examples 2: decode a node packet
```go
package main
import (
"fmt"
y3 "github.com/yomorun/y3"
)
func main() {
fmt.Println(">> Parsing [0x84, 0x06, 0x0A, 0x01, 0x7F, 0x0B, 0x01, 0x43] EQUALS JSON= 0x84: { 0x0A: -1, 0x0B: 'C' }")
buf := []byte{0x84, 0x06, 0x0A, 0x01, 0x7F, 0x0B, 0x01, 0x43}
res, _, err := y3.DecodeNodePacket(buf)
v1 := res.PrimitivePackets[0]
p1, err := v1.ToInt32()
if err != nil {
panic(err)
}
fmt.Printf("Tag Key=[%#X.%#X], Value=%v\n", res.SeqID(), v1.SeqID(), p1)
v2 := res.PrimitivePackets[1]
p2, err := v2.ToUTF8String()
if err != nil {
panic(err)
}
fmt.Printf("Tag Key=[%#X.%#X], Value=%v\n", res.SeqID(), v2.SeqID(), p2)
}
```
More examples in `/examples/`
## Types
Y3 implements the [YoMo Codec](https://github.com/yomorun/yomo-codec) protocol and supports the following Golang data types:
int32
```golang
````
uint32
```golang
````
int64
```golang
```
uint64
```golang
```
float32
```golang
```
float64
```golang
```
bool
```golang
```
string
```golang
```
bytes
```golang
buf := []byte("yomo")
p := NewPrimitivePacketEncoder(0x02)
p.SetBytesValue(buf)
res := p.Encode()
// res -> { 0x02, 0x04, 0x79, 0x6F, 0x6D, 0x6F }
```
## Contributors
[//]: contributor-faces
[//]: contributor-faces
## License
[](https://app.fossa.com/projects/git%2Bgithub.com%2Fyomorun%2Fy3?ref=badge_large)