-
Notifications
You must be signed in to change notification settings - Fork 4
/
common.go
102 lines (83 loc) · 2.47 KB
/
common.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
package models
import (
"strings"
"time"
)
// Station is a station in the railway network. It has a code and 3 names (short, medium, long)
type Station struct {
Code string `json:"code"`
NameShort string `json:"short"`
NameMedium string `json:"medium"`
NameLong string `json:"long"`
}
// Material is the physical train unit
type Material struct {
NaterialType string `json:"type"`
Number string `json:"number"`
Position int `json:"position"`
DestinationActual Station `json:"destination_actual"`
DestinationPlanned Station `json:"destination_planned"`
Accessible bool `json:"accessible"`
Closed bool `json:"closed"`
RemainsBehind bool `json:"remains_behind"`
Added bool `json:"added"`
AlreadyRemoved bool `json:"already_removed"`
Modifications []Modification
}
// StoreItem is for shared fields like ID, timestamp etc.
type StoreItem struct {
ID string `json:"-"`
Timestamp time.Time `json:"-"`
ProductID string `json:"-"`
}
// NormalizedNumber returns a normal material number (i.e. it removes the 000000-0...-0 prefixes and suffixes).
// Example: 000000-09547-0 will be translated to 9547
func (material Material) NormalizedNumber() *string {
if material.Number == "" {
return nil
}
number := strings.TrimRight(strings.TrimRight(strings.TrimLeft(material.Number, "0-"), "0"), "0")
number = strings.ReplaceAll(number, "-", "")
// Translate TRAXX numbers (e.g., 186012 becomes 186.012)
if len(number) == 6 {
number = number[:3] + "." + number[3:]
}
return &number
}
func stationsLongString(stations []Station, separator string) string {
stationsText := ""
for index, station := range stations {
if index > 0 {
stationsText += separator
}
stationsText += station.NameLong
}
return stationsText
}
func stationsMediumString(stations []Station, separator string) string {
stationsText := ""
for index, station := range stations {
if index > 0 {
stationsText += separator
}
stationsText += station.NameMedium
}
return stationsText
}
func stationsShortString(stations []Station, separator string) string {
stationsText := ""
for index, station := range stations {
if index > 0 {
stationsText += separator
}
stationsText += station.NameShort
}
return stationsText
}
func stationCodes(stations []Station) []string {
var stationCodes []string
for _, station := range stations {
stationCodes = append(stationCodes, station.Code)
}
return stationCodes
}