Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Marshal ¶
Example (Libra_program) ¶
package main import ( "fmt" "github.com/the729/lcs" ) type TransactionArgument interface{} type Program struct { Code []byte Args []TransactionArgument Modules [][]byte } func main() { prog := &Program{ Code: []byte("move"), Args: []TransactionArgument{ "CAFE D00D", "cafe d00d", }, Modules: [][]byte{{0xca}, {0xfe, 0xd0}, {0x0d}}, } bytes, err := lcs.Marshal(prog) if err != nil { panic(err) } fmt.Printf("%X\n", bytes) }
Output: 046D6F766502020943414645204430304402096361666520643030640301CA02FED0010D
Example (Struct) ¶
package main import ( "fmt" "github.com/the729/lcs" ) func main() { type MyStruct struct { Boolean bool Bytes []byte Label string unexported uint32 } type Wrapper struct { Inner *MyStruct `lcs:"optional"` Name string } bytes, err := lcs.Marshal(&Wrapper{ Name: "test", Inner: &MyStruct{ Bytes: []byte{0x01, 0x02, 0x03, 0x04}, Label: "hello", }, }) if err != nil { panic(err) } fmt.Printf("%x", bytes) }
Output: 010004010203040568656c6c6f0474657374
func RegisterEnum ¶ added in v0.1.5
func RegisterEnum(enumTypePtr interface{}, types ...interface{}) (err error)
RegisterEnum register an enum type with its available variants. If the enum type was registered, it will be overwriten.
This function panics on errors. The returned error is always nil.
func Unmarshal ¶
Example (Libra_program) ¶
package main import ( "encoding/hex" "fmt" "github.com/the729/lcs" ) type TransactionArgument interface{} type Program struct { Code []byte Args []TransactionArgument Modules [][]byte } func main() { bytes, _ := hex.DecodeString("046D6F766502020943414645204430304402096361666520643030640301CA02FED0010D") out := &Program{} err := lcs.Unmarshal(bytes, out) if err != nil { panic(err) } fmt.Printf("%+v\n", out) }
Output: &{Code:[109 111 118 101] Args:[CAFE D00D cafe d00d] Modules:[[202] [254 208] [13]]}
Example (Struct) ¶
package main import ( "encoding/hex" "fmt" "github.com/the729/lcs" ) func main() { type MyStruct struct { Boolean bool Bytes []byte Label string unexported uint32 } type Wrapper struct { Inner *MyStruct `lcs:"optional"` Name string } bytes, _ := hex.DecodeString("010004010203040568656c6c6f0474657374") out := &Wrapper{} err := lcs.Unmarshal(bytes, out) if err != nil { panic(err) } fmt.Printf("Name: %s, Label: %s\n", out.Name, out.Inner.Label) }
Output: Name: test, Label: hello
Types ¶
type EnumKeyType ¶ added in v0.1.4
type EnumKeyType = uint64
type EnumTypeUser ¶
type EnumTypeUser interface { // EnumTypes return the ingredients used for all enum types in the struct. EnumTypes() []EnumVariant }
EnumTypeUser is an interface of struct with enum type definition. Struct with enum type should implement this interface.
type EnumVariant ¶
type EnumVariant struct { // Name of the enum type. Different variants of a same enum type should have same name. // This name should match the name defined in the struct field tag. Name string // Value is the numeric value of the enum variant. Should be unique within the same enum type. Value EnumKeyType // Template object for the enum variant. Should be the zero value of the variant type. // // Example values: (*SomeStruct)(nil), MyUint32(0). Template interface{} }
EnumVariant is a definition of a variant of enum type.
Click to show internal directories.
Click to hide internal directories.