Skip to content

Commit

Permalink
Adapter pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
ferminhg committed Feb 10, 2024
1 parent 27ccf95 commit 4f10e45
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 0 deletions.
59 changes: 59 additions & 0 deletions patterns/adapter/square_peg_adapter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package adapter

import math "math"

type RoundWithRadius interface {
GetRadius() int
}

type RoundHole struct {
radius int
}

func NewRoundHole(radius int) *RoundHole {
return &RoundHole{radius: radius}
}

func (r *RoundHole) GetRadius() int {
return r.radius
}

func (r *RoundHole) Fits(peg RoundWithRadius) bool {
return r.radius >= peg.GetRadius()
}

type RoundPeg struct {
radius int
}

func NewRoundPeg(radius int) *RoundPeg {
return &RoundPeg{radius: radius}
}

func (p *RoundPeg) GetRadius() int {
return p.radius
}

type SquarePeg struct {
width int
}

func NewSquarePeg(width int) *SquarePeg {
return &SquarePeg{width: width}
}

func (s *SquarePeg) GetWidth() int {
return s.width
}

type SquarePegAdapter struct {
peg *SquarePeg
}

func NewSquarePegAdapter(peg *SquarePeg) *SquarePegAdapter {
return &SquarePegAdapter{peg: peg}
}

func (s *SquarePegAdapter) GetRadius() int {
return int(float64(s.peg.GetWidth()) * math.Sqrt(2) / 2)
}
19 changes: 19 additions & 0 deletions patterns/adapter/square_peg_adapter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package adapter

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestNewSquarePegAdapter(t *testing.T) {
hole := NewRoundHole(5)
peg := NewRoundPeg(5)
assert.True(t, hole.Fits(peg))

smallSqpeg := NewSquarePeg(5)
largeSqpeg := NewSquarePeg(10)
sAdapter := NewSquarePegAdapter(smallSqpeg)
lAdapter := NewSquarePegAdapter(largeSqpeg)
assert.True(t, hole.Fits(sAdapter))
assert.False(t, hole.Fits(lAdapter))
}
49 changes: 49 additions & 0 deletions patterns/adapter/xml2json_adapater.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package adapter

import (
"encoding/json"
"encoding/xml"
"fmt"
)

type note struct {
To string `xml:"to"`
From string `xml:"from"`
Heading string `xml:"heading"`
Body string `xml:"body"`
}

type XMLDTO struct {
id string
data []byte
}

type JSONDTO struct {
id string
data []byte
}

type JSONAdapter struct {
xmlDTO XMLDTO
}

func NewJSONAdapter(xmldto XMLDTO) *JSONAdapter {
return &JSONAdapter{xmlDTO: xmldto}
}

func (x *JSONAdapter) ToJSON() (*JSONDTO, error) {
var noteToConvert note
if err := xml.Unmarshal(x.xmlDTO.data, &noteToConvert); err != nil {
return nil, err
}

b, err := json.Marshal(noteToConvert)
if err != nil {
fmt.Println("error:", err)
}

return &JSONDTO{
id: x.xmlDTO.id,
data: b,
}, nil
}
24 changes: 24 additions & 0 deletions patterns/adapter/xml2json_adapater_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package adapter

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestXML2JSONAdapter_ConvertXMLToJSON(t *testing.T) {
xmlDto := XMLDTO{
id: "1",
data: []byte(`<note>
<to>Maya</to>
<from>Beka</from>
<heading>Reminder</heading>
<body>Miau!</body>
</note>`),
}
jsonExpected := `{"To":"Maya","From":"Beka","Heading":"Reminder","Body":"Miau!"}`

result, err := NewJSONAdapter(xmlDto).ToJSON()
assert.Nil(t, err)
assert.Equal(t, xmlDto.id, result.id)
assert.Equal(t, jsonExpected, string(result.data))
}

0 comments on commit 4f10e45

Please sign in to comment.