diff --git a/README.md b/README.md index 3ac2dbe..dcfddb5 100644 --- a/README.md +++ b/README.md @@ -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. @@ -103,38 +103,28 @@ 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 @@ -142,7 +132,7 @@ type Wrapper struct { Enum Enum1 } bytes, err := lcs.Marshal(&Wrapper{ - Enum: Enum1Opt1(true), + Enum: Enum1Opt0{10}, }) ``` diff --git a/example_test.go b/example_test.go index e74c5bc..8e4cba8 100644 --- a/example_test.go +++ b/example_test.go @@ -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 { @@ -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}}, }