diff --git a/pkg/parameters/parameters.go b/pkg/parameters/parameters.go new file mode 100644 index 0000000000..87c62d65c6 --- /dev/null +++ b/pkg/parameters/parameters.go @@ -0,0 +1,44 @@ +/* +Copyright 2018 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package parameters + +import ( + "fmt" + "strings" +) + +// ParseVariableAssignments converts a string array of variable assignments +// into a map of keys and values +// Example: +// [a=b c=abc1232=== d=banana d=pineapple] becomes map[a:b c:abc1232=== d:[pineapple]] +func ParseVariableAssignments(params []string) (map[string]string, error) { + variables := make(map[string]string) + for _, p := range params { + + parts := strings.SplitN(p, "=", 2) + if len(parts) < 2 { + return nil, fmt.Errorf("invalid parameter (%s), must be in name=value format", p) + } + + variable := strings.TrimSpace(parts[0]) + if variable == "" { + return nil, fmt.Errorf("invalid parameter (%s), variable name is required", p) + } + value := strings.TrimSpace(parts[1]) + + variables[variable] = value + } + + return variables, nil +} diff --git a/pkg/parameters/parameters_test.go b/pkg/parameters/parameters_test.go new file mode 100644 index 0000000000..577898c18d --- /dev/null +++ b/pkg/parameters/parameters_test.go @@ -0,0 +1,74 @@ +/* +Copyright 2018 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package parameters + +import ( + "reflect" + "testing" +) + +func TestParseVariableAssignments(t *testing.T) { + testcases := []struct { + Name, Raw, Variable, Value string + }{ + {"simple", "a=b", "a", "b"}, + {"multiple equal signs", "c=abc1232===", "c", "abc1232==="}, + {"empty value", "d=", "d", ""}, + {"extra whitespace", " a = b ", "a", "b"}, + } + + for _, tc := range testcases { + t.Run(tc.Name, func(t *testing.T) { + + params := []string{tc.Raw} + + got, err := ParseVariableAssignments(params) + if err != nil { + t.Fatal(err) + } + + want := make(map[string]string) + want[tc.Variable] = tc.Value + if !reflect.DeepEqual(want, got) { + t.Fatalf("%s\nexpected:\n\t%v\ngot:\n\t%v\n", tc.Raw, want, got) + } + }) + } +} + +func TestParseVariableAssignments_MissingVariableName(t *testing.T) { + params := []string{"=b"} + + _, err := ParseVariableAssignments(params) + if err == nil { + t.Fatal("should have failed due to a missing variable name") + } +} + +func TestParseVariableAssignments_OneVariableThreeValues(t *testing.T) { + params := []string{"a=banana", "a=pineapple", "a=coconut"} + + got, err := ParseVariableAssignments(params) + if err != nil { + t.Fatal(err) + } + + want := map[string]string{ + "a": "coconut", + } + + if !reflect.DeepEqual(want, got) { + t.Fatalf("%s\nexpected:\n\t%v\ngot:\n\t%v\n", "one var three values", want, got) + } +}