github.com/chain5j/chain5j-pkg@v1.0.7/codec/rlp/encoder_example_test.go (about) 1 // Copyright 2014 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package rlp_test 18 19 import ( 20 "fmt" 21 "io" 22 "testing" 23 24 "github.com/chain5j/chain5j-pkg/codec/rlp" 25 ) 26 27 type MyCoolType struct { 28 Name string 29 a, b uint 30 } 31 32 // EncodeRLP writes x as RLP list [a, b] that omits the Name field. 33 func (x *MyCoolType) EncodeRLP(w io.Writer) (err error) { 34 return rlp.Encode(w, []uint{x.a, x.b}) 35 } 36 37 func ExampleEncoder() { 38 var t *MyCoolType // t is nil pointer to MyCoolType 39 bytes, _ := rlp.EncodeToBytes(t) 40 fmt.Printf("%v → %X\n", t, bytes) 41 42 t = &MyCoolType{Name: "foobar", a: 5, b: 6} 43 bytes, _ = rlp.EncodeToBytes(t) 44 fmt.Printf("%v → %X\n", t, bytes) 45 46 // Output: 47 // <nil> → C0 48 // &{foobar 5 6} → C20506 49 } 50 51 type A struct { 52 Name string 53 } 54 type AA struct { 55 Name string 56 Age uint64 `rlp:"optional"` 57 } 58 type AAA struct { 59 Name string 60 Age uint64 `rlp:"optional"` 61 Sex bool `rlp:"optional"` 62 } 63 type AAAA struct { 64 Name string 65 Age uint64 `rlp:"optional"` 66 Sex bool `rlp:"optional"` 67 Data []byte `rlp:"optional"` 68 } 69 70 func TestRlpDecodeA(t *testing.T) { 71 a := A{ 72 Name: "姓名", 73 } 74 a1Bytes, err := rlp.EncodeToBytes(a) 75 if err != nil { 76 t.Fatal(err) 77 } 78 { 79 // decode1 80 var a A 81 err := rlp.DecodeBytes(a1Bytes, &a) 82 if err != nil { 83 t.Fatal(err) 84 } 85 t.Log("decode1", a) 86 } 87 { 88 // decode2 89 var a AA 90 err := rlp.DecodeBytes(a1Bytes, &a) 91 if err != nil { 92 t.Fatal(err) 93 } 94 t.Log("decode2", a) 95 } 96 { 97 // decode3 98 var a AAA 99 err := rlp.DecodeBytes(a1Bytes, &a) 100 if err != nil { 101 t.Fatal(err) 102 } 103 t.Log("decode3", a) 104 } 105 { 106 // decode4 107 var a AAAA 108 err := rlp.DecodeBytes(a1Bytes, &a) 109 if err != nil { 110 t.Fatal(err) 111 } 112 t.Log("decode4", a) 113 } 114 } 115 func TestRlpDecodeAA(t *testing.T) { 116 a := AA{ 117 Name: "姓名", 118 Age: 12, 119 } 120 a1Bytes, err := rlp.EncodeToBytes(a) 121 if err != nil { 122 t.Fatal(err) 123 } 124 { 125 // decode2 126 var a AA 127 err := rlp.DecodeBytes(a1Bytes, &a) 128 if err != nil { 129 t.Fatal(err) 130 } 131 t.Log("decode2", a) 132 } 133 { 134 // decode3 135 var a AAA 136 err := rlp.DecodeBytes(a1Bytes, &a) 137 if err != nil { 138 t.Fatal(err) 139 } 140 t.Log("decode3", a) 141 } 142 { 143 // decode4 144 var a AAAA 145 err := rlp.DecodeBytes(a1Bytes, &a) 146 if err != nil { 147 t.Fatal(err) 148 } 149 t.Log("decode4", a) 150 } 151 }