Skip to content

Commit

Permalink
Refactor marshaler interface
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasinsaurralde committed Mar 23, 2017
1 parent 8255b47 commit f264542
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 64 deletions.
9 changes: 5 additions & 4 deletions marshalers/brotli.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package transports

import (
"errors"

"gopkg.in/kothar/brotli-go.v0/dec"
"gopkg.in/kothar/brotli-go.v0/enc"
)

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 {
Expand All @@ -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 {
Expand All @@ -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
}
23 changes: 12 additions & 11 deletions marshalers/brotli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -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")
Expand Down
36 changes: 18 additions & 18 deletions marshalers/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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)
}
Expand All @@ -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 {
Expand All @@ -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
}
11 changes: 6 additions & 5 deletions marshalers/chain_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -23,7 +24,7 @@ func init() {
}

func TestBasicChaining(t *testing.T) {
err, chain := transports.NewChain(
chain, err := transports.NewChain(
transports.DummyMarshaler{},
transports.DummyMarshaler{},
)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
}
Expand Down
11 changes: 3 additions & 8 deletions marshalers/dummy.go
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 3 additions & 2 deletions marshalers/dummy_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package transports_test

import (
"github.com/matiasinsaurralde/transports/marshalers"
"testing"

"github.com/matiasinsaurralde/transports/marshalers"
)

type UnknownType struct {
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions marshalers/marshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
24 changes: 12 additions & 12 deletions marshalers/protobuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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{}

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions marshalers/protobuf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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")
Expand Down

0 comments on commit f264542

Please sign in to comment.