Skip to content

Commit

Permalink
Merge pull request kubernetes#15989 from hakman/set-string-map
Browse files Browse the repository at this point in the history
Allow setting map[string]string from the command line
  • Loading branch information
k8s-ci-robot authored Oct 3, 2023
2 parents fcb3a85 + 781fe0c commit ed6edaf
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
19 changes: 19 additions & 0 deletions util/pkg/reflectutils/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,23 @@ func setType(v reflect.Value, newValue string) error {

t := v.Type().String()

if v.Type().Kind() == reflect.Map {
if v.IsNil() {
v.Set(reflect.MakeMap(v.Type()))
}

switch t {
case "map[string]string":
name, value, _ := strings.Cut(newValue, "=")
v.SetMapIndex(reflect.ValueOf(name), reflect.ValueOf(value))

default:
return fmt.Errorf("unhandled type %q", t)
}

return nil
}

var newV reflect.Value

switch t {
Expand Down Expand Up @@ -168,6 +185,7 @@ func setType(v reflect.Value, newValue string) error {
default:
panic("missing case in int switch")
}

case "uint64", "uint32", "uint16", "uint":
v, err := strconv.Atoi(newValue)
if err != nil {
Expand All @@ -189,6 +207,7 @@ func setType(v reflect.Value, newValue string) error {
default:
panic("missing case in uint switch")
}

case "intstr.IntOrString":
newV = reflect.ValueOf(intstr.Parse(newValue))

Expand Down
23 changes: 23 additions & 0 deletions util/pkg/reflectutils/access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ type fakeObjectContainers struct {

Enum fakeEnum `json:"enum"`
EnumSlice []fakeEnum `json:"enumSlice"`

StringMap map[string]string `json:"stringMap"`
}

type fakeObjectPolicy struct {
Expand Down Expand Up @@ -197,6 +199,27 @@ func TestSet(t *testing.T) {
Path: "spec.containers[0].duration",
Value: "500ms",
},
{
Name: "set new map key",
Input: "{ 'spec': { 'containers': [ {} ] } }",
Expected: "{ 'spec': { 'containers': [ { 'stringMap': { 'ABC': '' } } ] } }",
Path: "spec.containers[0].stringMap",
Value: "ABC",
},
{
Name: "set new map key with val",
Input: "{ 'spec': { 'containers': [ {} ] } }",
Expected: "{ 'spec': { 'containers': [ { 'stringMap': { 'ABC': 'DEF' } } ] } }",
Path: "spec.containers[0].stringMap",
Value: "ABC=DEF",
},
{
Name: "set existing map key with val",
Input: "{ 'spec': { 'containers': [ { 'stringMap': { 'ABC': 'DEF' } } ] } }",
Expected: "{ 'spec': { 'containers': [ { 'stringMap': { 'ABC': 'DEF', 'GHI': 'JKL' } } ] } }",
Path: "spec.containers[0].stringMap",
Value: "GHI=JKL",
},
// Not sure if we should do this...
// {
// Name: "creating missing array elements",
Expand Down
3 changes: 1 addition & 2 deletions util/pkg/reflectutils/field_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ func ParseFieldPath(s string) (*FieldPath, error) {
token: field,
})

case '.':
// TODO: Validate that we don't have two dots?
case '.', '/':
// Skip

case '[':
Expand Down

0 comments on commit ed6edaf

Please sign in to comment.