OSDN Git Service

Wallet store test (#312)
[bytom/vapor.git] / vendor / github.com / tendermint / go-wire / data / common_test.go
1 package data_test
2
3 import (
4         "strings"
5
6         data "github.com/tendermint/go-wire/data"
7 )
8
9 /** These are some sample types to test parsing **/
10
11 type Fooer interface {
12         Foo() string
13 }
14
15 type Bar struct {
16         Name string `json:"name"`
17 }
18
19 func (b Bar) Foo() string {
20         return "Bar " + b.Name
21 }
22
23 type Baz struct {
24         Name string `json:"name"`
25 }
26
27 func (b Baz) Foo() string {
28         return strings.Replace(b.Name, "r", "z", -1)
29 }
30
31 type Nested struct {
32         Prefix string `json:"prefix"`
33         Sub    FooerS `json:"sub"`
34 }
35
36 func (n Nested) Foo() string {
37         return n.Prefix + ": " + n.Sub.Foo()
38 }
39
40 /** This is parse code: todo - autogenerate **/
41
42 var fooersParser data.Mapper
43
44 type FooerS struct {
45         Fooer
46 }
47
48 func (f FooerS) MarshalJSON() ([]byte, error) {
49         return fooersParser.ToJSON(f.Fooer)
50 }
51
52 func (f *FooerS) UnmarshalJSON(data []byte) (err error) {
53         parsed, err := fooersParser.FromJSON(data)
54         if err == nil {
55                 f.Fooer = parsed.(Fooer)
56         }
57         return
58 }
59
60 // Set is a helper to deal with wrapped interfaces
61 func (f *FooerS) Set(foo Fooer) {
62         f.Fooer = foo
63 }
64
65 /** end TO-BE auto-generated code **/
66
67 /** This connects our code with the auto-generated helpers **/
68
69 // this init must come after the above init (which should be in a file from import)
70 func init() {
71         fooersParser = data.NewMapper(FooerS{}).
72                 RegisterImplementation(Bar{}, "bar", 0x01).
73                 RegisterImplementation(Baz{}, "baz", 0x02).
74                 RegisterImplementation(Nested{}, "nest", 0x03)
75 }