-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1bf86a1
commit d86cad9
Showing
2 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |