Skip to content
This repository has been archived by the owner on Jan 3, 2023. It is now read-only.

Commit

Permalink
update readme and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
the729 committed Apr 18, 2020
1 parent 6387fd4 commit 3369f23
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 32 deletions.
28 changes: 9 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![Go Report Card](https://goreportcard.com/badge/github.com/the729/lcs)](https://goreportcard.com/report/github.com/the729/lcs)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/a70c457b8b7d44c0b69460b2a8704365)](https://www.codacy.com/app/the729/lcs?utm_source=github.com&utm_medium=referral&utm_content=the729/lcs&utm_campaign=Badge_Grade)

Go library for Libra canonical serialization (and deserialization). See [LCS Spec](https://github.com/libra/libra/tree/master/common/canonical_serialization).
Go library for Libra canonical serialization (and deserialization). See [LCS Spec](https://github.com/libra/libra/tree/6a89e827b95405066dc83eec97eca2cb75bc991d/common/canonical-serialization).

For types defined and used in actual Libra blockchain, please visit [go-libra](https://github.com/the729/go-libra): Libra client library with crypto verifications.

Expand Down Expand Up @@ -103,46 +103,36 @@ Enum types are golang interfaces.
```golang
// Enum1 is an enum type.
type Enum1 interface {
isEnum1() // optional: a dummy function to identify its variants.
// isEnum1() // optional: member functions
}

// *Enum1Opt0, Enum1Opt1, Enum1Opt2, Enum1Opt3 are variants of Enum1
// Use pointer for non-empty struct.
// Use empty struct for a variant without contents.
// *Enum1Opt0, Enum1Opt1, Enum1Opt2 are variants of Enum1
type Enum1Opt0 struct {
Data uint32
}
type Enum1Opt1 bool
type Enum1Opt2 []byte
type Enum1Opt3 []Enum1 // self reference is OK

// Variants should implement Enum1
func (*Enum1Opt0) isEnum1() {}
func (Enum1Opt1) isEnum1() {}
func (Enum1Opt2) isEnum1() {}
func (Enum1Opt3) isEnum1() {}
type Enum1Opt1 struct{} // Use empty struct for a variant without contents.
type Enum1Opt2 []Enum1 // self reference is OK

// Register Enum1 with LCS. Will be available globaly.
var _ = lcs.RegisterEnum(
// nil pointer to the enum interface type:
(*Enum1)(nil),
// zero-values of each variants
(*Enum1Opt0)(nil),
Enum1Opt1(false),
(*Enum1Opt0)(nil), // Use pointer for non-empty struct.
Enum1Opt1{},
Enum1Opt2(nil),
Enum1Opt3(nil),
)

// Usage: Marshal the enum alone, must use pointer
e1 := Enum1(Enum1Opt1(true))
e1 := Enum1(Enum1Opt1{})
bytes, err := lcs.Marshal(&e1)

// Use Enum1 within other structs
type Wrapper struct {
Enum Enum1
}
bytes, err := lcs.Marshal(&Wrapper{
Enum: Enum1Opt1(true),
Enum: Enum1Opt0{10},
})

```
17 changes: 4 additions & 13 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,14 @@ func ExampleUnmarshal_struct() {
// Output: Name: test, Label: hello
}

type TransactionArgument interface {
isTransactionArg()
}
type TxnArgU64 uint64
type TxnArgAddress [32]byte
type TxnArgString string

func (TxnArgU64) isTransactionArg() {}
func (TxnArgAddress) isTransactionArg() {}
func (TxnArgString) isTransactionArg() {}
type TransactionArgument interface{}

// Register TransactionArgument with LCS. Will be available globaly.
var _ = lcs.RegisterEnum(
// pointer to enum interface:
(*TransactionArgument)(nil),
// zero-value of variants:
TxnArgU64(0), TxnArgAddress([32]byte{}), TxnArgString(""),
uint64(0), [32]byte{}, "",
)

type Program struct {
Expand All @@ -86,8 +77,8 @@ func ExampleMarshal_libra_program() {
prog := &Program{
Code: []byte("move"),
Args: []TransactionArgument{
TxnArgString("CAFE D00D"),
TxnArgString("cafe d00d"),
"CAFE D00D",
"cafe d00d",
},
Modules: [][]byte{{0xca}, {0xfe, 0xd0}, {0x0d}},
}
Expand Down

0 comments on commit 3369f23

Please sign in to comment.