depot/go/trains/webapi/structs.go

198 lines
4.2 KiB
Go
Raw Normal View History

2021-11-18 22:24:20 +00:00
package webapi
import (
"time"
)
type TrainOperator struct {
Code string `json:"code"`
Name *string `json:"name"`
URL *string `json:"url"`
}
func (toc *TrainOperator) IsEmpty() bool {
return toc == nil || toc.Code == ""
}
func (toc *TrainOperator) Canonicalize() {
if toc == nil {
return
}
if *toc.Name == "" {
toc.Name = nil
}
if *toc.URL == "" {
toc.URL = nil
}
}
type Location struct {
TIPLOC string `json:"tiploc"`
Name string `json:"name"`
CRS *string `json:"crs"`
TOC *TrainOperator `json:"operator"`
}
func (loc *Location) IsEmpty() bool {
return loc == nil || loc.TIPLOC == ""
}
func (loc *Location) Canonicalize() {
if loc == nil {
return
}
if loc.CRS == nil || *loc.CRS == "" {
loc.CRS = nil
}
if loc.TOC.IsEmpty() {
loc.TOC = nil
}
loc.TOC.Canonicalize()
}
type DisruptionReason struct {
Code int `json:"code"`
Text string `json:"text"`
Location *Location `json:"location"`
NearLocation bool `json:"near_location"`
}
func (dr *DisruptionReason) IsEmpty() bool {
return dr == nil || dr.Code == 0
}
func (dr *DisruptionReason) Canonicalize() {
if dr == nil {
return
}
if dr.Location.IsEmpty() {
dr.Location = nil
}
dr.Location.Canonicalize()
}
type ServiceData struct {
ID int `json:"id"`
RID string `json:"rid"`
UID string `json:"uid"`
RSID string `json:"rsid"`
Headcode string `json:"headcode"`
StartDate string `json:"scheduled_start_date"`
TrainOperator TrainOperator `json:"operator"`
DelayReason *DisruptionReason `json:"delay_reason"`
CancelReason *DisruptionReason `json:"cancel_reason"`
Active bool `json:"is_active"`
Deleted bool `json:"is_deleted"`
Cancelled bool `json:"is_cancelled"`
Locations []ServiceLocation `json:"locations"`
}
func (sd *ServiceData) Canonicalize() {
if sd.DelayReason.IsEmpty() {
sd.DelayReason = nil
}
sd.DelayReason.Canonicalize()
if sd.CancelReason.IsEmpty() {
sd.CancelReason = nil
}
sd.CancelReason.Canonicalize()
}
type TimingData struct {
PublicScheduled *time.Time `json:"public_scheduled"`
WorkingScheduled *time.Time `json:"working_scheduled"`
PublicEstimated *time.Time `json:"public_estimated"`
WorkingEstimated *time.Time `json:"working_estimated"`
Actual *time.Time `json:"actual"`
}
func (td *TimingData) IsEmpty() bool {
if td == nil {
return true
}
return td.PublicScheduled == nil && td.WorkingScheduled == nil && td.PublicEstimated == nil && td.WorkingEstimated == nil && td.Actual == nil
}
func (td *TimingData) Canonicalize() {
if td == nil {
return
}
ts := []**time.Time{&td.PublicScheduled, &td.WorkingScheduled, &td.PublicEstimated, &td.WorkingEstimated, &td.Actual}
for _, t := range ts {
if *t == nil || (*t).IsZero() {
*t = nil
}
}
}
type PlatformData struct {
Scheduled string `json:"scheduled"`
Live string `json:"live"`
Confirmed bool `json:"confirmed"`
Suppressed bool `json:"platform_suppressed"`
}
func (pd *PlatformData) IsEmpty() bool {
return pd.Scheduled == "" && pd.Live == ""
}
func (pd *PlatformData) Canonicalize() {}
type ServiceLocation struct {
ID int `json:"id"`
Location *Location `json:"location"`
CallingPointType string `json:"calling_point_type"`
Length *int `json:"train_length"`
Suppressed bool `json:"service_suppressed"`
Platform *PlatformData `json:"platform"`
Cancelled bool `json:"cancelled"`
FalseDestination *Location `json:"false_destination"`
ArrivalTiming *TimingData `json:"arrival_timing"`
DepartureTiming *TimingData `json:"departure_timing"`
PassTiming *TimingData `json:"pass_timing"`
}
func (sl *ServiceLocation) Canonicalize() {
if sl == nil {
return
}
sl.Location.Canonicalize()
if sl.Length == nil || *sl.Length == 0 {
sl.Length = nil
}
if sl.Platform.IsEmpty() {
sl.Platform = nil
}
sl.Platform.Canonicalize()
if sl.FalseDestination.IsEmpty() {
sl.FalseDestination = nil
}
sl.FalseDestination.Canonicalize()
if sl.ArrivalTiming.IsEmpty() {
sl.ArrivalTiming = nil
}
sl.ArrivalTiming.Canonicalize()
if sl.DepartureTiming.IsEmpty() {
sl.DepartureTiming = nil
}
sl.DepartureTiming.Canonicalize()
if sl.PassTiming.IsEmpty() {
sl.PassTiming = nil
}
sl.PassTiming.Canonicalize()
}