From 2a2a20dcb8156b2a8925f5e22b9d8ded1a651940 Mon Sep 17 00:00:00 2001 From: Cameron Moore Date: Thu, 19 Mar 2015 11:32:10 -0500 Subject: [PATCH 1/3] Add tests for ExtractParameter --- helpers/helpers_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 helpers/helpers_test.go diff --git a/helpers/helpers_test.go b/helpers/helpers_test.go new file mode 100644 index 00000000..af25530e --- /dev/null +++ b/helpers/helpers_test.go @@ -0,0 +1,28 @@ +package helpers + +import "testing" + +var extractParameterTests = []struct { + s string + params map[string]interface{} + value string + ok bool +}{ + {"a", map[string]interface{}{"a": "z"}, "z", true}, + {"a.b", map[string]interface{}{"a": map[string]interface{}{"b": "z"}}, "z", true}, + {"a.b.c", map[string]interface{}{"a": map[string]interface{}{"b": map[string]interface{}{"c": "z"}}}, "z", true}, + {"a.1.b", map[string]interface{}{"a": []interface{}{map[string]interface{}{"b": "y"}, map[string]interface{}{"b": "z"}}}, "z", true}, + // failures + {"a.X", map[string]interface{}{"a": map[string]interface{}{"b": "z"}}, "", false}, + {"a.500.b", map[string]interface{}{"a": map[string]interface{}{"b": "z"}}, "", false}, + {"a.501.b", map[string]interface{}{"a": []interface{}{map[string]interface{}{"b": "y"}, map[string]interface{}{"b": "z"}}}, "", false}, +} + +func TestExtractParameter(t *testing.T) { + for _, tt := range extractParameterTests { + s, ok := ExtractParameter(tt.s, tt.params) + if ok != tt.ok || s != tt.value { + t.Errorf("failed to extract parameter %q:\nexpected {value:%#v, ok:%#v},\ngot {value:%#v, ok:%#v}", tt.s, tt.value, tt.ok, s, ok) + } + } +} From 7635cfde33e11b469c05cfeb2baf62e604e75223 Mon Sep 17 00:00:00 2001 From: Cameron Moore Date: Thu, 19 Mar 2015 11:55:24 -0500 Subject: [PATCH 2/3] Add another slice test to ExtractParameter tests --- helpers/helpers_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/helpers/helpers_test.go b/helpers/helpers_test.go index af25530e..61a54bb0 100644 --- a/helpers/helpers_test.go +++ b/helpers/helpers_test.go @@ -12,6 +12,7 @@ var extractParameterTests = []struct { {"a.b", map[string]interface{}{"a": map[string]interface{}{"b": "z"}}, "z", true}, {"a.b.c", map[string]interface{}{"a": map[string]interface{}{"b": map[string]interface{}{"c": "z"}}}, "z", true}, {"a.1.b", map[string]interface{}{"a": []interface{}{map[string]interface{}{"b": "y"}, map[string]interface{}{"b": "z"}}}, "z", true}, + {"a.1.b.c", map[string]interface{}{"a": []interface{}{map[string]interface{}{"b": map[string]interface{}{"c": "y"}}, map[string]interface{}{"b": map[string]interface{}{"c": "z"}}}}, "z", true}, // failures {"a.X", map[string]interface{}{"a": map[string]interface{}{"b": "z"}}, "", false}, {"a.500.b", map[string]interface{}{"a": map[string]interface{}{"b": "z"}}, "", false}, From 8a627f7e67787c8ba9a61ffeb86678d9d8cb5ad1 Mon Sep 17 00:00:00 2001 From: Cameron Moore Date: Thu, 19 Mar 2015 12:15:37 -0500 Subject: [PATCH 3/3] Fix slice traversal in ExtractParameter With these changes, I'm able to pass tests "a.1.b" and "a.1.b.c". --- helpers/helpers.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/helpers/helpers.go b/helpers/helpers.go index c5bbed75..9e5d88cf 100644 --- a/helpers/helpers.go +++ b/helpers/helpers.go @@ -45,8 +45,8 @@ func ExtractParameter(s string, params interface{}) (string, bool) { if paramsValue := reflect.ValueOf(params); paramsValue.Kind() == reflect.Slice { if paramsValueSliceLength := paramsValue.Len(); paramsValueSliceLength > 0 { - if p := strings.SplitN(s, ".", 3); len(p) > 3 { - index, err := strconv.ParseInt(p[1], 10, 64) + if p := strings.SplitN(s, ".", 2); len(p) > 1 { + index, err := strconv.ParseInt(p[0], 10, 64) if err != nil { return "", false @@ -54,7 +54,7 @@ func ExtractParameter(s string, params interface{}) (string, bool) { return "", false } - return ExtractParameter(p[2], params.([]map[string]interface{})[index]) + return ExtractParameter(p[1], params.([]interface{})[index]) } }