Skip to content

gagliardetto/binary

Folders and files

NameName
Last commit message
Last commit date

Latest commit

79f49c5 · Apr 1, 2024
Aug 20, 2022
Nov 12, 2020
Nov 20, 2020
Nov 20, 2020
Apr 7, 2022
Dec 11, 2022
Apr 1, 2024
Apr 1, 2024
Jun 26, 2023
Aug 22, 2022
Oct 22, 2022
Oct 22, 2022
Oct 22, 2022
Sep 18, 2022
Jun 26, 2023
Oct 22, 2022
Jun 26, 2023
Oct 22, 2022
Dec 11, 2022
Oct 7, 2021
Jun 26, 2023
Jun 26, 2023
Oct 30, 2022
Oct 30, 2022
Jun 26, 2023
Oct 7, 2021
Aug 28, 2022
Aug 20, 2022
Jun 26, 2023
Oct 22, 2022
Oct 30, 2022
Oct 30, 2022
Oct 22, 2022
Oct 22, 2022
Oct 29, 2022
Aug 20, 2022
Jan 31, 2022
Feb 1, 2022
Aug 28, 2022
Oct 7, 2021
Oct 7, 2021
Dec 11, 2022
Jan 5, 2022

Repository files navigation

binary

Borsh

Decoding borsh

 dec := bin.NewBorshDecoder(data)
 var meta token_metadata.Metadata
 err = dec.Decode(&meta)
 if err != nil {
   panic(err)
 }

Encoding borsh

buf := new(bytes.Buffer)
enc := bin.NewBorshEncoder(buf)
err := enc.Encode(meta)
if err != nil {
  panic(err)
}
// fmt.Print(buf.Bytes())

Optional Types

type Person struct {
	Name string
	Age  uint8 `bin:"optional"`
}

Rust equivalent:

struct Person {
    name: String,
    age: Option<u8>
}

Enum Types

type MyEnum struct {
	Enum  bin.BorshEnum `borsh_enum:"true"`
	One   bin.EmptyVariant
	Two   uint32
	Three int16
}

Rust equivalent:

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.

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.

type MyStruct struct {
	One   string
	Two   uint32 `borsh_skip:"true"`
	Three int16
}