1 // Copyright 2017 Tendermint. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
22 "github.com/tendermint/go-wire"
25 func Example_RegisterInterface() {
26 type Receiver interface{}
27 type bcMessage struct {
32 type bcResponse struct {
37 type bcStatus struct {
41 var _ = wire.RegisterInterface(
43 wire.ConcreteType{&bcMessage{}, 0x01},
44 wire.ConcreteType{&bcResponse{}, 0x02},
45 wire.ConcreteType{&bcStatus{}, 0x03},
49 func Example_EndToEnd_ReadWriteBinary() {
50 type Receiver interface{}
51 type bcMessage struct {
56 type bcResponse struct {
61 type bcStatus struct {
65 var _ = wire.RegisterInterface(
67 wire.ConcreteType{&bcMessage{}, 0x01},
68 wire.ConcreteType{&bcResponse{}, 0x02},
69 wire.ConcreteType{&bcStatus{}, 0x03},
74 buf := new(bytes.Buffer)
75 bm := &bcMessage{Message: "Tendermint", Height: 100}
76 wire.WriteBinary(bm, buf, &n, &err)
78 log.Fatalf("writeBinary: %v", err)
80 fmt.Printf("Encoded: %x\n", buf.Bytes())
82 recv := wire.ReadBinary(struct{ Receiver }{}, buf, 0, &n, &err).(struct{ Receiver }).Receiver
84 log.Fatalf("readBinary: %v", err)
86 decoded := recv.(*bcMessage)
87 fmt.Printf("Decoded: %#v\n", decoded)
90 // Encoded: 01010a54656e6465726d696e740164
91 // Decoded: &wire_test.bcMessage{Message:"Tendermint", Height:100}