mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-24 02:01:43 +00:00
Cleanup; Implement .Wrap()
This commit is contained in:
102
embed_test.go
102
embed_test.go
@ -9,50 +9,13 @@ import (
|
||||
data "github.com/tendermint/go-data"
|
||||
)
|
||||
|
||||
type Foo struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func (f Foo) Greet() string {
|
||||
return "Foo: " + f.Name
|
||||
}
|
||||
|
||||
type Bar struct {
|
||||
Age int
|
||||
}
|
||||
|
||||
func (b Bar) Greet() string {
|
||||
return fmt.Sprintf("Bar #%d", b.Age)
|
||||
type PubName struct {
|
||||
PubNameInner
|
||||
}
|
||||
|
||||
type PubNameInner interface {
|
||||
Greet() string
|
||||
}
|
||||
|
||||
type privNameInner interface {
|
||||
Greet() string
|
||||
}
|
||||
|
||||
type Greeter interface {
|
||||
Greet() string
|
||||
}
|
||||
|
||||
var (
|
||||
pubNameMapper, privNameMapper data.Mapper
|
||||
)
|
||||
|
||||
// register both public key types with go-data (and thus go-wire)
|
||||
func init() {
|
||||
pubNameMapper = data.NewMapper(PubName{}).
|
||||
RegisterImplementation(Foo{}, "foo", 1).
|
||||
RegisterImplementation(Bar{}, "bar", 2)
|
||||
privNameMapper = data.NewMapper(PrivName{}).
|
||||
RegisterImplementation(Foo{}, "foo", 1).
|
||||
RegisterImplementation(Bar{}, "bar", 2)
|
||||
}
|
||||
|
||||
type PubName struct {
|
||||
PubNameInner
|
||||
AssertIsPubNameInner()
|
||||
String() string
|
||||
}
|
||||
|
||||
func (p PubName) MarshalJSON() ([]byte, error) {
|
||||
@ -67,62 +30,61 @@ func (p *PubName) UnmarshalJSON(data []byte) error {
|
||||
return err
|
||||
}
|
||||
|
||||
type PrivName struct {
|
||||
privNameInner
|
||||
var pubNameMapper = data.NewMapper(PubName{}).
|
||||
RegisterImplementation(PubNameFoo{}, "foo", 1).
|
||||
RegisterImplementation(PubNameBar{}, "bar", 2)
|
||||
|
||||
func (f PubNameFoo) AssertIsPubNameInner() {}
|
||||
func (f PubNameBar) AssertIsPubNameInner() {}
|
||||
|
||||
//----------------------------------------
|
||||
|
||||
type PubNameFoo struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func (p PrivName) MarshalJSON() ([]byte, error) {
|
||||
return privNameMapper.ToJSON(p.privNameInner)
|
||||
func (f PubNameFoo) String() string { return "Foo: " + f.Name }
|
||||
|
||||
type PubNameBar struct {
|
||||
Age int
|
||||
}
|
||||
|
||||
func (p *PrivName) UnmarshalJSON(data []byte) error {
|
||||
parsed, err := privNameMapper.FromJSON(data)
|
||||
if err == nil && parsed != nil {
|
||||
p.privNameInner = parsed.(privNameInner)
|
||||
}
|
||||
return err
|
||||
}
|
||||
func (b PubNameBar) String() string { return fmt.Sprintf("Bar #%d", b.Age) }
|
||||
|
||||
//----------------------------------------
|
||||
|
||||
// TestEncodeDemo tries the various strategies to encode the objects
|
||||
func TestEncodeDemo(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
// assert := assert.New(t)
|
||||
// require := require.New(t)
|
||||
|
||||
cases := []struct {
|
||||
in, out Greeter
|
||||
in, out PubNameInner
|
||||
expected string
|
||||
}{
|
||||
{PubName{Foo{"pub-foo"}}, &PubName{}, "Foo: pub-foo"},
|
||||
{PubName{Bar{7}}, &PubName{}, "Bar #7"},
|
||||
|
||||
// Note these fail - if you can figure a solution here, I'll buy you a beer :)
|
||||
// (ebuchman is right, you must either break the reflection system, or modify go-wire)
|
||||
// but such a mod would let us make REALLY sure that no one could construct like this
|
||||
|
||||
// {PrivName{Foo{"priv-foo"}}, &PrivName{}, "Foo: priv-foo"},
|
||||
// {PrivName{Bar{9}}, &PrivName{}, "Bar #9"},
|
||||
{PubName{PubNameFoo{"pub-foo"}}, &PubName{}, "Foo: pub-foo"},
|
||||
{PubName{PubNameBar{7}}, &PubName{}, "Bar #7"},
|
||||
}
|
||||
|
||||
for i, tc := range cases {
|
||||
// make sure it is proper to start
|
||||
require.Equal(tc.expected, tc.in.Greet())
|
||||
|
||||
// now, try to encode as binary
|
||||
// Make sure it is proper to start
|
||||
require.Equal(tc.expected, tc.in.String())
|
||||
|
||||
// Try to encode as binary
|
||||
b, err := data.ToWire(tc.in)
|
||||
if assert.Nil(err, "%d: %#v", i, tc.in) {
|
||||
err := data.FromWire(b, tc.out)
|
||||
if assert.Nil(err) {
|
||||
assert.Equal(tc.expected, tc.out.Greet())
|
||||
assert.Equal(tc.expected, tc.out.String())
|
||||
}
|
||||
}
|
||||
|
||||
// try to encode it as json
|
||||
// Try to encode it as json
|
||||
j, err := data.ToJSON(tc.in)
|
||||
if assert.Nil(err, "%d: %#v", i, tc.in) {
|
||||
err := data.FromJSON(j, tc.out)
|
||||
if assert.Nil(err) {
|
||||
assert.Equal(tc.expected, tc.out.Greet())
|
||||
assert.Equal(tc.expected, tc.out.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user