-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjourney.go
270 lines (231 loc) · 6.94 KB
/
journey.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package fptf
import (
"encoding/json"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsontype"
"time"
)
// Journey is a computed set of directions to get from A to B at a
// specific time. It would typically be the result of a route planning
// algorithm.
type Journey struct {
Id string
Trips []*Trip
Price *Price
Meta interface{} // any additional data
}
func (j *Journey) GetFirstTrip() *Trip {
if len(j.Trips) == 0 {
return nil
}
return j.Trips[0]
}
func (j *Journey) GetOrigin() *StopStation {
first := j.GetFirstTrip()
if first == nil {
return nil
}
return first.Origin
}
func (j *Journey) GetDeparture() time.Time {
first := j.GetFirstTrip()
if first == nil {
return time.Time{}
}
return first.Departure.Time
}
func (j *Journey) GetDeparturePlatform() string {
first := j.GetFirstTrip()
if first == nil {
return ""
}
return first.DeparturePlatform
}
func (j *Journey) GetDepartureDelay() *int {
first := j.GetFirstTrip()
if first == nil {
return nil
}
return first.DepartureDelay
}
func (j *Journey) GetLastTrip() *Trip {
if len(j.Trips) == 0 {
return nil
}
return j.Trips[len(j.Trips)-1]
}
func (j *Journey) GetDestination() *StopStation {
last := j.GetLastTrip()
if last == nil {
return nil
}
return last.Origin
}
func (j *Journey) GetArrival() time.Time {
last := j.GetLastTrip()
if last == nil {
return time.Time{}
}
return last.Arrival.Time
}
func (j *Journey) GetArrivalPlatform() string {
last := j.GetLastTrip()
if last == nil {
return ""
}
return last.ArrivalPlatform
}
func (j *Journey) GetArrivalDelay() *int {
last := j.GetLastTrip()
if last == nil {
return nil
}
return last.ArrivalDelay
}
// Trip is a formalized, inferred version of a journey leg
type Trip struct {
Origin *StopStation `json:"origin,omitempty" bson:"origin,omitempty"`
Destination *StopStation `json:"destination,omitempty" bson:"destination,omitempty"`
Departure TimeNullable `json:"departure,omitempty" bson:"departure,omitempty"`
DepartureDelay *int `json:"departureDelay,omitempty" bson:"departureDelay,omitempty"`
DeparturePlatform string `json:"departurePlatform,omitempty" bson:"departurePlatform,omitempty"`
Arrival TimeNullable `json:"arrival,omitempty" bson:"arrival,omitempty"`
ArrivalDelay *int `json:"arrivalDelay,omitempty" bson:"arrivalDelay,omitempty"`
ArrivalPlatform string `json:"arrivalPlatform,omitempty" bson:"arrivalPlatform,omitempty"`
Schedule *Schedule `json:"schedule,omitempty" bson:"schedule,omitempty"`
Stopovers []*Stopover `json:"stopovers,omitempty" bson:"stopovers,omitempty"`
Mode Mode `json:"mode,omitempty" bson:"mode,omitempty"`
SubMode string `json:"subMode,omitempty" bson:"subMode,omitempty"`
Public *bool `json:"public,omitempty" bson:"public,omitempty"`
Operator *Operator `json:"operator,omitempty" bson:"operator,omitempty"`
Price *Price `json:"price,omitempty" bson:"price,omitempty"`
// Some additional arguments, inspired by https://github.com/public-transport/hafas-client
Line *Line `json:"line,omitempty" bson:"line,omitempty"` // The line on which this trip is going
Direction string `json:"direction,omitempty" bson:"direction,omitempty"` // The direction string on the train
Polyline string `json:"polyline,omitempty" bson:"polyline,omitempty"` // The polyline of the trip
Meta interface{} `json:"meta,omitempty" bson:"meta,omitempty"` // any additional data
}
type Price struct {
Amount float64 `json:"amount,omitempty" bson:"amount,omitempty"`
Currency string `json:"currency,omitempty" bson:"currency,omitempty"`
Meta interface{} `json:"meta,omitempty" bson:"meta,omitempty"` // any additional data
}
// GetMode The mode of a trip can be defined in many places.
// This method finds the mode of a given trip.
func (trip *Trip) GetMode() *Mode {
if trip.Mode != "" {
return &trip.Mode
}
if trip.Line != nil && trip.Line.Mode != "" {
return &trip.Line.Mode
}
if trip.Schedule != nil {
if trip.Schedule.Mode != "" {
return &trip.Schedule.Mode
}
if trip.Schedule.Route != nil && trip.Schedule.Route.Mode != "" {
return &trip.Schedule.Route.Mode
}
if trip.Schedule.Route != nil && trip.Schedule.Route.Line != nil && trip.Schedule.Route.Line.Mode != "" {
return &trip.Schedule.Route.Line.Mode
}
}
return nil
}
// GetLine The line of a trip can be defined in multiple places
// This method finds it
func (trip *Trip) GetLine() *Line {
if trip.Line != nil {
return trip.Line
}
if trip.Schedule != nil && trip.Schedule.Route != nil && trip.Schedule.Route.Line != nil {
return trip.Schedule.Route.Line
}
return nil
}
func (trip *Trip) SubTrip(startInclusive int, endExclusive int) *Trip {
stopovers := trip.Stopovers[startInclusive:endExclusive]
origin := stopovers[0]
dest := stopovers[len(stopovers)-1]
return &Trip{
Origin: origin.StopStation,
Destination: dest.StopStation,
Departure: origin.Departure,
DepartureDelay: origin.DepartureDelay,
DeparturePlatform: origin.DeparturePlatform,
Arrival: dest.Arrival,
ArrivalDelay: dest.ArrivalDelay,
ArrivalPlatform: dest.ArrivalPlatform,
Schedule: trip.Schedule,
Stopovers: stopovers,
Mode: trip.Mode,
SubMode: trip.SubMode,
Public: trip.Public,
Operator: trip.Operator,
Price: nil,
Line: trip.Line,
Direction: trip.Direction,
Polyline: trip.Polyline,
Meta: trip.Meta,
}
}
type mJourney struct {
Typed `bson:"inline"`
Id string `json:"id,omitempty" bson:"id,omitempty"`
Trips []*Trip `json:"legs,omitempty" bson:"legs,omitempty"`
Price *Price `json:"price,omitempty" bson:"price,omitempty"`
Meta interface{} `json:"meta,omitempty" bson:"meta,omitempty"`
}
func (j *Journey) toM() *mJourney {
return &mJourney{
Typed: typedJourney,
Id: j.Id,
Trips: j.Trips,
Price: j.Price,
Meta: j.Meta,
}
}
func (j *Journey) fromM(m *mJourney) {
j.Id = m.Id
j.Trips = m.Trips
j.Price = m.Price
j.Meta = m.Meta
}
// as it is optional to give either line id or Line object,
// we have to unmarshal|marshal it ourselves.
func (j *Journey) UnmarshalJSON(data []byte) error {
var m mJourney
err := json.Unmarshal(data, &m)
if err != nil {
return err
}
j.fromM(&m)
return nil
}
func (j *Journey) MarshalJSON() ([]byte, error) {
return json.Marshal(j.toM())
}
func (j *Journey) UnmarshalBSON(data []byte) error {
var m mJourney
err := bson.Unmarshal(data, &m)
if err != nil {
return err
}
j.fromM(&m)
return nil
}
func (j *Journey) UnmarshalBSONValue(typ bsontype.Type, data []byte) error {
var m mJourney
err := bson.UnmarshalValue(typ, data, &m)
if err != nil {
return err
}
j.fromM(&m)
return nil
}
func (j Journey) MarshalBSON() ([]byte, error) {
return bson.Marshal(j.toM())
}
func (j Journey) MarshalBSONValue() (bsontype.Type, []byte, error) {
return bson.MarshalValue(j.toM())
}