-
Notifications
You must be signed in to change notification settings - Fork 0
/
stops.go
47 lines (39 loc) · 848 Bytes
/
stops.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
package merged
import (
"bytes"
"encoding/gob"
"fmt"
"github.com/PiotrKozimor/krkstops/pkg/ttss"
)
type Stops map[uint]Stop
type Stop struct {
Name string
Tram, Bus bool
}
func Do(bus, tram []ttss.Stop) (Stops, error) {
stopsM := make(Stops, len(bus))
for _, stop := range bus {
stopsM[stop.Id] = Stop{Name: stop.Name, Bus: true}
}
for _, stop := range tram {
if s, ok := stopsM[stop.Id]; ok {
if s.Name != stop.Name {
return nil, fmt.Errorf("got different name for stop %d\tbus: '%s'\ttram: '%s'", stop.Id, s.Name, stop.Name)
}
s.Tram = true
stopsM[stop.Id] = s
} else {
stopsM[stop.Id] = Stop{
Name: stop.Name,
Tram: true,
}
}
}
return stopsM, nil
}
func Read(b []byte) (Stops, error) {
var s Stops
buf := bytes.NewBuffer(b)
err := gob.NewDecoder(buf).Decode(&s)
return s, err
}