forked from Project-OSRM/osrm-backend
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: move route options func and test into route/options package * refactor: implement PraseSteps in route/options package * feat: implements more route request parameters * refactor: move own ParseQuery to api package * feat: implement generic options
- Loading branch information
1 parent
3cfecde
commit a5c7887
Showing
17 changed files
with
1,264 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package coordinate | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/Telenav/osrm-backend/integration/pkg/api" | ||
"github.com/golang/glog" | ||
) | ||
|
||
// Index indicates which NO. of Coordinates. | ||
type Index uint | ||
|
||
// Indexes represents a list of Index. | ||
type Indexes []Index | ||
|
||
func (i *Indexes) String() string { | ||
var s string | ||
for _, v := range *i { | ||
if len(s) > 0 { | ||
s += api.Semicolon | ||
} | ||
s += strconv.FormatUint(uint64(v), 10) | ||
} | ||
return s | ||
} | ||
|
||
// PraseIndexes parses string to indexes of coordinates. | ||
func PraseIndexes(s string) (Indexes, error) { | ||
indexes := []Index{} | ||
|
||
splits := strings.Split(s, api.Semicolon) | ||
for _, split := range splits { | ||
if len(split) == 0 { | ||
continue | ||
} | ||
n, err := strconv.ParseUint(split, 10, 32) | ||
if err != nil { | ||
fullErr := fmt.Errorf("invalid indexes value: %s, err %v", s, err) | ||
glog.Warning(fullErr) | ||
return nil, fullErr | ||
} | ||
indexes = append(indexes, Index(n)) | ||
} | ||
return indexes, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package coordinate | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestPraseIndexes(t *testing.T) { | ||
|
||
cases := []struct { | ||
s string | ||
Indexes | ||
expectFail bool | ||
}{ | ||
{"0;5;7", Indexes{0, 5, 7}, false}, | ||
{"0;5;7;", Indexes{0, 5, 7}, false}, | ||
{"0", Indexes{0}, false}, | ||
{"5;1;4;2;3;6", Indexes{5, 1, 4, 2, 3, 6}, false}, | ||
{"", Indexes{}, false}, | ||
{"-1", nil, true}, | ||
{"a", nil, true}, | ||
} | ||
|
||
for _, c := range cases { | ||
indexes, err := PraseIndexes(c.s) | ||
if err != nil && c.expectFail { | ||
continue //right | ||
} else if (err != nil && !c.expectFail) || (err == nil && c.expectFail) { | ||
t.Errorf("parse %s expect fail %t, but got err %v", c.s, c.expectFail, err) | ||
continue | ||
} | ||
|
||
if !reflect.DeepEqual(indexes, c.Indexes) { | ||
t.Errorf("parse %s, expect %v, but got %v", c.s, c.Indexes, indexes) | ||
} | ||
} | ||
} | ||
|
||
func TestIndexesString(t *testing.T) { | ||
cases := []struct { | ||
expect string | ||
Indexes | ||
}{ | ||
{"0;5;7", Indexes{0, 5, 7}}, | ||
{"0", Indexes{0}}, | ||
{"5;1;4;2;3;6", Indexes{5, 1, 4, 2, 3, 6}}, | ||
{"", Indexes{}}, | ||
} | ||
|
||
for _, c := range cases { | ||
s := c.Indexes.String() | ||
if s != c.expect { | ||
t.Errorf("%v String(), expect %s, but got %s", c.Indexes, c.expect, s) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package genericoptions | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/Telenav/osrm-backend/integration/pkg/api" | ||
) | ||
|
||
// Elements represents values split by `;`, i.e. bearings, radiuses, hints, approaches | ||
type Elements []string | ||
|
||
// ParseElemenets parses OSRM option elements. | ||
func ParseElemenets(s string) (Elements, error) { | ||
|
||
s = strings.TrimSuffix(s, api.Semicolon) // remove the last `;` if exist | ||
if len(s) == 0 { | ||
return Elements{}, nil | ||
} | ||
|
||
elements := Elements{} | ||
splits := strings.Split(s, api.Semicolon) | ||
for _, split := range splits { | ||
elements = append(elements, split) | ||
} | ||
return elements, nil | ||
} | ||
|
||
func (e *Elements) String() string { | ||
var s string | ||
for _, element := range *e { | ||
if len(s) > 0 { | ||
s += api.Semicolon | ||
} | ||
s += element | ||
} | ||
return s | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package genericoptions | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestPraseElements(t *testing.T) { | ||
|
||
cases := []struct { | ||
s string | ||
Elements | ||
expectFail bool | ||
}{ | ||
{"0;5;7", Elements{"0", "5", "7"}, false}, | ||
{"0;5;7;", Elements{"0", "5", "7"}, false}, | ||
{"0;;7", Elements{"0", "", "7"}, false}, | ||
{"", Elements{}, false}, | ||
} | ||
|
||
for _, c := range cases { | ||
elements, err := ParseElemenets(c.s) | ||
if err != nil && c.expectFail { | ||
continue //right | ||
} else if (err != nil && !c.expectFail) || (err == nil && c.expectFail) { | ||
t.Errorf("parse %s expect fail %t, but got err %v", c.s, c.expectFail, err) | ||
continue | ||
} | ||
|
||
if !reflect.DeepEqual(elements, c.Elements) { | ||
t.Errorf("parse %s, expect %v, but got %v", c.s, c.Elements, elements) | ||
} | ||
} | ||
} | ||
|
||
func TestElementsString(t *testing.T) { | ||
cases := []struct { | ||
expect string | ||
Elements | ||
}{ | ||
{"0;5;7", Elements{"0", "5", "7"}}, | ||
{"0;;7", Elements{"0", "", "7"}}, | ||
{"", Elements{""}}, | ||
{"", Elements{}}, | ||
} | ||
|
||
for _, c := range cases { | ||
s := c.Elements.String() | ||
if s != c.expect { | ||
t.Errorf("%v String(), expect %s, but got %s", c.Elements, c.expect, s) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package genericoptions | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/Telenav/osrm-backend/integration/pkg/api" | ||
) | ||
|
||
// Classes represents OSRM exclude classes. | ||
// https://github.com/Telenav/osrm-backend/blob/master-telenav/docs/http.md#general-options | ||
type Classes []string | ||
|
||
// ParseClasses parses OSRM option elements. | ||
func ParseClasses(s string) (Classes, error) { | ||
|
||
s = strings.TrimSuffix(s, api.Comma) // remove the last `,` if exist | ||
|
||
classes := Classes{} | ||
splits := strings.Split(s, api.Comma) | ||
for _, split := range splits { | ||
if len(split) == 0 { | ||
continue | ||
} | ||
classes = append(classes, split) | ||
} | ||
return classes, nil | ||
} | ||
|
||
func (c *Classes) String() string { | ||
var s string | ||
for _, class := range *c { | ||
if len(class) == 0 { | ||
continue | ||
} | ||
|
||
if len(s) > 0 { | ||
s += api.Comma | ||
} | ||
s += class | ||
} | ||
return s | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package genericoptions | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestPraseClasses(t *testing.T) { | ||
|
||
cases := []struct { | ||
s string | ||
Classes | ||
expectFail bool | ||
}{ | ||
{"0,5,7", Classes{"0", "5", "7"}, false}, | ||
{"0,5,7,", Classes{"0", "5", "7"}, false}, | ||
{"0,,7", Classes{"0", "7"}, false}, | ||
{"", Classes{}, false}, | ||
} | ||
|
||
for _, c := range cases { | ||
classes, err := ParseClasses(c.s) | ||
if err != nil && c.expectFail { | ||
continue //right | ||
} else if (err != nil && !c.expectFail) || (err == nil && c.expectFail) { | ||
t.Errorf("parse %s expect fail %t, but got err %v", c.s, c.expectFail, err) | ||
continue | ||
} | ||
|
||
if !reflect.DeepEqual(classes, c.Classes) { | ||
t.Errorf("parse %s, expect %v, but got %v", c.s, c.Classes, classes) | ||
} | ||
} | ||
} | ||
|
||
func TestClassesString(t *testing.T) { | ||
cases := []struct { | ||
expect string | ||
Classes | ||
}{ | ||
{"0,5,7", Classes{"0", "5", "7"}}, | ||
{"0,7", Classes{"0", "", "7"}}, | ||
{"", Classes{""}}, | ||
{"", Classes{}}, | ||
} | ||
|
||
for _, c := range cases { | ||
s := c.Classes.String() | ||
if s != c.expect { | ||
t.Errorf("%v String(), expect %s, but got %s", c.Classes, c.expect, s) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package genericoptions | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/golang/glog" | ||
) | ||
|
||
// ParseGenerateHints parses generic generate_hints option. | ||
func ParseGenerateHints(s string) (bool, error) { | ||
b, err := strconv.ParseBool(s) | ||
if err != nil { | ||
glog.Warning(err) | ||
return false, err | ||
} | ||
|
||
return b, nil | ||
} |
31 changes: 31 additions & 0 deletions
31
integration/pkg/api/osrm/genericoptions/generic_hints_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package genericoptions | ||
|
||
import "testing" | ||
|
||
func TestParseGenerateHints(t *testing.T) { | ||
cases := []struct { | ||
s string | ||
expect bool | ||
expectFail bool | ||
}{ | ||
{"true", true, false}, | ||
{"false", false, false}, | ||
{"", false, true}, | ||
{"true1", false, true}, | ||
{"-1", false, true}, | ||
} | ||
|
||
for _, c := range cases { | ||
b, err := ParseGenerateHints(c.s) | ||
if err != nil && c.expectFail { | ||
continue //right | ||
} else if (err != nil && !c.expectFail) || (err == nil && c.expectFail) { | ||
t.Errorf("parse %s expect fail %t, but got err %v", c.s, c.expectFail, err) | ||
continue | ||
} | ||
|
||
if b != c.expect { | ||
t.Errorf("parse %s, expect %t, but got %t", c.s, c.expect, b) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
package genericoptions | ||
|
||
// GenerateHints values | ||
const ( | ||
GenerateHintsDefaultValue = true // default | ||
) |
Oops, something went wrong.