Skip to content

Commit

Permalink
model: separate package (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
matslina authored Jan 6, 2024
1 parent 16ac901 commit e30f5c6
Show file tree
Hide file tree
Showing 28 changed files with 548 additions and 515 deletions.
5 changes: 2 additions & 3 deletions cmd/departures.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (

"github.com/spf13/cobra"

"tidbyt.dev/gtfs"
"tidbyt.dev/gtfs/storage"
"tidbyt.dev/gtfs/model"
)

var departuresCmd = &cobra.Command{
Expand Down Expand Up @@ -35,7 +34,7 @@ func departures(cmd *cobra.Command, args []string) error {
stopID := args[0]

type DepartureProvider interface {
Departures(string, time.Time, time.Duration, int, string, int8, []storage.RouteType) ([]gtfs.Departure, error)
Departures(string, time.Time, time.Duration, int, string, int8, []model.RouteType) ([]model.Departure, error)
}

var provider DepartureProvider
Expand Down
125 changes: 77 additions & 48 deletions storage/model.go → model/model.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,64 @@
package storage
package model

import (
"strconv"
"time"
)

// Holds all external facing types and constants.

type LocationType int

const (
LocationTypeStop LocationType = iota
LocationTypeStation
LocationTypeEntranceExit
LocationTypeGenericNode
LocationTypeBoardingArea
)

type RouteType int

const (
RouteTypeTram RouteType = 0
RouteTypeSubway = 1
RouteTypeRail = 2
RouteTypeBus = 3
RouteTypeFerry = 4
RouteTypeCable = 5
RouteTypeAerial = 6
RouteTypeFunicular = 7
RouteTypeTrolleybus = 11
RouteTypeMonorail = 12
)

type ExceptionType int

const (
ExceptionTypeAdded ExceptionType = 1
ExceptionTypeRemoved = 2
)

type Agency struct {
ID string
Name string
URL string
Timezone string
}

type Calendar struct {
ServiceID string
StartDate string
EndDate string
Weekday int8
}

type CalendarDate struct {
ServiceID string
Date string
ExceptionType ExceptionType
}

type Stop struct {
ID string
Code string
Expand All @@ -25,16 +72,6 @@ type Stop struct {
PlatformCode string
}

type LocationType int

const (
LocationTypeStop LocationType = iota
LocationTypeStation
LocationTypeEntranceExit
LocationTypeGenericNode
LocationTypeBoardingArea
)

type Trip struct {
ID string
RouteID string
Expand All @@ -44,6 +81,18 @@ type Trip struct {
DirectionID int8
}

type Route struct {
ID string
AgencyID string
ShortName string
LongName string
Desc string
Type RouteType
URL string
Color string
TextColor string
}

type StopTime struct {
TripID string
StopID string
Expand All @@ -67,43 +116,23 @@ func (st *StopTime) DepartureTime() time.Duration {
return time.Duration(h)*time.Hour + time.Duration(m)*time.Minute + time.Duration(s)*time.Second
}

type Route struct {
ID string
AgencyID string
ShortName string
LongName string
Desc string
Type RouteType
URL string
Color string
TextColor string
}

type RouteType int

const (
RouteTypeTram RouteType = 0
RouteTypeSubway = 1
RouteTypeRail = 2
RouteTypeBus = 3
RouteTypeFerry = 4
RouteTypeCable = 5
RouteTypeAerial = 6
RouteTypeFunicular = 7
RouteTypeTrolleybus = 11
RouteTypeMonorail = 12
)

type Calendar struct {
ServiceID string
StartDate string
EndDate string
Weekday int8
// Holds all Headsigns for trips passing through a stop, for a given
// route and direction.
type RouteDirection struct {
StopID string
RouteID string
DirectionID int8
Headsigns []string
}

type CalendarDate struct {
ServiceID string
Date string
ExceptionType int8
// TODO: Enum for exception types?
// A vehicle departing from a stop.
type Departure struct {
StopID string
RouteID string
TripID string
StopSequence uint32
DirectionID int8
Time time.Time
Headsign string
Delay time.Duration
}
3 changes: 2 additions & 1 deletion parse/agency.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/gocarina/gocsv"

"tidbyt.dev/gtfs/model"
"tidbyt.dev/gtfs/storage"
)

Expand Down Expand Up @@ -65,7 +66,7 @@ func ParseAgency(writer storage.FeedWriter, data io.Reader) (map[string]bool, st
return nil, "", fmt.Errorf("missing agency_url")
}

writer.WriteAgency(&storage.Agency{
writer.WriteAgency(&model.Agency{
ID: a.ID,
Name: a.Name,
URL: a.URL,
Expand Down
13 changes: 7 additions & 6 deletions parse/agency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"tidbyt.dev/gtfs/model"
"tidbyt.dev/gtfs/storage"
)

Expand All @@ -17,7 +18,7 @@ func TestParseAgency(t *testing.T) {
content string
agencyIDs map[string]bool
timezone string
agencies []*storage.Agency
agencies []*model.Agency
err bool
}{
{
Expand All @@ -27,7 +28,7 @@ agency_name,agency_url,agency_timezone
Agency Name,http://www.example.com,America/New_York`,
map[string]bool{"": true},
"America/New_York",
[]*storage.Agency{&storage.Agency{
[]*model.Agency{&model.Agency{
Name: "Agency Name",
URL: "http://www.example.com",
Timezone: "America/New_York",
Expand All @@ -44,20 +45,20 @@ agency_id,agency_name,agency_url,agency_timezone
3,Agency Three,http://www.example.com/three,America/New_York`,
map[string]bool{"1": true, "2": true, "3": true},
"America/New_York",
[]*storage.Agency{
&storage.Agency{
[]*model.Agency{
&model.Agency{
ID: "1",
Name: "Agency One",
URL: "http://www.example.com/one",
Timezone: "America/New_York",
},
&storage.Agency{
&model.Agency{
ID: "2",
Name: "Agency Two",
URL: "http://www.example.com/two",
Timezone: "America/New_York",
},
&storage.Agency{
&model.Agency{
ID: "3",
Name: "Agency Three",
URL: "http://www.example.com/three",
Expand Down
3 changes: 2 additions & 1 deletion parse/calendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/gocarina/gocsv"

"tidbyt.dev/gtfs/model"
"tidbyt.dev/gtfs/storage"
)

Expand Down Expand Up @@ -99,7 +100,7 @@ func ParseCalendar(writer storage.FeedWriter, data io.Reader) (map[string]bool,
maxDate = c.EndDate
}

err = writer.WriteCalendar(&storage.Calendar{
err = writer.WriteCalendar(&model.Calendar{
ServiceID: c.ServiceID,
StartDate: c.StartDate,
EndDate: c.EndDate,
Expand Down
5 changes: 3 additions & 2 deletions parse/calendar_dates.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/gocarina/gocsv"

"tidbyt.dev/gtfs/model"
"tidbyt.dev/gtfs/storage"
)

Expand Down Expand Up @@ -54,10 +55,10 @@ func ParseCalendarDates(
maxDate = cd.Date
}

writer.WriteCalendarDate(&storage.CalendarDate{
writer.WriteCalendarDate(&model.CalendarDate{
ServiceID: cd.ServiceID,
Date: cd.Date,
ExceptionType: cd.ExceptionType,
ExceptionType: model.ExceptionType(cd.ExceptionType),
})
}

Expand Down
23 changes: 12 additions & 11 deletions parse/calendar_dates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"tidbyt.dev/gtfs/model"
"tidbyt.dev/gtfs/storage"
)

func TestCalendarDates(t *testing.T) {
for _, tc := range []struct {
name string
content string
expected []*storage.CalendarDate
expected []*model.CalendarDate
minDate string
maxDate string
err bool
Expand All @@ -25,11 +26,11 @@ func TestCalendarDates(t *testing.T) {
`
service_id,date,exception_type
s1,20170101,1`,
[]*storage.CalendarDate{
&storage.CalendarDate{
[]*model.CalendarDate{
&model.CalendarDate{
ServiceID: "s1",
Date: "20170101",
ExceptionType: 1,
ExceptionType: model.ExceptionTypeAdded,
},
},
"20170101",
Expand All @@ -44,21 +45,21 @@ service_id,date,exception_type
s1,20170101,1
s1,20170102,2
s2,20170103,1`,
[]*storage.CalendarDate{
&storage.CalendarDate{
[]*model.CalendarDate{
&model.CalendarDate{
ServiceID: "s1",
Date: "20170101",
ExceptionType: 1,
ExceptionType: model.ExceptionTypeAdded,
},
&storage.CalendarDate{
&model.CalendarDate{
ServiceID: "s1",
Date: "20170102",
ExceptionType: 2,
ExceptionType: model.ExceptionTypeRemoved,
},
&storage.CalendarDate{
&model.CalendarDate{
ServiceID: "s2",
Date: "20170103",
ExceptionType: 1,
ExceptionType: model.ExceptionTypeAdded,
},
},
"20170101",
Expand Down
9 changes: 5 additions & 4 deletions parse/calendar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"tidbyt.dev/gtfs/model"
"tidbyt.dev/gtfs/storage"
)

func TestCalendar(t *testing.T) {
for _, tc := range []struct {
name string
content string
expected []*storage.Calendar
expected []*model.Calendar
minDate string
maxDate string
err bool
Expand All @@ -27,7 +28,7 @@ func TestCalendar(t *testing.T) {
service_id,start_date,end_date
s,20170101,20170131`,

[]*storage.Calendar{
[]*model.Calendar{
{
ServiceID: "s",
Weekday: 0,
Expand All @@ -45,7 +46,7 @@ s,20170101,20170131`,
`
service_id,monday,tuesday,wednesday,thursday,friday,saturday,sunday,start_date,end_date
s,1,1,1,1,1,1,1,20170101,20170131`,
[]*storage.Calendar{
[]*model.Calendar{
{
ServiceID: "s",
Weekday: 127,
Expand All @@ -65,7 +66,7 @@ service_id,monday,tuesday,wednesday,thursday,friday,saturday,sunday,start_date,e
s1,1,1,1,1,1,1,1,20170101,20170131
s2,1,1,1,1,1,0,0,20171001,20180201
s3,1,1,0,1,1,0,1,20161225,20170202`,
[]*storage.Calendar{
[]*model.Calendar{
{
ServiceID: "s1",
Weekday: 127,
Expand Down
Loading

0 comments on commit e30f5c6

Please sign in to comment.