diff --git a/dialog/params.go b/dialog/params.go index c046d96..133ad4e 100644 --- a/dialog/params.go +++ b/dialog/params.go @@ -39,7 +39,7 @@ func insertParams(command string, params map[string]string) string { } // SearchForParams returns variables from a command -func SearchForParams(lines []string) map[string]string { +func SearchForParams(lines []string) [][2]string { if len(lines) == 1 { r, _ := regexp.Compile(patternRegex) @@ -49,6 +49,7 @@ func SearchForParams(lines []string) map[string]string { } extracted := map[string]string{} + ordered_params := [][2]string{} for _, p := range params { splitted := strings.Split(p[1], "=") key := splitted[0] @@ -58,10 +59,22 @@ func SearchForParams(lines []string) map[string]string { if len(splitted) == 1 && !param_exists { extracted[key] = "" } else if len(splitted) > 1 { + // Set the value instead if it is provided extracted[key] = splitted[1] } + + // Fill in the keys only if seen for the first time to track order + if !param_exists { + ordered_params = append(ordered_params, [2]string{key, ""}) + } + } + + // Fill in the values + for i, param := range ordered_params { + pair := [2]string{param[0], extracted[param[0]]} + ordered_params[i] = pair } - return extracted + return ordered_params } return nil } diff --git a/dialog/params_test.go b/dialog/params_test.go index 129ec1b..73637f5 100644 --- a/dialog/params_test.go +++ b/dialog/params_test.go @@ -9,23 +9,15 @@ import ( func TestSearchForParams(t *testing.T) { command := " hello" - params := map[string]string{ - "a": "1", - "b": "", + want := [][2]string{ + {"a", "1"}, + {"b", ""}, } got := SearchForParams([]string{command}) - for key, value := range params { - if got[key] != value { - t.Fatalf("wanted param '%s' to equal '%s', got '%s'", key, value, got[key]) - } - } - - for key, value := range got { - if params[key] != value { - t.Fatalf("wanted param '%s' to equal '%s', got '%s'", key, value, got[key]) - } + if diff := deep.Equal(want, got); diff != nil { + t.Fatal(diff) } } @@ -42,24 +34,16 @@ func TestSearchForParams_WithNoParams(t *testing.T) { func TestSearchForParams_WithMultipleParams(t *testing.T) { command := " " - params := map[string]string{ - "a": "1", - "b": "", - "c": "3", + want := [][2]string{ + {"a", "1"}, + {"b", ""}, + {"c", "3"}, } got := SearchForParams([]string{command}) - for key, value := range params { - if got[key] != value { - t.Fatalf("wanted param '%s' to equal '%s', got '%s'", key, value, got[key]) - } - } - - for key, value := range got { - if params[key] != value { - t.Fatalf("wanted param '%s' to equal '%s', got '%s'", key, value, got[key]) - } + if diff := deep.Equal(want, got); diff != nil { + t.Fatal(diff) } } @@ -76,31 +60,23 @@ func TestSearchForParams_WithEmptyCommand(t *testing.T) { func TestSearchForParams_WithNewline(t *testing.T) { command := " hello\n" - params := map[string]string{ - "a": "1", - "b": "", - "c": "3", + want := [][2]string{ + {"a", "1"}, + {"b", ""}, + {"c", "3"}, } got := SearchForParams([]string{command}) - for key, value := range params { - if got[key] != value { - t.Fatalf("wanted param '%s' to equal '%s', got '%s'", key, value, got[key]) - } - } - - for key, value := range got { - if params[key] != value { - t.Fatalf("wanted param '%s' to equal '%s', got '%s'", key, value, got[key]) - } + if diff := deep.Equal(want, got); diff != nil { + t.Fatal(diff) } } func TestSearchForParams_InvalidParamFormat(t *testing.T) { command := " hello" - want := map[string]string{ - "b": "", + want := [][2]string{ + {"b", ""}, } got := SearchForParams([]string{command}) @@ -111,8 +87,8 @@ func TestSearchForParams_InvalidParamFormat(t *testing.T) { func TestSearchForParams_ConfusingBrackets(t *testing.T) { command := "cat < \nEOF" - want := map[string]string{ - "file": "path/to/file", + want := [][2]string{ + {"file", "path/to/file"}, } got := SearchForParams([]string{command}) if diff := deep.Equal(want, got); diff != nil { @@ -122,8 +98,8 @@ func TestSearchForParams_ConfusingBrackets(t *testing.T) { func TestSearchForParams_MultipleParamsSameKey(t *testing.T) { command := " " - want := map[string]string{ - "a": "3", + want := [][2]string{ + {"a", "3"}, } got := SearchForParams([]string{command}) @@ -134,8 +110,8 @@ func TestSearchForParams_MultipleParamsSameKey(t *testing.T) { func TestSearchForParams_MultipleParamsSameKeyDifferentValues(t *testing.T) { command := " " - want := map[string]string{ - "a": "3", + want := [][2]string{ + {"a", "3"}, } got := SearchForParams([]string{command}) @@ -146,9 +122,9 @@ func TestSearchForParams_MultipleParamsSameKeyDifferentValues(t *testing.T) { func TestSearchForParams_MultipleParamsSameKeyDifferentValues_MultipleLines(t *testing.T) { command := " \n" - want := map[string]string{ - "a": "3", - "b": "4", + want := [][2]string{ + {"a", "3"}, + {"b", "4"}, } got := SearchForParams([]string{command}) @@ -159,8 +135,8 @@ func TestSearchForParams_MultipleParamsSameKeyDifferentValues_MultipleLines(t *t func TestSearchForParams_MultipleParamsSameKeyDifferentValues_InvalidFormat(t *testing.T) { command := " " - want := map[string]string{ - "a": "3", + want := [][2]string{ + {"a", "3"}, } got := SearchForParams([]string{command}) @@ -171,9 +147,9 @@ func TestSearchForParams_MultipleParamsSameKeyDifferentValues_InvalidFormat(t *t func TestSearchForParams_MultipleParamsSameKeyDifferentValues_InvalidFormat_MultipleLines(t *testing.T) { command := " " - want := map[string]string{ - "a": "2", - "b": "4", + want := [][2]string{ + {"a", "2"}, + {"b", "4"}, } got := SearchForParams([]string{command}) @@ -185,8 +161,8 @@ func TestSearchForParams_MultipleParamsSameKeyDifferentValues_InvalidFormat_Mult func TestSearchForParams_MultipleParamsSameKeyDifferentValues_InvalidFormat_MultipleLines2(t *testing.T) { command := " \n Select next, ENTER => Execute command):", command, []int{maxX / 10, maxY / 10, (maxX / 2) + (maxX / 3), maxY/10 + 5}, false) idx := 0 - for k, v := range params { + + // Create a view for each param + for _, pair := range params { + // Unpack parameter key and value + k, v := pair[0], pair[1] generateView(g, k, v, []int{maxX / 10, (maxY / 4) + (idx+1)*layoutStep, maxX/10 + 20, (maxY / 4) + 2 + (idx+1)*layoutStep}, true) idx++