forked from go-kit/kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inmem.go
142 lines (118 loc) · 3.78 KB
/
inmem.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Package inmem provides in-memory implementations of all the domain repositories.
package inmem
import (
"sync"
"github.com/go-kit/kit/examples/shipping/cargo"
"github.com/go-kit/kit/examples/shipping/location"
"github.com/go-kit/kit/examples/shipping/voyage"
)
type cargoRepository struct {
mtx sync.RWMutex
cargos map[cargo.TrackingID]*cargo.Cargo
}
func (r *cargoRepository) Store(c *cargo.Cargo) error {
r.mtx.Lock()
defer r.mtx.Unlock()
r.cargos[c.TrackingID] = c
return nil
}
func (r *cargoRepository) Find(id cargo.TrackingID) (*cargo.Cargo, error) {
r.mtx.RLock()
defer r.mtx.RUnlock()
if val, ok := r.cargos[id]; ok {
return val, nil
}
return nil, cargo.ErrUnknown
}
func (r *cargoRepository) FindAll() []*cargo.Cargo {
r.mtx.RLock()
defer r.mtx.RUnlock()
c := make([]*cargo.Cargo, 0, len(r.cargos))
for _, val := range r.cargos {
c = append(c, val)
}
return c
}
// NewCargoRepository returns a new instance of a in-memory cargo repository.
func NewCargoRepository() cargo.Repository {
return &cargoRepository{
cargos: make(map[cargo.TrackingID]*cargo.Cargo),
}
}
type locationRepository struct {
locations map[location.UNLocode]*location.Location
}
func (r *locationRepository) Find(locode location.UNLocode) (*location.Location, error) {
if l, ok := r.locations[locode]; ok {
return l, nil
}
return nil, location.ErrUnknown
}
func (r *locationRepository) FindAll() []*location.Location {
l := make([]*location.Location, 0, len(r.locations))
for _, val := range r.locations {
l = append(l, val)
}
return l
}
// NewLocationRepository returns a new instance of a in-memory location repository.
func NewLocationRepository() location.Repository {
r := &locationRepository{
locations: make(map[location.UNLocode]*location.Location),
}
r.locations[location.SESTO] = location.Stockholm
r.locations[location.AUMEL] = location.Melbourne
r.locations[location.CNHKG] = location.Hongkong
r.locations[location.JNTKO] = location.Tokyo
r.locations[location.NLRTM] = location.Rotterdam
r.locations[location.DEHAM] = location.Hamburg
return r
}
type voyageRepository struct {
voyages map[voyage.Number]*voyage.Voyage
}
func (r *voyageRepository) Find(voyageNumber voyage.Number) (*voyage.Voyage, error) {
if v, ok := r.voyages[voyageNumber]; ok {
return v, nil
}
return nil, voyage.ErrUnknown
}
// NewVoyageRepository returns a new instance of a in-memory voyage repository.
func NewVoyageRepository() voyage.Repository {
r := &voyageRepository{
voyages: make(map[voyage.Number]*voyage.Voyage),
}
r.voyages[voyage.V100.Number] = voyage.V100
r.voyages[voyage.V300.Number] = voyage.V300
r.voyages[voyage.V400.Number] = voyage.V400
r.voyages[voyage.V0100S.Number] = voyage.V0100S
r.voyages[voyage.V0200T.Number] = voyage.V0200T
r.voyages[voyage.V0300A.Number] = voyage.V0300A
r.voyages[voyage.V0301S.Number] = voyage.V0301S
r.voyages[voyage.V0400S.Number] = voyage.V0400S
return r
}
type handlingEventRepository struct {
mtx sync.RWMutex
events map[cargo.TrackingID][]cargo.HandlingEvent
}
func (r *handlingEventRepository) Store(e cargo.HandlingEvent) {
r.mtx.Lock()
defer r.mtx.Unlock()
// Make array if it's the first event with this tracking ID.
if _, ok := r.events[e.TrackingID]; !ok {
r.events[e.TrackingID] = make([]cargo.HandlingEvent, 0)
}
r.events[e.TrackingID] = append(r.events[e.TrackingID], e)
}
func (r *handlingEventRepository) QueryHandlingHistory(id cargo.TrackingID) cargo.HandlingHistory {
r.mtx.RLock()
defer r.mtx.RUnlock()
return cargo.HandlingHistory{HandlingEvents: r.events[id]}
}
// NewHandlingEventRepository returns a new instance of a in-memory handling event repository.
func NewHandlingEventRepository() cargo.HandlingEventRepository {
return &handlingEventRepository{
events: make(map[cargo.TrackingID][]cargo.HandlingEvent),
}
}