From 750b25c47a5782f5f2b773ed9e706cb82b3ccef4 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Tue, 21 Mar 2017 21:13:50 +0100 Subject: [PATCH] Update go-data, test embedding strategies --- embed_test.go | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++ priv_key.go | 4 +- pub_key.go | 4 +- signature.go | 4 +- 4 files changed, 132 insertions(+), 6 deletions(-) create mode 100644 embed_test.go diff --git a/embed_test.go b/embed_test.go new file mode 100644 index 00000000..71852848 --- /dev/null +++ b/embed_test.go @@ -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()) + } + } + } +} diff --git a/priv_key.go b/priv_key.go index 7c2543ab..3a9a5f69 100644 --- a/priv_key.go +++ b/priv_key.go @@ -32,8 +32,8 @@ var privKeyMapper data.Mapper // register both private key types with go-data (and thus go-wire) func init() { privKeyMapper = data.NewMapper(PrivKeyS{}). - RegisterInterface(PrivKeyEd25519{}, NameEd25519, TypeEd25519). - RegisterInterface(PrivKeySecp256k1{}, NameSecp256k1, TypeSecp256k1) + RegisterImplementation(PrivKeyEd25519{}, NameEd25519, TypeEd25519). + RegisterImplementation(PrivKeySecp256k1{}, NameSecp256k1, TypeSecp256k1) } // PrivKeyS add json serialization to PrivKey diff --git a/pub_key.go b/pub_key.go index ad7a5400..a047edb2 100644 --- a/pub_key.go +++ b/pub_key.go @@ -27,8 +27,8 @@ var pubKeyMapper data.Mapper // register both public key types with go-data (and thus go-wire) func init() { pubKeyMapper = data.NewMapper(PubKeyS{}). - RegisterInterface(PubKeyEd25519{}, NameEd25519, TypeEd25519). - RegisterInterface(PubKeySecp256k1{}, NameSecp256k1, TypeSecp256k1) + RegisterImplementation(PubKeyEd25519{}, NameEd25519, TypeEd25519). + RegisterImplementation(PubKeySecp256k1{}, NameSecp256k1, TypeSecp256k1) } // PubKeyS add json serialization to PubKey diff --git a/signature.go b/signature.go index 7a03dc6d..bb9d8200 100644 --- a/signature.go +++ b/signature.go @@ -22,8 +22,8 @@ var sigMapper data.Mapper // register both public key types with go-data (and thus go-wire) func init() { sigMapper = data.NewMapper(SignatureS{}). - RegisterInterface(SignatureEd25519{}, NameEd25519, TypeEd25519). - RegisterInterface(SignatureSecp256k1{}, NameSecp256k1, TypeSecp256k1) + RegisterImplementation(SignatureEd25519{}, NameEd25519, TypeEd25519). + RegisterImplementation(SignatureSecp256k1{}, NameSecp256k1, TypeSecp256k1) } // SignatureS add json serialization to Signature