From f2645422df1ccaee39629710b24b27aedf307ccd Mon Sep 17 00:00:00 2001 From: Matias Insaurralde Date: Thu, 23 Mar 2017 03:22:53 -0300 Subject: [PATCH] Refactor marshaler interface --- marshalers/brotli.go | 9 +++++---- marshalers/brotli_test.go | 23 ++++++++++++----------- marshalers/chain.go | 36 ++++++++++++++++++------------------ marshalers/chain_test.go | 11 ++++++----- marshalers/dummy.go | 11 +++-------- marshalers/dummy_test.go | 5 +++-- marshalers/marshaler.go | 4 ++-- marshalers/protobuf.go | 24 ++++++++++++------------ marshalers/protobuf_test.go | 4 ++-- 9 files changed, 63 insertions(+), 64 deletions(-) diff --git a/marshalers/brotli.go b/marshalers/brotli.go index 50fff7c..2f53034 100644 --- a/marshalers/brotli.go +++ b/marshalers/brotli.go @@ -2,6 +2,7 @@ package transports import ( "errors" + "gopkg.in/kothar/brotli-go.v0/dec" "gopkg.in/kothar/brotli-go.v0/enc" ) @@ -9,7 +10,7 @@ import ( type BrotliMarshaler struct { } -func (marshaler BrotliMarshaler) Marshal(i *interface{}) (error, interface{}) { +func (marshaler BrotliMarshaler) Marshal(i *interface{}) (interface{}, error) { var err error if i == nil { @@ -29,10 +30,10 @@ func (marshaler BrotliMarshaler) Marshal(i *interface{}) (error, interface{}) { buf, err := enc.CompressBuffer(nil, inputBuf, make([]byte, 0)) - return err, buf + return buf, err } -func (marshaler BrotliMarshaler) Unmarshal(i *interface{}) (error, interface{}) { +func (marshaler BrotliMarshaler) Unmarshal(i *interface{}) (interface{}, error) { var err error if i == nil { @@ -52,5 +53,5 @@ func (marshaler BrotliMarshaler) Unmarshal(i *interface{}) (error, interface{}) buf, err := dec.DecompressBuffer(inputBuf, make([]byte, 0)) - return err, buf + return buf, err } diff --git a/marshalers/brotli_test.go b/marshalers/brotli_test.go index 01f4761..16b8cdc 100644 --- a/marshalers/brotli_test.go +++ b/marshalers/brotli_test.go @@ -2,29 +2,30 @@ package transports_test import ( "bytes" + "strings" + "testing" + "github.com/matiasinsaurralde/transports/marshalers" "github.com/matiasinsaurralde/transports/marshalers/protos" "gopkg.in/kothar/brotli-go.v0/dec" - "strings" - "testing" ) func TestBrotliMarshalChain(t *testing.T) { - _, chain := transports.NewChain( + chain, _ := transports.NewChain( transports.ProtobufMarshaler{}, transports.DummyMarshaler{}, ) - _, output := chain.Marshal(&request) + output, _ := chain.Marshal(&request) protobufOutput := output.([]byte) - _, compressionChain := transports.NewChain( + compressionChain, _ := transports.NewChain( transports.ProtobufMarshaler{}, transports.BrotliMarshaler{}, ) - _, output = compressionChain.Marshal(&request) + output, _ = compressionChain.Marshal(&request) compressedOutput := output.([]byte) @@ -38,19 +39,19 @@ func TestBrotliMarshalChain(t *testing.T) { func TestBrotliUnmarshalChain(t *testing.T) { - _, chain := transports.NewChain( + chain, _ := transports.NewChain( transports.ProtobufMarshaler{}, transports.BrotliMarshaler{}, ) - _, output := chain.Marshal(&request) + output, _ := chain.Marshal(&request) compressedOutput := output.([]byte) var i interface{} i = compressedOutput - err, unmarshalOutput := chain.Unmarshal(i) + unmarshalOutput, err := chain.Unmarshal(i) if err != nil { t.Fatal(err) @@ -75,7 +76,7 @@ func TestBrotliUnsupportedType(t *testing.T) { var i interface{} i = v - err, _ := marshaler.Marshal(&i) + _, err := marshaler.Marshal(&i) exists := strings.Index(err.Error(), transports.MarshalerTypeNotSupportedError) @@ -87,7 +88,7 @@ func TestBrotliUnsupportedType(t *testing.T) { func TestBrotliNilInput(t *testing.T) { var marshaler transports.Marshaler marshaler = transports.BrotliMarshaler{} - err, _ := marshaler.Marshal(nil) + _, err := marshaler.Marshal(nil) if strings.Index(err.Error(), transports.MarshalerNilTypeError) < 0 { t.Fatal("Nil type doesn't break the Brotli marshaler") diff --git a/marshalers/chain.go b/marshalers/chain.go index 2730408..9fcb366 100644 --- a/marshalers/chain.go +++ b/marshalers/chain.go @@ -14,23 +14,23 @@ type ChainData struct { } type Chain interface { - Marshal(interface{}) (error, interface{}) - Unmarshal(interface{}) (error, interface{}) - process(bool) (error, interface{}) + Marshal(interface{}) (interface{}, error) + Unmarshal(interface{}) (interface{}, error) + process(bool) (interface{}, error) reverse() []Marshaler } -func NewChain(marshalers ...Marshaler) (error, Chain) { +func NewChain(marshalers ...Marshaler) (Chain, error) { var err error if len(marshalers) <= 1 { err = errors.New(ChainSingleMarshalerError) - return err, nil + return nil, err } data := ChainData{marshalers, nil} - return err, Chain(&data) + return Chain(&data), err } -func (s *ChainData) process(reverseOrder bool) (error, interface{}) { +func (s *ChainData) process(reverseOrder bool) (interface{}, error) { log.Println("Process()", s) var output interface{} @@ -51,17 +51,17 @@ func (s *ChainData) process(reverseOrder bool) (error, interface{}) { if output == nil { log.Println("No previous output, starting chain") if reverseOrder { - err, output = m.Unmarshal(&s.input) + output, err = m.Unmarshal(&s.input) } else { - err, output = m.Marshal(&s.input) + output, err = m.Marshal(&s.input) } log.Println("First output:", output, "Error:", err) } else { log.Println("Previous output", output) if reverseOrder { - err, output = m.Unmarshal(&output) + output, err = m.Unmarshal(&output) } else { - err, output = m.Marshal(&output) + output, err = m.Marshal(&output) } log.Println("New output:", output, "Error:", err) } @@ -76,7 +76,7 @@ func (s *ChainData) process(reverseOrder bool) (error, interface{}) { err = errors.New(ChainNilOutput) } - return err, output + return output, err } func (s *ChainData) reverse() []Marshaler { @@ -90,16 +90,16 @@ func (s *ChainData) reverse() []Marshaler { return marshalers } -func (s *ChainData) Marshal(i interface{}) (error, interface{}) { +func (s *ChainData) Marshal(i interface{}) (interface{}, error) { log.Println("Output()", s) s.input = i - err, output := s.process(false) - return err, output + output, err := s.process(false) + return output, err } -func (s *ChainData) Unmarshal(i interface{}) (error, interface{}) { +func (s *ChainData) Unmarshal(i interface{}) (interface{}, error) { log.Println("Output()", s) s.input = i - err, output := s.process(true) - return err, output + output, err := s.process(true) + return output, err } diff --git a/marshalers/chain_test.go b/marshalers/chain_test.go index d9ecef8..86a99cc 100644 --- a/marshalers/chain_test.go +++ b/marshalers/chain_test.go @@ -1,11 +1,12 @@ package transports_test import ( - "github.com/matiasinsaurralde/transports/marshalers" "net/http" "net/url" "strings" "testing" + + "github.com/matiasinsaurralde/transports/marshalers" ) const ChainTestBasicChainingError string = "Couldn't match Request URL field after chaining" @@ -23,7 +24,7 @@ func init() { } func TestBasicChaining(t *testing.T) { - err, chain := transports.NewChain( + chain, err := transports.NewChain( transports.DummyMarshaler{}, transports.DummyMarshaler{}, ) @@ -32,7 +33,7 @@ func TestBasicChaining(t *testing.T) { t.Fatal(err) } - err, output := chain.Marshal(&request) + output, err := chain.Marshal(&request) if err != nil { t.Fatal(err) @@ -47,7 +48,7 @@ func TestBasicChaining(t *testing.T) { } func TestChainingWithSingleOrNoMarshalers(t *testing.T) { - err, chain := transports.NewChain(transports.ProtobufMarshaler{}) + chain, err := transports.NewChain(transports.ProtobufMarshaler{}) if err == nil || chain != nil { t.Fatal(ChainTestMarshalerCountError) @@ -56,7 +57,7 @@ func TestChainingWithSingleOrNoMarshalers(t *testing.T) { t.Fatal(ChainTestMarshalerCountError) } - err, chain = transports.NewChain() + chain, err = transports.NewChain() if err == nil || chain != nil { t.Fatal(ChainTestMarshalerCountError) } diff --git a/marshalers/dummy.go b/marshalers/dummy.go index c1b0d24..69de8fd 100644 --- a/marshalers/dummy.go +++ b/marshalers/dummy.go @@ -1,17 +1,12 @@ package transports -import ( -// "log" -) - type DummyMarshaler struct { } -func (marshaler DummyMarshaler) Marshal(i *interface{}) (error, interface{}) { - var err error - return err, *i +func (marshaler DummyMarshaler) Marshal(i *interface{}) (interface{}, error) { + return *i, nil } -func (marshaler DummyMarshaler) Unmarshal(i *interface{}) (error, interface{}) { +func (marshaler DummyMarshaler) Unmarshal(i *interface{}) (interface{}, error) { return nil, nil } diff --git a/marshalers/dummy_test.go b/marshalers/dummy_test.go index 3b30a06..c915082 100644 --- a/marshalers/dummy_test.go +++ b/marshalers/dummy_test.go @@ -1,8 +1,9 @@ package transports_test import ( - "github.com/matiasinsaurralde/transports/marshalers" "testing" + + "github.com/matiasinsaurralde/transports/marshalers" ) type UnknownType struct { @@ -25,7 +26,7 @@ func TestMarshal(t *testing.T) { marshaler = transports.DummyMarshaler{} - err, output := marshaler.Marshal(&TestInterface) + output, err := marshaler.Marshal(&TestInterface) if err != nil { t.Fatal(err) diff --git a/marshalers/marshaler.go b/marshalers/marshaler.go index c604a02..2438356 100644 --- a/marshalers/marshaler.go +++ b/marshalers/marshaler.go @@ -6,6 +6,6 @@ const MarshalerTypeNotSupportedError string = "Marshaler doesn't support the typ const MarshalerUnexpectedOutput string = "Unexpected Marshaler output" type Marshaler interface { - Marshal(*interface{}) (error, interface{}) - Unmarshal(*interface{}) (error, interface{}) + Marshal(*interface{}) (interface{}, error) + Unmarshal(*interface{}) (interface{}, error) } diff --git a/marshalers/protobuf.go b/marshalers/protobuf.go index 9d737bb..4ac2da5 100644 --- a/marshalers/protobuf.go +++ b/marshalers/protobuf.go @@ -13,13 +13,13 @@ import ( type ProtobufMarshaler struct { } -func (marshaler ProtobufMarshaler) Marshal(i *interface{}) (error, interface{}) { +func (marshaler ProtobufMarshaler) Marshal(i *interface{}) (interface{}, error) { var err error var r interface{} if i == nil { err = errors.New(MarshalerNilTypeError) - return err, r + return r, err } switch t := (*i).(type) { @@ -38,10 +38,10 @@ func (marshaler ProtobufMarshaler) Marshal(i *interface{}) (error, interface{}) err = errors.New(strings.Join([]string{message, typestr}, " ")) } - return err, r + return r, err } -func (marshaler ProtobufMarshaler) Unmarshal(i *interface{}) (error, interface{}) { +func (marshaler ProtobufMarshaler) Unmarshal(i *interface{}) (interface{}, error) { var err error var r interface{} @@ -55,20 +55,20 @@ func (marshaler ProtobufMarshaler) Unmarshal(i *interface{}) (error, interface{} buffer := (*i).([]byte) - HttpResponse := &transportsProto.HttpResponse{} - err = proto.Unmarshal(buffer, HttpResponse) + httpResponse := &transportsProto.HttpResponse{} + err = proto.Unmarshal(buffer, httpResponse) if err == nil { - r = HttpResponse - return err, r + r = httpResponse + return r, err } - HttpRequest := &transportsProto.HttpRequest{} - err = proto.Unmarshal(buffer, HttpRequest) + httpRequest := &transportsProto.HttpRequest{} + err = proto.Unmarshal(buffer, httpRequest) if err == nil { - r = HttpRequest - return err, r + r = httpRequest + return r, err } return err, nil diff --git a/marshalers/protobuf_test.go b/marshalers/protobuf_test.go index 2016c11..79f90e4 100644 --- a/marshalers/protobuf_test.go +++ b/marshalers/protobuf_test.go @@ -55,7 +55,7 @@ func TestProtobufUnsupportedType(t *testing.T) { var i interface{} i = v - err, _ := marshaler.Marshal(&i) + _, err := marshaler.Marshal(&i) exists := strings.Index(err.Error(), transports.MarshalerTypeNotSupportedError) @@ -67,7 +67,7 @@ func TestProtobufUnsupportedType(t *testing.T) { func TestProtobufNilInput(t *testing.T) { var marshaler transports.Marshaler marshaler = transports.ProtobufMarshaler{} - err, _ := marshaler.Marshal(nil) + _, err := marshaler.Marshal(nil) if strings.Index(err.Error(), transports.MarshalerNilTypeError) < 0 { t.Fatal("Nil type doesn't break the Protobuf marshaler")