Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gagliardetto/binary
Encoding/decoding in Borsh and other formats.
https://github.com/gagliardetto/binary
Last synced: about 1 month ago
JSON representation
Encoding/decoding in Borsh and other formats.
- Host: GitHub
- URL: https://github.com/gagliardetto/binary
- Owner: gagliardetto
- License: apache-2.0
- Created: 2021-08-08T13:53:02.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-07-16T03:30:20.000Z (5 months ago)
- Last Synced: 2024-11-10T16:52:15.339Z (about 2 months ago)
- Language: Go
- Homepage:
- Size: 310 KB
- Stars: 20
- Watchers: 4
- Forks: 11
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# binary
### Borsh
#### Decoding borsh
```golang
dec := bin.NewBorshDecoder(data)
var meta token_metadata.Metadata
err = dec.Decode(&meta)
if err != nil {
panic(err)
}
```#### Encoding borsh
```golang
buf := new(bytes.Buffer)
enc := bin.NewBorshEncoder(buf)
err := enc.Encode(meta)
if err != nil {
panic(err)
}
// fmt.Print(buf.Bytes())
```### Optional Types
```golang
type Person struct {
Name string
Age uint8 `bin:"optional"`
}
```Rust equivalent:
```rust
struct Person {
name: String,
age: Option
}
```### Enum Types
```golang
type MyEnum struct {
Enum bin.BorshEnum `borsh_enum:"true"`
One bin.EmptyVariant
Two uint32
Three int16
}
```Rust equivalent:
```rust
enum MyEnum {
One,
Two(u32),
Three(i16),
}
```### Exported vs Unexported Fields
In this example, the `two` field will be skipped by the encoder/decoder because the
field is not exported.
```golang
type MyStruct struct {
One string
two uint32
Three int16
}
```### Skip Decoding/Encoding Attributes
Encoding/Decoding of exported fields can be skipped using the `borsh_skip` tag.
```golang
type MyStruct struct {
One string
Two uint32 `borsh_skip:"true"`
Three int16
}
```