|
5 | 5 | [](https://goreportcard.com/report/github.com/the729/lcs)
|
6 | 6 | [](https://www.codacy.com/app/the729/lcs?utm_source=github.com&utm_medium=referral&utm_content=the729/lcs&utm_campaign=Badge_Grade)
|
7 | 7 |
|
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). |
9 | 9 |
|
10 | 10 | 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.
|
11 | 11 |
|
@@ -103,46 +103,36 @@ Enum types are golang interfaces.
|
103 | 103 | ```golang
|
104 | 104 | // Enum1 is an enum type.
|
105 | 105 | type Enum1 interface {
|
106 |
| - isEnum1() // optional: a dummy function to identify its variants. |
| 106 | +// isEnum1() // optional: member functions |
107 | 107 | }
|
108 | 108 |
|
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 |
112 | 110 | type Enum1Opt0 struct {
|
113 | 111 | Data uint32
|
114 | 112 | }
|
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 |
124 | 115 |
|
125 | 116 | // Register Enum1 with LCS. Will be available globaly.
|
126 | 117 | var _ = lcs.RegisterEnum(
|
127 | 118 | // nil pointer to the enum interface type:
|
128 | 119 | (*Enum1)(nil),
|
129 | 120 | // zero-values of each variants
|
130 |
| - (*Enum1Opt0)(nil), |
131 |
| - Enum1Opt1(false), |
| 121 | + (*Enum1Opt0)(nil), // Use pointer for non-empty struct. |
| 122 | + Enum1Opt1{}, |
132 | 123 | Enum1Opt2(nil),
|
133 |
| - Enum1Opt3(nil), |
134 | 124 | )
|
135 | 125 |
|
136 | 126 | // Usage: Marshal the enum alone, must use pointer
|
137 |
| -e1 := Enum1(Enum1Opt1(true)) |
| 127 | +e1 := Enum1(Enum1Opt1{}) |
138 | 128 | bytes, err := lcs.Marshal(&e1)
|
139 | 129 |
|
140 | 130 | // Use Enum1 within other structs
|
141 | 131 | type Wrapper struct {
|
142 | 132 | Enum Enum1
|
143 | 133 | }
|
144 | 134 | bytes, err := lcs.Marshal(&Wrapper{
|
145 |
| - Enum: Enum1Opt1(true), |
| 135 | + Enum: Enum1Opt0{10}, |
146 | 136 | })
|
147 | 137 |
|
148 | 138 | ```
|
0 commit comments