Skip to content
Snippets Groups Projects
Commit 78129222 authored by Jan Semmelink's avatar Jan Semmelink
Browse files

Add support for parsing URL values [a,b,c] into array of 3 strings

parent a0455aee
No related branches found
No related tags found
No related merge requests found
...@@ -100,7 +100,11 @@ func (ctx apiContext) extract(name string, t reflect.Type, v reflect.Value) erro ...@@ -100,7 +100,11 @@ func (ctx apiContext) extract(name string, t reflect.Type, v reflect.Value) erro
//get value(s) from query string //get value(s) from query string
var paramStrValues []string var paramStrValues []string
if paramStrValue, isDefined := ctx.request.QueryStringParameters[n]; isDefined { if paramStrValue, isDefined := ctx.request.QueryStringParameters[n]; isDefined {
if len(paramStrValue) >= 2 && paramStrValue[0] == '[' && paramStrValue[len(paramStrValue)-1] == ']' {
paramStrValues = strings.Split(paramStrValue[1:len(paramStrValue)-1], ",") //from [CSV]
} else {
paramStrValues = []string{paramStrValue} //single value paramStrValues = []string{paramStrValue} //single value
}
} else { } else {
paramStrValues = ctx.request.MultiValueQueryStringParameters[n] paramStrValues = ctx.request.MultiValueQueryStringParameters[n]
} }
......
...@@ -83,6 +83,7 @@ type MyGetParams struct { ...@@ -83,6 +83,7 @@ type MyGetParams struct {
Find string `json:"find"` Find string `json:"find"`
Find1 []string `json:"find1"` Find1 []string `json:"find1"`
Find2 []string `json:"find2"` Find2 []string `json:"find2"`
Find3 []string `json:"find3"`
} }
func TestGet(t *testing.T) { func TestGet(t *testing.T) {
...@@ -100,6 +101,7 @@ func TestGet(t *testing.T) { ...@@ -100,6 +101,7 @@ func TestGet(t *testing.T) {
"offset": "3", "offset": "3",
"search": "4", //single value parts into string "search": "4", //single value parts into string
"find1": "sarel", //single value parsed into array "find1": "sarel", //single value parsed into array
"find3": "[hendrik,,frederik,johan]", //array of 4 values in CSV notation
}, },
MultiValueQueryStringParameters: map[string][]string{ MultiValueQueryStringParameters: map[string][]string{
"find2": {"hans", "gert"}, //multi-values parsed into array "find2": {"hans", "gert"}, //multi-values parsed into array
...@@ -117,7 +119,8 @@ func TestGet(t *testing.T) { ...@@ -117,7 +119,8 @@ func TestGet(t *testing.T) {
t.Logf("get: %+v", get) t.Logf("get: %+v", get)
if get.ID != 1 || get.Offset != 3 || get.Limit != 2 || get.Search != "4" || get.Find != "koos" || if get.ID != 1 || get.Offset != 3 || get.Limit != 2 || get.Search != "4" || get.Find != "koos" ||
len(get.Find1) != 1 || get.Find1[0] != "sarel" || len(get.Find1) != 1 || get.Find1[0] != "sarel" ||
len(get.Find2) != 2 || get.Find2[0] != "hans" || get.Find2[1] != "gert" { len(get.Find2) != 2 || get.Find2[0] != "hans" || get.Find2[1] != "gert" ||
len(get.Find3) != 4 || get.Find3[0] != "hendrik" || get.Find3[1] != "" || get.Find3[2] != "frederik" || get.Find3[3] != "johan" {
t.Fatalf("wrong values") t.Fatalf("wrong values")
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment