Skip to content

Commit 955d9aa

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

File tree

11 files changed

+237
-57
lines changed

11 files changed

+237
-57
lines changed

match.go

Lines changed: 2 additions & 12 deletions
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

Lines changed: 13 additions & 5 deletions
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

Lines changed: 11 additions & 9 deletions
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

@@ -18,11 +18,8 @@ type NearestResponse struct {
1818

1919
// NearestWaypoint represents a nearest point on a nearest query
2020
type NearestWaypoint struct {
21-
Location geo.Point `json:"location"`
22-
Distance float64 `json:"distance"`
23-
Name string `json:"name"`
24-
Hint string `json:"hint"`
25-
Nodes []uint64 `json:"nodes"`
21+
Waypoint
22+
Nodes []uint64 `json:"nodes"`
2623
}
2724

2825
func (r NearestRequest) request() *request {
@@ -31,9 +28,7 @@ func (r NearestRequest) request() *request {
3128
opts.addInt("number", r.Number)
3229
}
3330

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

3833
return &request{
3934
profile: r.Profile,
@@ -42,3 +37,10 @@ func (r NearestRequest) request() *request {
4237
options: opts,
4338
}
4439
}
40+
41+
type Waypoint struct {
42+
Location geo.Point `json:"location"`
43+
Distance float64 `json:"distance"`
44+
Name string `json:"name"`
45+
Hint string `json:"hint"`
46+
}

nearest_test.go

Lines changed: 8 additions & 4 deletions
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

Lines changed: 32 additions & 0 deletions
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

Lines changed: 51 additions & 0 deletions
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

Lines changed: 4 additions & 5 deletions
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
@@ -21,7 +21,8 @@ type RouteRequest struct {
2121
// RouteResponse represents a response from the route method
2222
type RouteResponse struct {
2323
ResponseStatus
24-
Routes []Route `json:"routes"`
24+
Routes []Route `json:"routes"`
25+
Waypoints []Waypoint `json:"waypoints"`
2526
}
2627

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

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

7675
return &request{
7776
profile: r.Profile,

route_test.go

Lines changed: 5 additions & 3 deletions
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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,24 @@ 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
1116
type TableResponse struct {
1217
ResponseStatus
13-
Durations [][]float32 `json:"durations"`
18+
Durations [][]float32 `json:"durations"`
19+
Distances [][]float32 `json:"distances"`
20+
Sources []Waypoint `json:"sources"`
21+
Destinations []Waypoint `json:"destinations"`
22+
FallbackSpeedCells [][]int `json:"fallback_speed_cells"`
1423
}
1524

1625
func (r TableRequest) request() *request {
@@ -21,6 +30,20 @@ func (r TableRequest) request() *request {
2130
if len(r.Destinations) > 0 {
2231
opts.addInt("destinations", r.Destinations...)
2332
}
33+
if len(r.Annotations) > 0 {
34+
opts.setStringer("annotations", r.Annotations)
35+
}
36+
if r.FallbackSpeed > 0 {
37+
opts.addFloat("fallback_speed", r.FallbackSpeed)
38+
}
39+
if r.FallbackCoordinate.Valid() {
40+
opts.setStringer("fallback_coordinate", r.FallbackCoordinate)
41+
}
42+
if r.ScaleFactor > 0 {
43+
opts.addFloat("scale_factor", r.ScaleFactor)
44+
}
45+
46+
opts = r.GeneralOptions.options(opts)
2447

2548
return &request{
2649
profile: r.Profile,

0 commit comments

Comments
 (0)