Skip to content

Commit

Permalink
extract netex model as external dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
clezag committed Feb 21, 2025
1 parent be9c2c4 commit 8e44f42
Show file tree
Hide file tree
Showing 19 changed files with 245 additions and 694 deletions.
41 changes: 21 additions & 20 deletions src/netex/flights.go → src/comp/flights.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
// SPDX-FileCopyrightText: NOI Techpark <[email protected]>
// SPDX-License-Identifier: AGPL-3.0-or-later

package netex
package comp

import "github.com/noi-techpark/go-netex"

type StFlightData struct {
Operators []Operator
StopPlaces []StopPlace
ServiceCalendars []ServiceCalendar
Routes []Route
Lines []Line
ScheduledStopPoints []ScheduledStopPoint
ServiceLinks []ServiceLink
StopAssignments []PassengerStopAssignment
JourneyPatterns []ServiceJourneyPattern
VehicleJourneys []ServiceJourney
Operators []netex.Operator
StopPlaces []netex.StopPlace
ServiceCalendars []netex.ServiceCalendar
Routes []netex.Route
Lines []netex.Line
ScheduledStopPoints []netex.ScheduledStopPoint
ServiceLinks []netex.ServiceLink
StopAssignments []netex.PassengerStopAssignment
JourneyPatterns []netex.ServiceJourneyPattern
VehicleJourneys []netex.ServiceJourney
}

type StFlights interface {
StFlights() (StFlightData, error)
}

