Skip to content

Commit

Permalink
Merge pull request #1 from Vector-Hector/dev
Browse files Browse the repository at this point in the history
Update v1.0.1
  • Loading branch information
Vector-Hector authored Dec 22, 2023
2 parents 629ac99 + a728057 commit 6c03aac
Show file tree
Hide file tree
Showing 17 changed files with 631 additions and 133 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/Vector-Hector/fptf

go 1.15

require go.mongodb.org/mongo-driver v1.13.1 // indirect
41 changes: 41 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.mongodb.org/mongo-driver v1.13.1 h1:YIc7HTYsKndGK4RFzJ3covLz1byri52x0IoMB0Pt/vk=
go.mongodb.org/mongo-driver v1.13.1/go.mod h1:wcDf1JBCXy2mOW0bWHwO/IOYqdca1MPCwDtFu/Z9+eo=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
91 changes: 62 additions & 29 deletions journey.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package fptf

import (
"encoding/json"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsontype"
"time"
)

Expand Down Expand Up @@ -95,41 +97,43 @@ func (j *Journey) GetArrivalDelay() *int {

// Trip is a formalized, inferred version of a journey leg
type Trip struct {
Origin *StopStation `json:"origin,omitempty"`
Destination *StopStation `json:"destination,omitempty"`
Origin *StopStation `json:"origin,omitempty" bson:"origin,omitempty"`
Destination *StopStation `json:"destination,omitempty" bson:"destination,omitempty"`

Departure TimeNullable `json:"departure,omitempty"`
DepartureDelay *int `json:"departureDelay,omitempty"`
DeparturePlatform string `json:"departurePlatform,omitempty"`
Departure TimeNullable `json:"departure,omitempty" bson:"departure,omitempty"`
DepartureDelay *int `json:"departureDelay,omitempty" bson:"departureDelay,omitempty"`
DeparturePlatform string `json:"departurePlatform,omitempty" bson:"departurePlatform,omitempty"`

Arrival TimeNullable `json:"arrival,omitempty"`
ArrivalDelay *int `json:"arrivalDelay,omitempty"`
ArrivalPlatform string `json:"arrivalPlatform,omitempty"`
Arrival TimeNullable `json:"arrival,omitempty" bson:"arrival,omitempty"`
ArrivalDelay *int `json:"arrivalDelay,omitempty" bson:"arrivalDelay,omitempty"`
ArrivalPlatform string `json:"arrivalPlatform,omitempty" bson:"arrivalPlatform,omitempty"`

Schedule *Schedule `json:"schedule,omitempty"`
Schedule *Schedule `json:"schedule,omitempty" bson:"schedule,omitempty"`

Stopovers []*Stopover `json:"stopovers,omitempty"`
Stopovers []*Stopover `json:"stopovers,omitempty" bson:"stopovers,omitempty"`

Mode Mode `json:"mode,omitempty"`
SubMode string `json:"subMode,omitempty"`
Mode Mode `json:"mode,omitempty" bson:"mode,omitempty"`
SubMode string `json:"subMode,omitempty" bson:"subMode,omitempty"`

Public *bool `json:"public,omitempty"`
Public *bool `json:"public,omitempty" bson:"public,omitempty"`

Operator *Operator `json:"operator,omitempty"`
Operator *Operator `json:"operator,omitempty" bson:"operator,omitempty"`

Price *Price `json:"price,omitempty"`
Price *Price `json:"price,omitempty" bson:"price,omitempty"`

// Some additional arguments, inspired by https://github.com/public-transport/hafas-client
Line *Line `json:"line,omitempty"` // The line on which this trip is going
Direction string `json:"direction,omitempty"` // The direction string on the train
Line *Line `json:"line,omitempty" bson:"line,omitempty"` // The line on which this trip is going
Direction string `json:"direction,omitempty" bson:"direction,omitempty"` // The direction string on the train

Meta interface{} `json:"meta,omitempty"` // any additional data
Polyline string `json:"polyline,omitempty" bson:"polyline,omitempty"` // The polyline of the trip

Meta interface{} `json:"meta,omitempty" bson:"meta,omitempty"` // any additional data
}

type Price struct {
Amount float64 `json:"amount,omitempty"`
Currency string `json:"currency,omitempty"`
Meta interface{} `json:"meta,omitempty"` // any additional data
Amount float64 `json:"amount,omitempty" bson:"amount,omitempty"`
Currency string `json:"currency,omitempty" bson:"currency,omitempty"`
Meta interface{} `json:"meta,omitempty" bson:"meta,omitempty"` // any additional data
}

// GetMode The mode of a trip can be defined in many places.
Expand Down Expand Up @@ -168,9 +172,9 @@ func (trip *Trip) GetLine() *Line {
}

func (trip *Trip) SubTrip(startInclusive int, endExclusive int) *Trip {
stopovers := trip.Stopovers[startInclusive : endExclusive]
stopovers := trip.Stopovers[startInclusive:endExclusive]
origin := stopovers[0]
dest := stopovers[len(stopovers) - 1]
dest := stopovers[len(stopovers)-1]

return &Trip{
Origin: origin.StopStation,
Expand All @@ -190,21 +194,22 @@ func (trip *Trip) SubTrip(startInclusive int, endExclusive int) *Trip {
Price: nil,
Line: trip.Line,
Direction: trip.Direction,
Polyline: trip.Polyline,
Meta: trip.Meta,
}
}

type mJourney struct {
typed
Id string `json:"id"`
Trips []*Trip `json:"legs"`
Price *Price `json:"price"`
Meta interface{} `json:"meta,omitempty"`
Typed `bson:"inline"`
Id string `json:"id,omitempty" bson:"id,omitempty"`
Trips []*Trip `json:"legs,omitempty" bson:"legs,omitempty"`
Price *Price `json:"price,omitempty" bson:"price,omitempty"`
Meta interface{} `json:"meta,omitempty" bson:"meta,omitempty"`
}

func (j *Journey) toM() *mJourney {
return &mJourney{
typed: typedJourney,
Typed: typedJourney,
Id: j.Id,
Trips: j.Trips,
Price: j.Price,
Expand Down Expand Up @@ -235,3 +240,31 @@ func (j *Journey) UnmarshalJSON(data []byte) error {
func (j *Journey) MarshalJSON() ([]byte, error) {
return json.Marshal(j.toM())
}

func (j *Journey) UnmarshalBSON(data []byte) error {
var m mJourney
err := bson.Unmarshal(data, &m)
if err != nil {
return err
}
j.fromM(&m)
return nil
}

func (j *Journey) UnmarshalBSONValue(typ bsontype.Type, data []byte) error {
var m mJourney
err := bson.UnmarshalValue(typ, data, &m)
if err != nil {
return err
}
j.fromM(&m)
return nil
}

func (j Journey) MarshalBSON() ([]byte, error) {
return bson.Marshal(j.toM())
}

func (j Journey) MarshalBSONValue() (bsontype.Type, []byte, error) {
return bson.MarshalValue(j.toM())
}
53 changes: 43 additions & 10 deletions line.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package fptf

import "encoding/json"
import (
"encoding/json"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsontype"
)

type Line struct {
Id string
Expand All @@ -15,19 +19,19 @@ type Line struct {
}

type mLine struct {
typed
Id string `json:"id"`
Name string `json:"name"`
Mode Mode `json:"mode"`
SubMode string `json:"subMode,omitempty"`
Routes []*Route `json:"routes,omitempty"`
Operator *Operator `json:"operator"`
Meta interface{} `json:"meta,omitempty"`
Typed `bson:"inline"`
Id string `json:"id,omitempty" bson:"id,omitempty"`
Name string `json:"name,omitempty" bson:"name,omitempty"`
Mode Mode `json:"mode,omitempty" bson:"mode,omitempty"`
SubMode string `json:"subMode,omitempty" bson:"subMode,omitempty"`
Routes []*Route `json:"routes,omitempty" bson:"routes,omitempty"`
Operator *Operator `json:"operator,omitempty" bson:"operator,omitempty"`
Meta interface{} `json:"meta,omitempty" bson:"meta,omitempty"`
}

func (w *Line) toM() *mLine {
return &mLine{
typed: typedLine,
Typed: typedLine,
Id: w.Id,
Name: w.Name,
Mode: w.Mode,
Expand Down Expand Up @@ -73,3 +77,32 @@ func (w *Line) MarshalJSON() ([]byte, error) {
}
return json.Marshal(w.toM())
}

func (w *Line) UnmarshalBSONValue(typ bsontype.Type, data []byte) error {
if typ == bson.TypeEmbeddedDocument {
var m mLine
err := bson.UnmarshalValue(bson.TypeEmbeddedDocument, data, &m)
if err != nil {
return err
}
w.Partial = false
w.fromM(&m)
return nil
}

var id string
err := bson.UnmarshalValue(bson.TypeString, data, &id)
if err != nil {
return err
}
w.Id = id
w.Partial = true
return nil
}

func (w Line) MarshalBSONValue() (bsontype.Type, []byte, error) {
if w.Partial {
return bson.MarshalValue(w.Id)
}
return bson.MarshalValue(w.toM())
}
36 changes: 27 additions & 9 deletions location.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package fptf

import "encoding/json"
import (
"encoding/json"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsontype"
)

type Location struct {
Name string
Expand All @@ -12,18 +16,18 @@ type Location struct {
}

type mLocation struct {
typed
Name string `json:"name,omitempty"`
Address string `json:"address,omitempty"`
Longitude float64 `json:"longitude,omitempty"`
Latitude float64 `json:"latitude,omitempty"`
Altitude float64 `json:"altitude,omitempty"`
Meta interface{} `json:"meta,omitempty"`
Typed `bson:"inline"`
Name string `json:"name,omitempty" bson:"name,omitempty"`
Address string `json:"address,omitempty" bson:"address,omitempty"`
Longitude float64 `json:"longitude,omitempty" bson:"longitude,omitempty"`
Latitude float64 `json:"latitude,omitempty" bson:"latitude,omitempty"`
Altitude float64 `json:"altitude,omitempty" bson:"altitude,omitempty"`
Meta interface{} `json:"meta,omitempty" bson:"meta,omitempty"`
}

func (w *Location) toM() *mLocation {
return &mLocation{
typed: typedLocation,
Typed: typedLocation,
Name: w.Name,
Address: w.Address,
Longitude: w.Longitude,
Expand Down Expand Up @@ -55,3 +59,17 @@ func (w *Location) UnmarshalJSON(data []byte) error {
func (w *Location) MarshalJSON() ([]byte, error) {
return json.Marshal(w.toM())
}

func (w *Location) UnmarshalBSONValue(typ bsontype.Type, data []byte) error {
var m mLocation
err := bson.UnmarshalValue(typ, data, &m)
if err != nil {
return err
}
w.fromM(&m)
return nil
}

func (w Location) MarshalBSONValue() (bsontype.Type, []byte, error) {
return bson.MarshalValue(w.toM())
}
24 changes: 12 additions & 12 deletions object-type.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ const (
objectTypeJourney objectType = "journey"
)

type typed struct {
Type objectType `json:"type"`
type Typed struct {
Type objectType `json:"type,omitempty" bson:"type,omitempty"`
}

var (
typedLocation = typed{Type: objectTypeLocation}
typedStation = typed{Type: objectTypeStation}
typedStop = typed{Type: objectTypeStop}
typedRegion = typed{Type: objectTypeRegion}
typedLine = typed{Type: objectTypeLine}
typedRoute = typed{Type: objectTypeRoute}
typedSchedule = typed{Type: objectTypeSchedule}
typedOperator = typed{Type: objectTypeOperator}
typedStopover = typed{Type: objectTypeStopover}
typedJourney = typed{Type: objectTypeJourney}
typedLocation = Typed{Type: objectTypeLocation}
typedStation = Typed{Type: objectTypeStation}
typedStop = Typed{Type: objectTypeStop}
typedRegion = Typed{Type: objectTypeRegion}
typedLine = Typed{Type: objectTypeLine}
typedRoute = Typed{Type: objectTypeRoute}
typedSchedule = Typed{Type: objectTypeSchedule}
typedOperator = Typed{Type: objectTypeOperator}
typedStopover = Typed{Type: objectTypeStopover}
typedJourney = Typed{Type: objectTypeJourney}
)
Loading

0 comments on commit 6c03aac

Please sign in to comment.