Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/raphexion/gustav
Small helper library for and in Erlang
https://github.com/raphexion/gustav
binary-data erlang erlang-library helpers serialization
Last synced: 23 days ago
JSON representation
Small helper library for and in Erlang
- Host: GitHub
- URL: https://github.com/raphexion/gustav
- Owner: Raphexion
- License: apache-2.0
- Created: 2018-06-14T17:41:21.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-04-23T17:40:03.000Z (over 5 years ago)
- Last Synced: 2024-12-09T03:39:54.259Z (about 1 month ago)
- Topics: binary-data, erlang, erlang-library, helpers, serialization
- Language: Erlang
- Homepage:
- Size: 36.1 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
gustav
=====[![Build Status](https://travis-ci.org/Raphexion/gustav.svg?branch=master)](https://travis-ci.org/Raphexion/gustav)
[![codecov.io](https://codecov.io/gh/Raphexion/gustav/coverage.svg?branch=master)](https://codecov.io/gh/Raphexion/gustav?branch=master)A helper library for two use-cases:
1. To help pack-up and un-pack data
2. To help with writing a udp-serverExamples
--------Imagine that you have a state `#{a => 1, b => 2, c => 3, d => 4}`.
You need to pack-up this data (serialize) and send it over the wire.
Your protocol specifies that:| Variable | Bits in payload |
|----------|-----------------|
| a | 8 |
| b | 16 |
| c | 32 |
| d | 8 |Which can be described compactly as `[{a, 8}, {b, 16}, {c, 32}, {d, 8}]`.
When using *Gustav*, packing up the dictionary into a binary payload:
```
Dictionary = #{a => 1, b => 2, c => 3, d => 4},
Packer = packer(Dictionary),
Payload = Packer([{a, 8}, {b, 16}, {c, 32}, {d, 8}]),
Payload.
<<1:8, 2:16, 3:32, 4:8>>
```To unpack the binary payload is similar.
We describe the binary protocol `[{a, 8}, {b, 16}, {c, 32}, {d, 8}]`
and use a unpacker function.Normally we only want to unpack the data into an empty map `#{}`.
However, in some cases it is convenient to allow some "other" data
in the map (state).Using *Gustav*, to unpacking a binary payload into a dictionary:
```
Payload = <<1:8, 2:16, 3:32, 4:8>>,
UnPacker = unpacker(#{}, Payload),
NewDictionary = UnPacker([{a, 8}, {b, 16}, {c, 32}, {d, 8}]),
NewDictionary.
#{a => 1, b => 2, c => 3, d => 4}
```Or if you already have some data you want to preserve `#{q => 42}`:
```
OrgDictionary = #{q => 42} %% q => 42 is mapping we want to
Payload = <<1:8, 2:16, 3:32, 4:8>>,
UnPacker = unpacker(OrgDictionary, Payload),
NewDictionary = UnPacker([{a, 8}, {b, 16}, {c, 32}, {d, 8}]),
NewDictionary.
#{a => 1, b => 2, c => 3, d => 4, q => 42}
```Build
-----$ rebar3 compile