This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
example_test.go
107 lines (91 loc) · 2.07 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package lcs_test
import (
"encoding/hex"
"fmt"
"github.com/the729/lcs"
)
func ExampleMarshal_struct() {
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 ExampleUnmarshal_struct() {
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
}
type TransactionArgument interface{}
// Register TransactionArgument with LCS. Will be available globaly.
var _ = lcs.RegisterEnum(
// pointer to enum interface:
(*TransactionArgument)(nil),
// zero-value of variants:
uint64(0), [32]byte{}, "",
)
type Program struct {
Code []byte
Args []TransactionArgument
Modules [][]byte
}
func ExampleMarshal_libra_program() {
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
}
func ExampleUnmarshal_libra_program() {
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]]}
}