Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting map[string]string from the command line #15989

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading