-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstopover.go
88 lines (78 loc) · 2.59 KB
/
stopover.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
package fptf
import (
"encoding/json"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsontype"
)
// A Stopover represents a vehicle stopping at a stop/station at a specific time.
type Stopover struct {
StopStation *StopStation
Arrival TimeNullable
ArrivalDelay *int
ArrivalPlatform string
Departure TimeNullable
DepartureDelay *int
DeparturePlatform string
Meta interface{}
}
// intermediate, Typed format used by marshal
type mStopover struct {
Typed `bson:"inline"`
StopStation *StopStation `json:"stop,omitempty" bson:"stop,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"`
Departure TimeNullable `json:"departure,omitempty" bson:"departure,omitempty"`
DepartureDelay *int `json:"departureDelay,omitempty" bson:"departureDelay,omitempty"`
DeparturePlatform string `json:"departurePlatform,omitempty" bson:"departurePlatform,omitempty"`
Meta interface{} `json:"meta,omitempty" bson:"meta,omitempty"`
}
func (s *Stopover) toM() *mStopover {
return &mStopover{
Typed: typedStopover,
StopStation: s.StopStation,
Arrival: s.Arrival,
ArrivalDelay: s.ArrivalDelay,
ArrivalPlatform: s.ArrivalPlatform,
Departure: s.Departure,
DepartureDelay: s.DepartureDelay,
DeparturePlatform: s.DeparturePlatform,
Meta: s.Meta,
}
}
func (s *Stopover) fromM(m *mStopover) {
s.StopStation = m.StopStation
s.Arrival = m.Arrival
s.ArrivalDelay = m.ArrivalDelay
s.ArrivalPlatform = m.ArrivalPlatform
s.Departure = m.Departure
s.DepartureDelay = m.DepartureDelay
s.DeparturePlatform = m.DeparturePlatform
s.Meta = m.Meta
}
// as it is optional to give either stop|station id or Stop or Station object,
// we have to unmarshal|marshal it ourselves.
func (s *Stopover) UnmarshalJSON(data []byte) error {
var m mStopover
err := json.Unmarshal(data, &m)
if err != nil {
return err
}
s.fromM(&m)
return nil
}
func (s *Stopover) MarshalJSON() ([]byte, error) {
return json.Marshal(s.toM())
}
func (s *Stopover) UnmarshalBSONValue(typ bsontype.Type, data []byte) error {
var m mStopover
err := bson.UnmarshalValue(typ, data, &m)
if err != nil {
return err
}
s.fromM(&m)
return nil
}
func (s Stopover) MarshalBSONValue() (bsontype.Type, []byte, error) {
return bson.MarshalValue(s.toM())
}