Skip to content

Commit 1108fff

Browse files
committed
Extract common types into package
1 parent 68f39e6 commit 1108fff

File tree

4 files changed

+50
-34
lines changed

4 files changed

+50
-34
lines changed

refactoring/theatre/createStatementData.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
package theatre
22

33
import (
4+
"dsa/refactoring/theatre/types"
45
"fmt"
56
"math"
67
)
78

8-
type Plays map[string]Play
9+
type Plays map[string]types.Play
910

10-
func (p Plays) PlayFor(aPerformance Performance) Play {
11+
func (p Plays) PlayFor(aPerformance types.Performance) types.Play {
1112
return p[aPerformance.PlayID]
1213
}
1314

1415
type EnrichedPerformance struct {
1516
Audience int
1617
Amount int
17-
Play Play
18+
Play types.Play
1819
playID string
1920
volumeCredits int
2021
}
@@ -24,7 +25,10 @@ type StatementData struct {
2425
Performances []EnrichedPerformance
2526
}
2627

27-
func CreateStatementData(invoice Invoice, plays map[string]Play) (StatementData, error) {
28+
func CreateStatementData(
29+
invoice types.Invoice,
30+
plays map[string]types.Play,
31+
) (StatementData, error) {
2832
result := StatementData{}
2933
result.Customer = invoice.Customer
3034

@@ -42,7 +46,7 @@ func CreateStatementData(invoice Invoice, plays map[string]Play) (StatementData,
4246
}
4347

4448
func (StatementData) enrichPerformance(
45-
performance Performance,
49+
performance types.Performance,
4650
plays Plays,
4751
) (EnrichedPerformance, error) {
4852
calculator, err := CreatePerformanceCalculator(performance, plays.PlayFor(performance))
@@ -79,19 +83,22 @@ func (s StatementData) TotalAmount() int {
7983
type PerformanceCalculator interface {
8084
Amount() int
8185
VolumeCredits() int
82-
Play() Play
86+
Play() types.Play
8387
}
8488

8589
type PerformanceCalculatorImpl struct {
86-
performance Performance
87-
play Play
90+
performance types.Performance
91+
play types.Play
8892
}
8993

90-
func NewPerformanceCalculator(pe Performance, pl Play) PerformanceCalculatorImpl {
94+
func NewPerformanceCalculator(pe types.Performance, pl types.Play) PerformanceCalculatorImpl {
9195
return PerformanceCalculatorImpl{pe, pl}
9296
}
9397

94-
func CreatePerformanceCalculator(pe Performance, pl Play) (PerformanceCalculator, error) {
98+
func CreatePerformanceCalculator(
99+
pe types.Performance,
100+
pl types.Play,
101+
) (PerformanceCalculator, error) {
95102
switch pl.Type {
96103
case "tragedy":
97104
return TragedyCalculatorImpl{NewPerformanceCalculator(pe, pl)}, nil
@@ -102,7 +109,7 @@ func CreatePerformanceCalculator(pe Performance, pl Play) (PerformanceCalculator
102109
}
103110
}
104111

105-
func (p PerformanceCalculatorImpl) Play() Play {
112+
func (p PerformanceCalculatorImpl) Play() types.Play {
106113
return p.play
107114
}
108115

refactoring/theatre/statement.go

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,18 @@
11
package theatre
22

33
import (
4+
"dsa/refactoring/theatre/types"
45
"fmt"
56

67
"github.com/leekchan/accounting"
78
)
89

9-
type Play struct {
10-
Name string
11-
Type string
12-
}
13-
14-
type Invoice struct {
15-
Customer string
16-
Performances []Performance
17-
}
18-
19-
type Performance struct {
20-
PlayID string
21-
Audience int
22-
}
23-
2410
type StatementPrinter struct{}
2511

26-
func (s StatementPrinter) Print(invoice Invoice, plays map[string]Play) (string, error) {
12+
func (s StatementPrinter) Print(
13+
invoice types.Invoice,
14+
plays map[string]types.Play,
15+
) (string, error) {
2716
statementData, err := CreateStatementData(invoice, plays)
2817
if err != nil {
2918
return "", err
@@ -49,7 +38,10 @@ func renderPlainText(data StatementData) string {
4938
return result
5039
}
5140

52-
func (s StatementPrinter) HtmlPrint(invoice Invoice, plays map[string]Play) (string, error) {
41+
func (s StatementPrinter) HtmlPrint(
42+
invoice types.Invoice,
43+
plays map[string]types.Play,
44+
) (string, error) {
5345
statementData, err := CreateStatementData(invoice, plays)
5446
if err != nil {
5547
return "", err

refactoring/theatre/statement_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
approvals "github.com/approvals/go-approval-tests"
1010

1111
"dsa/refactoring/theatre"
12+
"dsa/refactoring/theatre/types"
1213
)
1314

1415
func TestMain(m *testing.M) {
@@ -31,13 +32,13 @@ func TestPrinterPrintByApproval(t *testing.T) {
3132
}
3233

3334
func TestStatementWithNewPlayTypes(t *testing.T) {
34-
plays := map[string]theatre.Play{
35+
plays := map[string]types.Play{
3536
"henry-v": {Name: "Henry V", Type: "history"},
3637
"as-like": {Name: "As You Like It", Type: "pastoral"},
3738
}
38-
invoice := theatre.Invoice{
39+
invoice := types.Invoice{
3940
Customer: "BigCo",
40-
Performances: []theatre.Performance{
41+
Performances: []types.Performance{
4142
{PlayID: "henry-v", Audience: 53},
4243
{PlayID: "as-like", Audience: 55},
4344
},
@@ -50,7 +51,7 @@ func TestStatementWithNewPlayTypes(t *testing.T) {
5051
}
5152
}
5253

53-
func createTestData(t testing.TB, r io.Reader) (theatre.Invoice, map[string]theatre.Play) {
54+
func createTestData(t testing.TB, r io.Reader) (types.Invoice, map[string]types.Play) {
5455
var in struct {
5556
Plays []struct {
5657
ID string
@@ -59,14 +60,14 @@ func createTestData(t testing.TB, r io.Reader) (theatre.Invoice, map[string]thea
5960
Type string
6061
}
6162
}
62-
Invoice theatre.Invoice
63+
Invoice types.Invoice
6364
}
6465

6566
if err := json.NewDecoder(r).Decode(&in); err != nil {
6667
t.Fatalf("failed to decode input data: %v", err)
6768
}
6869

69-
plays := make(map[string]theatre.Play)
70+
plays := make(map[string]types.Play)
7071
for _, identifiedPlay := range in.Plays {
7172
plays[identifiedPlay.ID] = identifiedPlay.Play
7273
}

refactoring/theatre/types/types.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package types
2+
3+
type Play struct {
4+
Name string
5+
Type string
6+
}
7+
8+
type Invoice struct {
9+
Customer string
10+
Performances []Performance
11+
}
12+
13+
type Performance struct {
14+
PlayID string
15+
Audience int
16+
}

0 commit comments

Comments
 (0)