mirror of
https://github.com/fluencelabs/tendermint
synced 2025-04-25 14:52:17 +00:00
Update go-data, test embedding strategies
This commit is contained in:
parent
49569ac244
commit
750b25c47a
126
embed_test.go
Normal file
126
embed_test.go
Normal file
@ -0,0 +1,126 @@
|
|||||||
|
package crypto_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
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 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
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PubName) MarshalJSON() ([]byte, error) {
|
||||||
|
return pubNameMapper.ToJSON(p.PubNameInner)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PubName) UnmarshalJSON(data []byte) error {
|
||||||
|
parsed, err := pubNameMapper.FromJSON(data)
|
||||||
|
if err == nil && parsed != nil {
|
||||||
|
p.PubNameInner = parsed.(PubNameInner)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
type PrivName struct {
|
||||||
|
privNameInner
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PrivName) MarshalJSON() ([]byte, error) {
|
||||||
|
return privNameMapper.ToJSON(p.privNameInner)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PrivName) UnmarshalJSON(data []byte) error {
|
||||||
|
parsed, err := privNameMapper.FromJSON(data)
|
||||||
|
if err == nil && parsed != nil {
|
||||||
|
p.privNameInner = parsed.(privNameInner)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
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 :)
|
||||||
|
// {PrivName{Foo{"priv-foo"}}, &PrivName{}, "Foo: priv-foo"},
|
||||||
|
// {PrivName{Bar{9}}, &PrivName{}, "Bar #9"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, tc := range cases {
|
||||||
|
// make sure it is proper to start
|
||||||
|
require.Equal(tc.expected, tc.in.Greet())
|
||||||
|
fmt.Println(tc.expected)
|
||||||
|
|
||||||
|
// now, 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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -32,8 +32,8 @@ var privKeyMapper data.Mapper
|
|||||||
// register both private key types with go-data (and thus go-wire)
|
// register both private key types with go-data (and thus go-wire)
|
||||||
func init() {
|
func init() {
|
||||||
privKeyMapper = data.NewMapper(PrivKeyS{}).
|
privKeyMapper = data.NewMapper(PrivKeyS{}).
|
||||||
RegisterInterface(PrivKeyEd25519{}, NameEd25519, TypeEd25519).
|
RegisterImplementation(PrivKeyEd25519{}, NameEd25519, TypeEd25519).
|
||||||
RegisterInterface(PrivKeySecp256k1{}, NameSecp256k1, TypeSecp256k1)
|
RegisterImplementation(PrivKeySecp256k1{}, NameSecp256k1, TypeSecp256k1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrivKeyS add json serialization to PrivKey
|
// PrivKeyS add json serialization to PrivKey
|
||||||
|
@ -27,8 +27,8 @@ var pubKeyMapper data.Mapper
|
|||||||
// register both public key types with go-data (and thus go-wire)
|
// register both public key types with go-data (and thus go-wire)
|
||||||
func init() {
|
func init() {
|
||||||
pubKeyMapper = data.NewMapper(PubKeyS{}).
|
pubKeyMapper = data.NewMapper(PubKeyS{}).
|
||||||
RegisterInterface(PubKeyEd25519{}, NameEd25519, TypeEd25519).
|
RegisterImplementation(PubKeyEd25519{}, NameEd25519, TypeEd25519).
|
||||||
RegisterInterface(PubKeySecp256k1{}, NameSecp256k1, TypeSecp256k1)
|
RegisterImplementation(PubKeySecp256k1{}, NameSecp256k1, TypeSecp256k1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// PubKeyS add json serialization to PubKey
|
// PubKeyS add json serialization to PubKey
|
||||||
|
@ -22,8 +22,8 @@ var sigMapper data.Mapper
|
|||||||
// register both public key types with go-data (and thus go-wire)
|
// register both public key types with go-data (and thus go-wire)
|
||||||
func init() {
|
func init() {
|
||||||
sigMapper = data.NewMapper(SignatureS{}).
|
sigMapper = data.NewMapper(SignatureS{}).
|
||||||
RegisterInterface(SignatureEd25519{}, NameEd25519, TypeEd25519).
|
RegisterImplementation(SignatureEd25519{}, NameEd25519, TypeEd25519).
|
||||||
RegisterInterface(SignatureSecp256k1{}, NameSecp256k1, TypeSecp256k1)
|
RegisterImplementation(SignatureSecp256k1{}, NameSecp256k1, TypeSecp256k1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SignatureS add json serialization to Signature
|
// SignatureS add json serialization to Signature
|
||||||
|
Loading…
x
Reference in New Issue
Block a user