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 17, 2019
1 parent 788b384 commit 969b7c7
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 42 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
6 changes: 2 additions & 4 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 @@ -31,9 +31,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 Down
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())
})
}
}
6 changes: 2 additions & 4 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 Down Expand Up @@ -69,9 +69,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
16 changes: 16 additions & 0 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ 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
Expand All @@ -21,6 +26,17 @@ func (r TableRequest) request() *request {
if len(r.Destinations) > 0 {
opts.addInt("destinations", r.Destinations...)
}
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
62 changes: 52 additions & 10 deletions table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,57 @@ import (
"github.com/stretchr/testify/assert"
)

func TestEmptyTableRequestOptions(t *testing.T) {
req := TableRequest{}
assert.Empty(t, req.request().options.encode())
}

func TestNotEmptyTableRequestOptions(t *testing.T) {
req := TableRequest{
Sources: []int{0, 1, 2},
Destinations: []int{1, 3},
func TestTableRequestOptions(t *testing.T) {
cases := []struct {
name string
request TableRequest
expectedURI string
}{
{
name: "empty",
request: TableRequest{},
expectedURI: "",
},
{
name: "with sources and destinations",
request: TableRequest{
Sources: []int{0, 1, 2},
Destinations: []int{1, 3},
},
expectedURI: "destinations=1;3&sources=0;1;2",
},
{
name: "scale_factor",
request: TableRequest{
ScaleFactor: 0.8,
GeneralOptions: GeneralOptions{
Exclude: []string{"toll"},
},
},
expectedURI: "exclude=toll&scale_factor=0.8",
},
{
name: "fallback_coordinate",
request: TableRequest{
FallbackSpeed: 11.5,
GeneralOptions: GeneralOptions{
Hints: []string{"a", "b"},
},
},
expectedURI: "fallback_speed=11.5&hints=a;b",
},
{
name: "fallback_coordinate",
request: TableRequest{
FallbackSpeed: 11.5,
FallbackCoordinate: FallbackCoordinateInput,
},
expectedURI: "fallback_coordinate=input&fallback_speed=11.5",
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
assert.Equal(t, c.expectedURI, c.request.request().options.encode())
})
}
assert.Equal(t, "destinations=1;3&sources=0;1;2", req.request().options.encode())
}
18 changes: 18 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,21 @@ func bearings(br []Bearing) string {
}
return strings.Join(s, ";")
}

type FallbackCoordinate string

const (
FallbackCoordinateInput FallbackCoordinate = "input"
FallbackCoordinateSnapped = "snapped"
)

func (f FallbackCoordinate) String() string {
return string(f)
}

func (f FallbackCoordinate) Valid() bool {
if f == FallbackCoordinateInput || f == FallbackCoordinateSnapped {
return true
}
return false
}

0 comments on commit 969b7c7

Please sign in to comment.