Skip to content

Commit

Permalink
Adds general options and missing options for table request
Browse files Browse the repository at this point in the history
  • Loading branch information
mkasner committed Sep 20, 2019
1 parent 788b384 commit 7fc54cc
Show file tree
Hide file tree
Showing 11 changed files with 242 additions and 57 deletions.
14 changes: 2 additions & 12 deletions match.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ import geo "github.com/paulmach/go.geo"

// MatchRequest represents a request to the match method
type MatchRequest struct {
GeneralOptions
Profile string
Coordinates Geometry
Bearings []Bearing
Steps Steps
Annotations Annotations
Tidy Tidy
Timestamps []int64
Radiuses []float64
Hints []string
Overview Overview
Gaps Gaps
Geometries Geometries
Expand Down Expand Up @@ -41,15 +39,7 @@ func (r MatchRequest) request() *request {
if len(r.Timestamps) > 0 {
options.addInt64("timestamps", r.Timestamps...)
}
if len(r.Radiuses) > 0 {
options.addFloat("radiuses", r.Radiuses...)
}
if len(r.Hints) > 0 {
options.add("hints", r.Hints...)
}
if len(r.Bearings) > 0 {
options.set("bearings", bearings(r.Bearings))
}
options = r.GeneralOptions.options(options)

return &request{
profile: r.Profile,
Expand Down
18 changes: 13 additions & 5 deletions match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ func TestEmptyMatchRequestOptions(t *testing.T) {
name: "with timestamps and radiuses",
request: MatchRequest{
Timestamps: []int64{0, 1, 2},
Radiuses: []float64{0.123123, 0.12312},
GeneralOptions: GeneralOptions{
Radiuses: []float64{0.123123, 0.12312},
},
},
expectedURI: "geometries=polyline6&radiuses=0.123123;0.12312&timestamps=0;1;2",
},
{
name: "with gaps and tidy",
request: MatchRequest{
GeneralOptions: GeneralOptions{
Radiuses: []float64{0.123123, 0.12312},
},
Timestamps: []int64{0, 1, 2},
Radiuses: []float64{0.123123, 0.12312},
Gaps: GapsSplit,
Tidy: TidyTrue,
},
Expand All @@ -37,15 +41,19 @@ func TestEmptyMatchRequestOptions(t *testing.T) {
{
name: "with hints",
request: MatchRequest{
Hints: []string{"a", "b", "c", "d"},
GeneralOptions: GeneralOptions{
Hints: []string{"a", "b", "c", "d"},
},
},
expectedURI: "geometries=polyline6&hints=a;b;c;d",
},
{
name: "with bearings",
request: MatchRequest{
Bearings: []Bearing{
{0, 20}, {10, 20},
GeneralOptions: GeneralOptions{
Bearings: []Bearing{
{0, 20}, {10, 20},
},
},
},
expectedURI: "bearings=0%2C20%3B10%2C20&geometries=polyline6",
Expand Down
20 changes: 11 additions & 9 deletions nearest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import geo "github.com/paulmach/go.geo"

// NearestRequest represents a request to the nearest method
type NearestRequest struct {
GeneralOptions
Profile string
Coordinates Geometry
Bearings []Bearing
Number int
}

Expand All @@ -18,11 +18,8 @@ type NearestResponse struct {

// NearestWaypoint represents a nearest point on a nearest query
type NearestWaypoint struct {
Location geo.Point `json:"location"`
Distance float64 `json:"distance"`
Name string `json:"name"`
Hint string `json:"hint"`
Nodes []uint64 `json:"nodes"`
Waypoint
Nodes []uint64 `json:"nodes"`
}

func (r NearestRequest) request() *request {
Expand All @@ -31,9 +28,7 @@ func (r NearestRequest) request() *request {
opts.addInt("number", r.Number)
}

if len(r.Bearings) > 0 {
opts.set("bearings", bearings(r.Bearings))
}
opts = r.GeneralOptions.options(opts)

return &request{
profile: r.Profile,
Expand All @@ -42,3 +37,10 @@ func (r NearestRequest) request() *request {
options: opts,
}
}

type Waypoint struct {
Location geo.Point `json:"location"`
Distance float64 `json:"distance"`
Name string `json:"name"`
Hint string `json:"hint"`
}
12 changes: 8 additions & 4 deletions nearest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
func TestNearestRequestOverviewOption(t *testing.T) {
req := NearestRequest{
Number: 2,
Bearings: []Bearing{
{60, 380},
GeneralOptions: GeneralOptions{
Bearings: []Bearing{
{60, 380},
},
},
}
assert.Equal(
Expand All @@ -19,8 +21,10 @@ func TestNearestRequestOverviewOption(t *testing.T) {
req.request().options.encode())

req = NearestRequest{
Bearings: []Bearing{
{60, 380},
GeneralOptions: GeneralOptions{
Bearings: []Bearing{
{60, 380},
},
},
}
assert.Equal(
Expand Down
32 changes: 32 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,38 @@ import (
"strconv"
)

type GeneralOptions struct {
Bearings []Bearing
Radiuses []float64
GenerateHintsDisabled bool
Hints []string
Approaches []string
Exclude []string
}

func (g GeneralOptions) options(opts options) options {
if len(g.Bearings) > 0 {
opts.add("bearings", bearings(g.Bearings))
}
if len(g.Radiuses) > 0 {
opts.addFloat("radiuses", g.Radiuses...)
}
// generate_hints option default is true
if g.GenerateHintsDisabled {
opts.setBool("generate_hints", !g.GenerateHintsDisabled)
}
if len(g.Hints) > 0 {
opts.add("hints", g.Hints...)
}
if len(g.Approaches) > 0 {
opts.add("approaches", g.Approaches...)
}
if len(g.Exclude) > 0 {
opts.add("exclude", g.Exclude...)
}
return opts
}

// options represents OSRM query params to be encoded in URL
type options map[string][]string

Expand Down
51 changes: 51 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,54 @@ func TestOptionsAddFloatValsAsVariadic(t *testing.T) {
opts.addFloat("foo", 1.1231312, 2.1233)
assert.Equal(t, "foo=1.1231312;2.1233", opts.encode())
}

func TestGeneralOptions(t *testing.T) {
cases := []struct {
name string
options GeneralOptions
expectedURI string
}{
{
name: "empty",
options: GeneralOptions{},
expectedURI: "",
},
{
name: "with bearings",
options: GeneralOptions{
Bearings: []Bearing{
{0, 20}, {10, 20},
},
},
expectedURI: "bearings=0%2C20%3B10%2C20",
},
{
name: "with radiuses",
options: GeneralOptions{
Radiuses: []float64{0.123123, 0.12312},
},
expectedURI: "radiuses=0.123123;0.12312",
},
{
name: "generate hints disabled",
options: GeneralOptions{
Radiuses: []float64{0.123123, 0.12312},
GenerateHintsDisabled: true,
},
expectedURI: "generate_hints=false&radiuses=0.123123;0.12312",
},
{
name: "with approaches and exclude",
options: GeneralOptions{
Exclude: []string{"toll", "highway"},
Approaches: []string{"a", "b"},
},
expectedURI: "approaches=a;b&exclude=toll;highway",
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
assert.Equal(t, c.expectedURI, c.options.options(options{}).encode())
})
}
}
9 changes: 4 additions & 5 deletions route.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (

// RouteRequest represents a request to the route method
type RouteRequest struct {
GeneralOptions
Profile string
Coordinates Geometry
Bearings []Bearing
Steps Steps
Annotations Annotations
Overview Overview
Expand All @@ -21,7 +21,8 @@ type RouteRequest struct {
// RouteResponse represents a response from the route method
type RouteResponse struct {
ResponseStatus
Routes []Route `json:"routes"`
Routes []Route `json:"routes"`
Waypoints []Waypoint `json:"waypoints"`
}

// Route represents a route through (potentially multiple) points.
Expand Down Expand Up @@ -69,9 +70,7 @@ func (r RouteRequest) request() *request {
opts := stepsOptions(r.Steps, r.Annotations, r.Overview, r.Geometries).
setStringer("continue_straight", r.ContinueStraight)

if len(r.Bearings) > 0 {
opts.set("bearings", bearings(r.Bearings))
}
opts = r.GeneralOptions.options(opts)

return &request{
profile: r.Profile,
Expand Down
8 changes: 5 additions & 3 deletions route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ func TestEmptyRouteRequestOptions(t *testing.T) {

func TestRouteRequestOptionsWithBearings(t *testing.T) {
req := RouteRequest{
Bearings: []Bearing{
{60, 380},
{45, 180},
GeneralOptions: GeneralOptions{
Bearings: []Bearing{
{60, 380},
{45, 180},
},
},
ContinueStraight: ContinueStraightTrue,
}
Expand Down
25 changes: 24 additions & 1 deletion table.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@ package osrm

// TableRequest represents a request to the table method
type TableRequest struct {
GeneralOptions
Profile string
Coordinates Geometry
Sources, Destinations []int
Annotations Annotations
FallbackSpeed float64
FallbackCoordinate FallbackCoordinate
ScaleFactor float64
}

// TableResponse resresents a response from the table method
type TableResponse struct {
ResponseStatus
Durations [][]float32 `json:"durations"`
Durations [][]float32 `json:"durations"`
Distances [][]float32 `json:"distances"`
Sources []Waypoint `json:"sources"`
Destinations []Waypoint `json:"destinations"`
FallbackSpeedCells [][]int `json:"fallback_speed_cells"`
}

func (r TableRequest) request() *request {
Expand All @@ -21,6 +30,20 @@ func (r TableRequest) request() *request {
if len(r.Destinations) > 0 {
opts.addInt("destinations", r.Destinations...)
}
if len(r.Annotations) > 0 {
opts.setStringer("annotations", r.Annotations)
}
if r.FallbackSpeed > 0 {
opts.addFloat("fallback_speed", r.FallbackSpeed)
}
if r.FallbackCoordinate.Valid() {
opts.setStringer("fallback_coordinate", r.FallbackCoordinate)
}
if r.ScaleFactor > 0 {
opts.addFloat("scale_factor", r.ScaleFactor)
}

opts = r.GeneralOptions.options(opts)

return &request{
profile: r.Profile,
Expand Down
Loading

0 comments on commit 7fc54cc

Please sign in to comment.