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

Add conversion to properly parse query parameter propagationPolicy #63414

Merged
merged 1 commit into from
May 25, 2018
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
12 changes: 12 additions & 0 deletions staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ func AddConversionFuncs(scheme *runtime.Scheme) error {
Convert_unversioned_LabelSelector_to_map,

Convert_Slice_string_To_Slice_int32,

Convert_Slice_string_To_v1_DeletionPropagation,
)
}

Expand Down Expand Up @@ -304,3 +306,13 @@ func Convert_Slice_string_To_Slice_int32(in *[]string, out *[]int32, s conversio
}
return nil
}

// Convert_Slice_string_To_v1_DeletionPropagation allows converting a URL query parameter propagationPolicy
func Convert_Slice_string_To_v1_DeletionPropagation(input *[]string, out *DeletionPropagation, s conversion.Scope) error {
if len(*input) > 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you need to check if *input is nil first?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's enforced by

func (c *Converter) doConversion(src, dest interface{}, flags FieldMatchingFlags, meta *Meta, f conversionFunc) error {

through
func EnforcePtr(obj interface{}) (reflect.Value, error) {
v := reflect.ValueOf(obj)
if v.Kind() != reflect.Ptr {
if v.Kind() == reflect.Invalid {
return reflect.Value{}, fmt.Errorf("expected pointer, but got invalid kind")
}
return reflect.Value{}, fmt.Errorf("expected pointer, but got %v type", v.Type())
}
if v.IsNil() {
return reflect.Value{}, fmt.Errorf("expected pointer, but got nil")
}
return v.Elem(), nil
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok thanks 👍

*out = DeletionPropagation((*input)[0])
} else {
*out = ""
}
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,38 @@ func TestMapToLabelSelectorRoundTrip(t *testing.T) {
}
}
}

func TestConvertSliceStringToDeletionPropagation(t *testing.T) {
tcs := []struct {
Input []string
Output v1.DeletionPropagation
}{
{
Input: nil,
Output: "",
},
{
Input: []string{},
Output: "",
},
{
Input: []string{"foo"},
Output: "foo",
},
{
Input: []string{"bar", "foo"},
Output: "bar",
},
}

for _, tc := range tcs {
var dp v1.DeletionPropagation
if err := v1.Convert_Slice_string_To_v1_DeletionPropagation(&tc.Input, &dp, nil); err != nil {
t.Errorf("Convert_Slice_string_To_v1_DeletionPropagation(%#v): %v", tc.Input, err)
continue
}
if !apiequality.Semantic.DeepEqual(dp, tc.Output) {
t.Errorf("slice string to DeletionPropagation conversion failed: got %v; want %v", dp, tc.Output)
}
}
}