Skip to content

Commit

Permalink
Borrow param parsing from svcat
Browse files Browse the repository at this point in the history
  • Loading branch information
carolynvs-msft committed Mar 19, 2019
1 parent 1bf86a1 commit d86cad9
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
44 changes: 44 additions & 0 deletions pkg/parameters/parameters.go
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
}
74 changes: 74 additions & 0 deletions pkg/parameters/parameters_test.go
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)
}
}

0 comments on commit d86cad9

Please sign in to comment.