Skip to content

Commit 4e40b18

Browse files
committed
Adds general options and missing options for table request
1 parent 788b384 commit 4e40b18

11 files changed

+221
-50
lines changed

match.go

+2-12
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ import geo "github.com/paulmach/go.geo"
44

55
// MatchRequest represents a request to the match method
66
type MatchRequest struct {
7+
GeneralOptions
78
Profile string
89
Coordinates Geometry
9-
Bearings []Bearing
1010
Steps Steps
1111
Annotations Annotations
1212
Tidy Tidy
1313
Timestamps []int64
14-
Radiuses []float64
15-
Hints []string
1614
Overview Overview
1715
Gaps Gaps
1816
Geometries Geometries
@@ -41,15 +39,7 @@ func (r MatchRequest) request() *request {
4139
if len(r.Timestamps) > 0 {
4240
options.addInt64("timestamps", r.Timestamps...)
4341
}
44-
if len(r.Radiuses) > 0 {
45-
options.addFloat("radiuses", r.Radiuses...)
46-
}
47-
if len(r.Hints) > 0 {
48-
options.add("hints", r.Hints...)
49-
}
50-
if len(r.Bearings) > 0 {
51-
options.set("bearings", bearings(r.Bearings))
52-
}
42+
options = r.GeneralOptions.options(options)
5343

5444
return &request{
5545
profile: r.Profile,

match_test.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,19 @@ func TestEmptyMatchRequestOptions(t *testing.T) {
2020
name: "with timestamps and radiuses",
2121
request: MatchRequest{
2222
Timestamps: []int64{0, 1, 2},
23-
Radiuses: []float64{0.123123, 0.12312},
23+
GeneralOptions: GeneralOptions{
24+
Radiuses: []float64{0.123123, 0.12312},
25+
},
2426
},
2527
expectedURI: "geometries=polyline6&radiuses=0.123123;0.12312&timestamps=0;1;2",
2628
},
2729
{
2830
name: "with gaps and tidy",
2931
request: MatchRequest{
32+
GeneralOptions: GeneralOptions{
33+
Radiuses: []float64{0.123123, 0.12312},
34+
},
3035
Timestamps: []int64{0, 1, 2},
31-
Radiuses: []float64{0.123123, 0.12312},
3236
Gaps: GapsSplit,
3337
Tidy: TidyTrue,
3438
},
@@ -37,15 +41,19 @@ func TestEmptyMatchRequestOptions(t *testing.T) {
3741
{
3842
name: "with hints",
3943
request: MatchRequest{
40-
Hints: []string{"a", "b", "c", "d"},
44+
GeneralOptions: GeneralOptions{
45+
Hints: []string{"a", "b", "c", "d"},
46+
},
4147
},
4248
expectedURI: "geometries=polyline6&hints=a;b;c;d",
4349
},
4450
{
4551
name: "with bearings",
4652
request: MatchRequest{
47-
Bearings: []Bearing{
48-
{0, 20}, {10, 20},
53+
GeneralOptions: GeneralOptions{
54+
Bearings: []Bearing{
55+
{0, 20}, {10, 20},
56+
},
4957
},
5058
},
5159
expectedURI: "bearings=0%2C20%3B10%2C20&geometries=polyline6",

nearest.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import geo "github.com/paulmach/go.geo"
44

55
// NearestRequest represents a request to the nearest method
66
type NearestRequest struct {
7+
GeneralOptions
78
Profile string
89
Coordinates Geometry
9-
Bearings []Bearing
1010
Number int
1111
}
1212

@@ -31,9 +31,7 @@ func (r NearestRequest) request() *request {
3131
opts.addInt("number", r.Number)
3232
}
3333

34-
if len(r.Bearings) > 0 {
35-
opts.set("bearings", bearings(r.Bearings))
36-
}
34+
opts = r.GeneralOptions.options(opts)
3735

3836
return &request{
3937
profile: r.Profile,

nearest_test.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import (
99
func TestNearestRequestOverviewOption(t *testing.T) {
1010
req := NearestRequest{
1111
Number: 2,
12-
Bearings: []Bearing{
13-
{60, 380},
12+
GeneralOptions: GeneralOptions{
13+
Bearings: []Bearing{
14+
{60, 380},
15+
},
1416
},
1517
}
1618
assert.Equal(
@@ -19,8 +21,10 @@ func TestNearestRequestOverviewOption(t *testing.T) {
1921
req.request().options.encode())
2022

2123
req = NearestRequest{
22-
Bearings: []Bearing{
23-
{60, 380},
24+
GeneralOptions: GeneralOptions{
25+
Bearings: []Bearing{
26+
{60, 380},
27+
},
2428
},
2529
}
2630
assert.Equal(

options.go

+32
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,38 @@ import (
77
"strconv"
88
)
99

10+
type GeneralOptions struct {
11+
Bearings []Bearing
12+
Radiuses []float64
13+
GenerateHintsDisabled bool
14+
Hints []string
15+
Approaches []string
16+
Exclude []string
17+
}
18+
19+
func (g GeneralOptions) options(opts options) options {
20+
if len(g.Bearings) > 0 {
21+
opts.add("bearings", bearings(g.Bearings))
22+
}
23+
if len(g.Radiuses) > 0 {
24+
opts.addFloat("radiuses", g.Radiuses...)
25+
}
26+
// generate_hints option default is true
27+
if g.GenerateHintsDisabled {
28+
opts.setBool("generate_hints", !g.GenerateHintsDisabled)
29+
}
30+
if len(g.Hints) > 0 {
31+
opts.add("hints", g.Hints...)
32+
}
33+
if len(g.Approaches) > 0 {
34+
opts.add("approaches", g.Approaches...)
35+
}
36+
if len(g.Exclude) > 0 {
37+
opts.add("exclude", g.Exclude...)
38+
}
39+
return opts
40+
}
41+
1042
// options represents OSRM query params to be encoded in URL
1143
type options map[string][]string
1244

options_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,54 @@ func TestOptionsAddFloatValsAsVariadic(t *testing.T) {
101101
opts.addFloat("foo", 1.1231312, 2.1233)
102102
assert.Equal(t, "foo=1.1231312;2.1233", opts.encode())
103103
}
104+
105+
func TestGeneralOptions(t *testing.T) {
106+
cases := []struct {
107+
name string
108+
options GeneralOptions
109+
expectedURI string
110+
}{
111+
{
112+
name: "empty",
113+
options: GeneralOptions{},
114+
expectedURI: "",
115+
},
116+
{
117+
name: "with bearings",
118+
options: GeneralOptions{
119+
Bearings: []Bearing{
120+
{0, 20}, {10, 20},
121+
},
122+
},
123+
expectedURI: "bearings=0%2C20%3B10%2C20",
124+
},
125+
{
126+
name: "with radiuses",
127+
options: GeneralOptions{
128+
Radiuses: []float64{0.123123, 0.12312},
129+
},
130+
expectedURI: "radiuses=0.123123;0.12312",
131+
},
132+
{
133+
name: "generate hints disabled",
134+
options: GeneralOptions{
135+
Radiuses: []float64{0.123123, 0.12312},
136+
GenerateHintsDisabled: true,
137+
},
138+
expectedURI: "generate_hints=false&radiuses=0.123123;0.12312",
139+
},
140+
{
141+
name: "with approaches and exclude",
142+
options: GeneralOptions{
143+
Exclude: []string{"toll", "highway"},
144+
Approaches: []string{"a", "b"},
145+
},
146+
expectedURI: "approaches=a;b&exclude=toll;highway",
147+
},
148+
}
149+
for _, c := range cases {
150+
t.Run(c.name, func(t *testing.T) {
151+
assert.Equal(t, c.expectedURI, c.options.options(options{}).encode())
152+
})
153+
}
154+
}

route.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88

99
// RouteRequest represents a request to the route method
1010
type RouteRequest struct {
11+
GeneralOptions
1112
Profile string
1213
Coordinates Geometry
13-
Bearings []Bearing
1414
Steps Steps
1515
Annotations Annotations
1616
Overview Overview
@@ -69,9 +69,7 @@ func (r RouteRequest) request() *request {
6969
opts := stepsOptions(r.Steps, r.Annotations, r.Overview, r.Geometries).
7070
setStringer("continue_straight", r.ContinueStraight)
7171

72-
if len(r.Bearings) > 0 {
73-
opts.set("bearings", bearings(r.Bearings))
74-
}
72+
opts = r.GeneralOptions.options(opts)
7573

7674
return &request{
7775
profile: r.Profile,

route_test.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ func TestEmptyRouteRequestOptions(t *testing.T) {
1616

1717
func TestRouteRequestOptionsWithBearings(t *testing.T) {
1818
req := RouteRequest{
19-
Bearings: []Bearing{
20-
{60, 380},
21-
{45, 180},
19+
GeneralOptions: GeneralOptions{
20+
Bearings: []Bearing{
21+
{60, 380},
22+
{45, 180},
23+
},
2224
},
2325
ContinueStraight: ContinueStraightTrue,
2426
}

table.go

+19
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ package osrm
22

33
// TableRequest represents a request to the table method
44
type TableRequest struct {
5+
GeneralOptions
56
Profile string
67
Coordinates Geometry
78
Sources, Destinations []int
9+
Annotations Annotations
10+
FallbackSpeed float64
11+
FallbackCoordinate FallbackCoordinate
12+
ScaleFactor float64
813
}
914

1015
// TableResponse resresents a response from the table method
@@ -21,6 +26,20 @@ func (r TableRequest) request() *request {
2126
if len(r.Destinations) > 0 {
2227
opts.addInt("destinations", r.Destinations...)
2328
}
29+
if len(r.Annotations) > 0 {
30+
opts.setStringer("annotations", r.Annotations)
31+
}
32+
if r.FallbackSpeed > 0 {
33+
opts.addFloat("fallback_speed", r.FallbackSpeed)
34+
}
35+
if r.FallbackCoordinate.Valid() {
36+
opts.setStringer("fallback_coordinate", r.FallbackCoordinate)
37+
}
38+
if r.ScaleFactor > 0 {
39+
opts.addFloat("scale_factor", r.ScaleFactor)
40+
}
41+
42+
opts = r.GeneralOptions.options(opts)
2443

2544
return &request{
2645
profile: r.Profile,

table_test.go

+60-10
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,65 @@ import (
66
"github.com/stretchr/testify/assert"
77
)
88

9-
func TestEmptyTableRequestOptions(t *testing.T) {
10-
req := TableRequest{}
11-
assert.Empty(t, req.request().options.encode())
12-
}
13-
14-
func TestNotEmptyTableRequestOptions(t *testing.T) {
15-
req := TableRequest{
16-
Sources: []int{0, 1, 2},
17-
Destinations: []int{1, 3},
9+
func TestTableRequestOptions(t *testing.T) {
10+
cases := []struct {
11+
name string
12+
request TableRequest
13+
expectedURI string
14+
}{
15+
{
16+
name: "empty",
17+
request: TableRequest{},
18+
expectedURI: "",
19+
},
20+
{
21+
name: "with sources and destinations",
22+
request: TableRequest{
23+
Sources: []int{0, 1, 2},
24+
Destinations: []int{1, 3},
25+
},
26+
expectedURI: "destinations=1;3&sources=0;1;2",
27+
},
28+
{
29+
name: "scale_factor",
30+
request: TableRequest{
31+
ScaleFactor: 0.8,
32+
GeneralOptions: GeneralOptions{
33+
Exclude: []string{"toll"},
34+
},
35+
},
36+
expectedURI: "exclude=toll&scale_factor=0.8",
37+
},
38+
{
39+
name: "fallback_coordinate",
40+
request: TableRequest{
41+
FallbackSpeed: 11.5,
42+
GeneralOptions: GeneralOptions{
43+
Hints: []string{"a", "b"},
44+
},
45+
},
46+
expectedURI: "fallback_speed=11.5&hints=a;b",
47+
},
48+
{
49+
name: "fallback_coordinate",
50+
request: TableRequest{
51+
FallbackSpeed: 11.5,
52+
FallbackCoordinate: FallbackCoordinateInput,
53+
},
54+
expectedURI: "fallback_coordinate=input&fallback_speed=11.5",
55+
},
56+
{
57+
name: "annotations",
58+
request: TableRequest{
59+
Annotations: AnnotationsDurationDistance,
60+
FallbackSpeed: 11.5,
61+
},
62+
expectedURI: "annotations=duration%2Cdistance&fallback_speed=11.5",
63+
},
64+
}
65+
for _, c := range cases {
66+
t.Run(c.name, func(t *testing.T) {
67+
assert.Equal(t, c.expectedURI, c.request.request().options.encode())
68+
})
1869
}
19-
assert.Equal(t, "destinations=1;3&sources=0;1;2", req.request().options.encode())
2070
}

0 commit comments

Comments
 (0)