-
Notifications
You must be signed in to change notification settings - Fork 0
/
printevent.go
98 lines (90 loc) · 2.74 KB
/
printevent.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
package main
import (
"encoding/json"
"fmt"
"github.com/digimortl/tycoon/dom/warehouse"
"os"
"time"
"github.com/digimortl/tycoon/dom/event"
"github.com/digimortl/tycoon/dom/transmap"
"github.com/digimortl/tycoon/dom/transport"
)
type (
eventMap = map[string]interface{}
cargoMap = map[string]string
)
func toRelTime(tm time.Time) float64 {
return tm.Sub(time.Time{}).Hours()
}
func toKind(opt transmap.ShipmentOption) string {
switch opt {
case transmap.Land:
return "TRUCK"
case transmap.Sea:
return "VESSEL"
default:
return "UNKNOWN"
}
}
func toCargoMaps(cargoes []*warehouse.Cargo) []cargoMap {
cs := make([]cargoMap, 0)
for _, c := range cargoes {
cs = append(cs, cargoMap{
"cargo_id": c.TrackNumber,
"origin": c.Origin,
"destination": c.Destination,
})
}
return cs
}
func toEventMap(anEvent event.DomainEvent) (e eventMap) {
switch anEvent.(type) {
case transport.Arrived:
e = eventMap{
"time": toRelTime(anEvent.(transport.Arrived).OccurredAt),
"event": "ARRIVE",
"kind": toKind(anEvent.(transport.Arrived).ShipmentOpt),
"transport_id": anEvent.(transport.Arrived).Transport,
"location": anEvent.(transport.Arrived).AtLocation,
"cargo": toCargoMaps(anEvent.(transport.Arrived).Cargoes),
}
case transport.Departed:
e = eventMap{
"time": toRelTime(anEvent.(transport.Departed).OccurredAt),
"event": "DEPART",
"kind": toKind(anEvent.(transport.Departed).ShipmentOpt),
"transport_id": anEvent.(transport.Departed).Transport,
"location": anEvent.(transport.Departed).FromLocation,
"destination": anEvent.(transport.Departed).ToLocation,
"cargo": toCargoMaps(anEvent.(transport.Departed).Cargoes),
}
case transport.Loaded:
e = eventMap{
"time": toRelTime(anEvent.(transport.Loaded).OccurredAt.Add(
-anEvent.(transport.Loaded).Duration)),
"event": "LOAD",
"kind": toKind(anEvent.(transport.Loaded).ShipmentOpt),
"transport_id": anEvent.(transport.Loaded).Transport,
"duration": anEvent.(transport.Loaded).Duration.Hours(),
"cargo": toCargoMaps(anEvent.(transport.Loaded).Cargoes),
}
case transport.Unloaded:
e = eventMap{
"time": toRelTime(anEvent.(transport.Unloaded).OccurredAt.Add(-anEvent.(transport.Unloaded).Duration)),
"event": "UNLOAD",
"kind": toKind(anEvent.(transport.Unloaded).ShipmentOpt),
"transport_id": anEvent.(transport.Unloaded).Transport,
"duration": anEvent.(transport.Unloaded).Duration.Hours(),
}
}
return
}
func printEventToStdout(anEvent event.DomainEvent) {
e := toEventMap(anEvent)
if e != nil {
jsonData, err := json.Marshal(e)
if err == nil {
fmt.Fprintln(os.Stdout, string(jsonData))
}
}
}