Skip to content

Commit

Permalink
support all route options (#119)
Browse files Browse the repository at this point in the history
* 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
wangyoucao577 authored and CodeBear801 committed Jan 3, 2020
1 parent 3cfecde commit a5c7887
Show file tree
Hide file tree
Showing 17 changed files with 1,264 additions and 203 deletions.
4 changes: 4 additions & 0 deletions integration/pkg/api/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ package api
// Semicolon defines name for ";".
// QuestionMark defines name for "?".
// Slash defines name for "/".
// Ampersand defines name for "&".
// EqualTo defines name for "=".
const (
Comma = ","
Semicolon = ";"
QuestionMark = "?"
Slash = "/"
Ampersand = "&"
EqualTo = "="
)
47 changes: 47 additions & 0 deletions integration/pkg/api/osrm/coordinate/index.go
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
}
56 changes: 56 additions & 0 deletions integration/pkg/api/osrm/coordinate/index_test.go
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)
}
}
}
37 changes: 37 additions & 0 deletions integration/pkg/api/osrm/genericoptions/elements.go
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
}
53 changes: 53 additions & 0 deletions integration/pkg/api/osrm/genericoptions/elements_test.go
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)
}
}
}
42 changes: 42 additions & 0 deletions integration/pkg/api/osrm/genericoptions/exclude.go
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
}
53 changes: 53 additions & 0 deletions integration/pkg/api/osrm/genericoptions/exclude_test.go
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)
}
}
}
18 changes: 18 additions & 0 deletions integration/pkg/api/osrm/genericoptions/generic_hints.go
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 integration/pkg/api/osrm/genericoptions/generic_hints_test.go
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)
}
}
}
5 changes: 5 additions & 0 deletions integration/pkg/api/osrm/genericoptions/values.go
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
package genericoptions

// GenerateHints values
const (
GenerateHintsDefaultValue = true // default
)
Loading

0 comments on commit a5c7887

Please sign in to comment.