OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / tendermint / go-wire / example_test.go
1 // Copyright 2017 Tendermint. All Rights Reserved.
2 //
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
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 package wire_test
16
17 import (
18         "bytes"
19         "fmt"
20         "log"
21
22         "github.com/tendermint/go-wire"
23 )
24
25 func Example_RegisterInterface() {
26         type Receiver interface{}
27         type bcMessage struct {
28                 Message string
29                 Height  int
30         }
31
32         type bcResponse struct {
33                 Status  int
34                 Message string
35         }
36
37         type bcStatus struct {
38                 Peers int
39         }
40
41         var _ = wire.RegisterInterface(
42                 struct{ Receiver }{},
43                 wire.ConcreteType{&bcMessage{}, 0x01},
44                 wire.ConcreteType{&bcResponse{}, 0x02},
45                 wire.ConcreteType{&bcStatus{}, 0x03},
46         )
47 }
48
49 func Example_EndToEnd_ReadWriteBinary() {
50         type Receiver interface{}
51         type bcMessage struct {
52                 Message string
53                 Height  int
54         }
55
56         type bcResponse struct {
57                 Status  int
58                 Message string
59         }
60
61         type bcStatus struct {
62                 Peers int
63         }
64
65         var _ = wire.RegisterInterface(
66                 struct{ Receiver }{},
67                 wire.ConcreteType{&bcMessage{}, 0x01},
68                 wire.ConcreteType{&bcResponse{}, 0x02},
69                 wire.ConcreteType{&bcStatus{}, 0x03},
70         )
71
72         var n int
73         var err error
74         buf := new(bytes.Buffer)
75         bm := &bcMessage{Message: "Tendermint", Height: 100}
76         wire.WriteBinary(bm, buf, &n, &err)
77         if err != nil {
78                 log.Fatalf("writeBinary: %v", err)
79         }
80         fmt.Printf("Encoded: %x\n", buf.Bytes())
81
82         recv := wire.ReadBinary(struct{ Receiver }{}, buf, 0, &n, &err).(struct{ Receiver }).Receiver
83         if err != nil {
84                 log.Fatalf("readBinary: %v", err)
85         }
86         decoded := recv.(*bcMessage)
87         fmt.Printf("Decoded: %#v\n", decoded)
88
89         // Output:
90         // Encoded: 01010a54656e6465726d696e740164
91         // Decoded: &wire_test.bcMessage{Message:"Tendermint", Height:100}
92 }