func GetFlights(ps []StFlights) ([]CompositeFrame, error) {
ret := []CompositeFrame{}
func GetFlights(ps []StFlights) ([]netex.CompositeFrame, error) {
ret := []netex.CompositeFrame{}

apd := StFlightData{}

Expand All @@ -47,24 +49,23 @@ func GetFlights(ps []StFlights) ([]CompositeFrame, error) {
return ret, nil
}

func compFlights(pd StFlightData) CompositeFrame {
var ret CompositeFrame
ret.Defaults()
func compFlights(pd StFlightData) netex.CompositeFrame {
ret := DefaultCompositFrame()
ret.Id = CreateFrameId("CompositeFrame_EU_PI_STOP_OFFER", "FLIGHTS", "ita")
ret.TypeOfFrameRef = MkTypeOfFrameRef("EU_PI_LINE_OFFER")

site := siteFrame()
site.StopPlaces = pd.StopPlaces
ret.Frames.Frames = append(ret.Frames.Frames, &site)

res := ResourceFrame{}
res := netex.ResourceFrame{}
res.Id = CreateFrameId("ResourceFrame_EU_PI_MOBILITY", "ita")
res.Version = "1"
res.TypeOfFrameRef = MkTypeOfFrameRef("EU_PI_COMMON")
res.Operators = &pd.Operators
ret.Frames.Frames = append(ret.Frames.Frames, &res)

ser := ServiceFrame{}
ser := netex.ServiceFrame{}
ser.Id = CreateFrameId("ServiceFrame_EU_PI_NETWORK", "ita")
ser.Version = "1"
ser.TypeOfFrameRef = MkTypeOfFrameRef("EU_PI_NETWORK")
Expand All @@ -76,14 +77,14 @@ func compFlights(pd StFlightData) CompositeFrame {
ser.StopAssignments = append(ser.StopAssignments, pd.StopAssignments...)
ret.Frames.Frames = append(ret.Frames.Frames, ser)

cal := ServiceCalendarFrame{}
cal := netex.ServiceCalendarFrame{}
cal.Id = CreateFrameId("ServiceCalendarFrame_EU_PI_CALENDAR", "ita")
cal.Version = "1"
cal.TypeOfFrameRef = MkTypeOfFrameRef("EU_PI_CALENDAR")
cal.ServiceCalendar = append(cal.ServiceCalendar, pd.ServiceCalendars...)
ret.Frames.Frames = append(ret.Frames.Frames, cal)

tim := TimetableFrame{}
tim := netex.TimetableFrame{}
tim.Id = CreateFrameId("TimetableFrame_EU_PI_TIMETABLE", "ita")
tim.Version = "1"
tim.TypeOfFrameRef = MkTypeOfFrameRef("EU_PI_TIMETABLE")
Expand Down
28 changes: 22 additions & 6 deletions src/netex/netex.go → src/comp/netex.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// SPDX-FileCopyrightText: NOI Techpark <[email protected]>
// SPDX-License-Identifier: AGPL-3.0-or-later

package netex
package comp

import (
"regexp"
"time"

"github.com/noi-techpark/go-netex"
)

// As per NeTEx spec, IDs must only contain non-accented charaters, numbers, hyphens and underscores
Expand All @@ -23,25 +25,27 @@ func CreateFrameId(segments ...string) string {
return "edp:" + CreateID(segments...)
}

func MkRef(tp string, id string) Ref {
r := Ref{}
func MkRef(tp string, id string) netex.Ref {
r := netex.Ref{}
r.Ref = id
r.Version = "1"
r.XMLName.Local = tp + "Ref"
return r
}

func MkTypeOfFrameRef(tp string) Ref {
r := Ref{}
func MkTypeOfFrameRef(tp string) netex.Ref {
r := netex.Ref{}
r.Ref = "epip:" + tp
r.Version = "1"
r.XMLName.Local = "TypeOfFrameRef"
return r
}

func (v *ValidBetween) AYear() {
func ValidAYear() netex.ValidBetween {
v := netex.ValidBetween{}
v.FromDate = time.Now().Truncate(time.Hour * 24)
v.ToDate = time.Now().AddDate(1, 0, 0).Truncate(time.Hour * 24)
return v
}

func AppendSafe[T any](h *[]T, t ...T) *[]T {
Expand All @@ -54,3 +58,15 @@ func AppendSafe[T any](h *[]T, t ...T) *[]T {
}
return h
}

func DefaultCompositFrame() netex.CompositeFrame {
c := netex.CompositeFrame{}
c.Version = "1"
c.ValidBetween = ValidAYear()
c.Codespaces.Codespace.Id = "ita"
c.Codespaces.Codespace.Xmlns = "ita"
c.Codespaces.Codespace.XmlnsUrl = "http://www.ita.it"
c.Codespaces.Codespace.Description = "Italian Profile"
c.FrameDefaults.DefaultCodespaceRef.Ref = "ita"
return c
}
7 changes: 4 additions & 3 deletions src/netex/operators.go → src/comp/operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package netex
package comp

import (
"log"
"opendatahub/transmodel-api/config"

"github.com/noi-techpark/go-netex"
"golang.org/x/exp/maps"
)

Expand All @@ -31,14 +32,14 @@ func GetOperatorOrigins(c *config.Config, id string) []string {
return origins
}

func GetOperator(c *config.Config, id string) Operator {
func GetOperator(c *config.Config, id string) netex.Operator {
mapped := mapByOrigin(c.Operators)
cfg, found := mapped[id]
if !found {
log.Panicln("Unable to map operator. Probably got some origin that we shouldn't have?", id, maps.Keys(mapped))
}

o := Operator{}
o := netex.Operator{}
o.Id = CreateID("Operator", cfg.Id)
o.Version = "1"
o.PrivateCode = cfg.Id
Expand Down
2 changes: 1 addition & 1 deletion src/netex/operators_test.go → src/comp/operators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package netex
package comp

import (
"opendatahub/transmodel-api/config"
Expand Down
23 changes: 12 additions & 11 deletions src/netex/parking.go → src/comp/parking.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// SPDX-FileCopyrightText: NOI Techpark <[email protected]>
// SPDX-License-Identifier: AGPL-3.0-or-later

package netex
package comp

import "github.com/noi-techpark/go-netex"

type OdhEcharging struct {
Scode string
Expand All @@ -18,16 +20,15 @@ type OdhEcharging struct {
}
}

func compFrame(pd StParkingData) CompositeFrame {
var ret CompositeFrame
ret.Defaults()
func compFrame(pd StParkingData) netex.CompositeFrame {
ret := DefaultCompositFrame()
ret.Id = CreateFrameId("CompositeFrame_EU_PI_STOP_OFFER", "PARKING", "ita")
ret.TypeOfFrameRef = MkTypeOfFrameRef("EU_PI_LINE_OFFER")

site := siteFrame()
ret.Frames.Frames = append(ret.Frames.Frames, &site)

res := ResourceFrame{}
res := netex.ResourceFrame{}
res.Id = CreateFrameId("ResourceFrame_EU_PI_MOBILITY", "ita")
res.Version = "1"
res.TypeOfFrameRef = MkTypeOfFrameRef("EU_PI_COMMON")
Expand All @@ -40,16 +41,16 @@ func compFrame(pd StParkingData) CompositeFrame {
}

type StParkingData struct {
Parkings []Parking
Operators []Operator
Parkings []netex.Parking
Operators []netex.Operator
}

type StParking interface {
StParking() (StParkingData, error)
}

func GetParking(ps []StParking) ([]CompositeFrame, error) {
ret := []CompositeFrame{}
func GetParking(ps []StParking) ([]netex.CompositeFrame, error) {
ret := []netex.CompositeFrame{}

apd := StParkingData{}

Expand All @@ -67,8 +68,8 @@ func GetParking(ps []StParking) ([]CompositeFrame, error) {
return ret, nil
}

func siteFrame() SiteFrame {
var site SiteFrame
func siteFrame() netex.SiteFrame {
var site netex.SiteFrame
site.Id = CreateFrameId("SiteFrame_EU_PI_STOP", "ita")
site.Version = "1"
site.TypeOfFrameRef = MkTypeOfFrameRef("EU_PI_STOP")
Expand Down
39 changes: 20 additions & 19 deletions src/netex/sharing.go → src/comp/sharing.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// SPDX-FileCopyrightText: NOI Techpark <[email protected]>
// SPDX-License-Identifier: AGPL-3.0-or-later

package netex
package comp

import (
"encoding/json"
"log/slog"

"github.com/noi-techpark/go-netex"
)

type Company struct {
Expand All @@ -28,23 +30,23 @@ func (pc *Company) UnmarshalJSON(p []byte) error {
}

type StSharingData struct {
Fleets []Fleet
Vehicles []Vehicle
CarModels []CarModelProfile
CycleModels []CycleModelProfile
Operators []Operator
Modes []VehicleSharing
Services []VehicleSharingService
Constraints []MobilityServiceConstraintZone
Parkings []Parking
Fleets []netex.Fleet
Vehicles []netex.Vehicle
CarModels []netex.CarModelProfile
CycleModels []netex.CycleModelProfile
Operators []netex.Operator
Modes []netex.VehicleSharing
Services []netex.VehicleSharingService
Constraints []netex.MobilityServiceConstraintZone
Parkings []netex.Parking
}

type StSharing interface {
StSharing() (StSharingData, error)
}

func GetSharing(bikeProviders []StSharing, carProviders []StSharing) ([]CompositeFrame, error) {
ret := []CompositeFrame{}
func GetSharing(bikeProviders []StSharing, carProviders []StSharing) ([]netex.CompositeFrame, error) {
ret := []netex.CompositeFrame{}

c, err := compSharing("BikeSharing", bikeProviders)
if err != nil {
Expand All @@ -60,26 +62,26 @@ func GetSharing(bikeProviders []StSharing, carProviders []StSharing) ([]Composit

return ret, nil
}
func compSharing(serviceName string, ps []StSharing) (CompositeFrame, error) {
mob := MobilityServiceFrame{}
func compSharing(serviceName string, ps []StSharing) (netex.CompositeFrame, error) {
mob := netex.MobilityServiceFrame{}
mob.Id = CreateFrameId("MobilityServiceFrame_EU_PI_MOBILITY", serviceName)
mob.Version = "1"
mob.FrameDefaults.DefaultCurrency = "EUR"

res := ResourceFrame{}
res := netex.ResourceFrame{}
res.Id = CreateFrameId("ResourceFrame_EU_PI_COMMON", serviceName)
res.Version = "1"
res.TypeOfFrameRef = MkTypeOfFrameRef("EU_PI_COMMON")

site := SiteFrame{}
site := netex.SiteFrame{}
site.Id = CreateFrameId("SiteFrame_EU_PI_STOP", serviceName)
site.Version = "1"
site.TypeOfFrameRef = MkTypeOfFrameRef("EU_PI_STOP")

for _, p := range ps {
d, err := p.StSharing()
if err != nil {
return CompositeFrame{}, err
return netex.CompositeFrame{}, err
}

mob.Fleets = append(mob.Fleets, d.Fleets...)
Expand All @@ -95,8 +97,7 @@ func compSharing(serviceName string, ps []StSharing) (CompositeFrame, error) {
site.Parkings = append(site.Parkings, d.Parkings...)
}

comp := CompositeFrame{}
comp.Defaults()
comp := DefaultCompositFrame()
comp.Id = CreateFrameId("CompositeFrame_EU_PI_STOP_OFFER", "SHARING", serviceName)
comp.TypeOfFrameRef = MkTypeOfFrameRef("EU_PI_LINE_OFFER")
comp.Frames.Frames = append(comp.Frames.Frames, mob, res, site)
Expand Down
5 changes: 4 additions & 1 deletion src/go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
module opendatahub/transmodel-api

go 1.23
go 1.23.6

replace github.com/noi-techpark/go-netex => ../../go-netex

require (
github.com/gin-contrib/cors v1.7.2
github.com/gin-gonic/gin v1.10.0
github.com/noi-techpark/go-netex v1.0.0
github.com/samber/slog-gin v1.13.6
gotest.tools/v3 v3.5.1
)
Expand Down
4 changes: 4 additions & 0 deletions src/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/noi-techpark/go-netex v0.0.0-20250221134253-de5633f29872 h1:yvxq+0Ea8H7yH1y/ya4MOygwIt9n5yZ7al5QPuM2hMc=
github.com/noi-techpark/go-netex v0.0.0-20250221134253-de5633f29872/go.mod h1:DDY9+ROb6h+/gPs97F44tVHbldaXPj3po/gavQgcay8=
github.com/noi-techpark/go-netex v1.0.0 h1:4W9epUZ+6pSfKSfQ3kdzdtHL5tlfPlXe2GBER6LtcEs=
github.com/noi-techpark/go-netex v1.0.0/go.mod h1:DDY9+ROb6h+/gPs97F44tVHbldaXPj3po/gavQgcay8=
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
Loading

0 comments on commit 8e44f42

Please sign in to comment.