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

Commit 3369f23

Browse files
committed
update readme and examples
1 parent 6387fd4 commit 3369f23

File tree

2 files changed

+13
-32
lines changed

2 files changed

+13
-32
lines changed

README.md

+9-19
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![Go Report Card](https://goreportcard.com/badge/github.com/the729/lcs)](https://goreportcard.com/report/github.com/the729/lcs)
66
[![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)
77

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

1010
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.
1111

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

109-
// *Enum1Opt0, Enum1Opt1, Enum1Opt2, Enum1Opt3 are variants of Enum1
110-
// Use pointer for non-empty struct.
111-
// Use empty struct for a variant without contents.
109+
// *Enum1Opt0, Enum1Opt1, Enum1Opt2 are variants of Enum1
112110
type Enum1Opt0 struct {
113111
Data uint32
114112
}
115-
type Enum1Opt1 bool
116-
type Enum1Opt2 []byte
117-
type Enum1Opt3 []Enum1 // self reference is OK
118-
119-
// Variants should implement Enum1
120-
func (*Enum1Opt0) isEnum1() {}
121-
func (Enum1Opt1) isEnum1() {}
122-
func (Enum1Opt2) isEnum1() {}
123-
func (Enum1Opt3) isEnum1() {}
113+
type Enum1Opt1 struct{} // Use empty struct for a variant without contents.
114+
type Enum1Opt2 []Enum1 // self reference is OK
124115

125116
// Register Enum1 with LCS. Will be available globaly.
126117
var _ = lcs.RegisterEnum(
127118
// nil pointer to the enum interface type:
128119
(*Enum1)(nil),
129120
// zero-values of each variants
130-
(*Enum1Opt0)(nil),
131-
Enum1Opt1(false),
121+
(*Enum1Opt0)(nil), // Use pointer for non-empty struct.
122+
Enum1Opt1{},
132123
Enum1Opt2(nil),
133-
Enum1Opt3(nil),
134124
)
135125

136126
// Usage: Marshal the enum alone, must use pointer
137-
e1 := Enum1(Enum1Opt1(true))
127+
e1 := Enum1(Enum1Opt1{})
138128
bytes, err := lcs.Marshal(&e1)
139129

140130
// Use Enum1 within other structs
141131
type Wrapper struct {
142132
Enum Enum1
143133
}
144134
bytes, err := lcs.Marshal(&Wrapper{
145-
Enum: Enum1Opt1(true),
135+
Enum: Enum1Opt0{10},
146136
})
147137

148138
```

example_test.go

+4-13
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,14 @@ func ExampleUnmarshal_struct() {
5757
// Output: Name: test, Label: hello
5858
}
5959

60-
type TransactionArgument interface {
61-
isTransactionArg()
62-
}
63-
type TxnArgU64 uint64
64-
type TxnArgAddress [32]byte
65-
type TxnArgString string
66-
67-
func (TxnArgU64) isTransactionArg() {}
68-
func (TxnArgAddress) isTransactionArg() {}
69-
func (TxnArgString) isTransactionArg() {}
60+
type TransactionArgument interface{}
7061

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

7970
type Program struct {
@@ -86,8 +77,8 @@ func ExampleMarshal_libra_program() {
8677
prog := &Program{
8778
Code: []byte("move"),
8879
Args: []TransactionArgument{
89-
TxnArgString("CAFE D00D"),
90-
TxnArgString("cafe d00d"),
80+
"CAFE D00D",
81+
"cafe d00d",
9182
},
9283
Modules: [][]byte{{0xca}, {0xfe, 0xd0}, {0x0d}},
9384
}

0 commit comments

Comments
 (0)