Pointer receiver support for MarshalQueryParameter()

pull/6/head
Mikhail Mazurskiy 2017-08-30 16:17:16 +10:00
parent 01e961b380
commit 0fe4911744
No known key found for this signature in database
GPG Key ID: 93551ECC96E2F568
3 changed files with 48 additions and 20 deletions

View File

@ -34,3 +34,21 @@ func TestUnstructuredList(t *testing.T) {
t.Fatalf("unexpected fields: %#v", items[0]) t.Fatalf("unexpected fields: %#v", items[0])
} }
} }
func TestNilDeletionTimestamp(t *testing.T) {
var u Unstructured
del := u.GetDeletionTimestamp()
if del != nil {
t.Errorf("unexpected non-nil deletion timestamp: %v", del)
}
u.SetDeletionTimestamp(u.GetDeletionTimestamp())
del = u.GetDeletionTimestamp()
if del != nil {
t.Errorf("unexpected non-nil deletion timestamp: %v", del)
}
metadata := u.Object["metadata"].(map[string]interface{})
deletionTimestamp := metadata["deletionTimestamp"]
if deletionTimestamp != nil {
t.Errorf("unexpected deletion timestamp field: %q", deletionTimestamp)
}
}

View File

@ -89,9 +89,16 @@ func customMarshalValue(value reflect.Value) (reflect.Value, bool) {
} }
marshaler, ok := value.Interface().(Marshaler) marshaler, ok := value.Interface().(Marshaler)
if !ok {
if !isPointerKind(value.Kind()) && value.CanAddr() {
marshaler, ok = value.Addr().Interface().(Marshaler)
if !ok { if !ok {
return reflect.Value{}, false return reflect.Value{}, false
} }
} else {
return reflect.Value{}, false
}
}
// Don't invoke functions on nil pointers // Don't invoke functions on nil pointers
// If the type implements MarshalQueryParameter, AND the tag is not omitempty, AND the value is a nil pointer, "" seems like a reasonable response // If the type implements MarshalQueryParameter, AND the tag is not omitempty, AND the value is a nil pointer, "" seems like a reasonable response

View File

@ -72,6 +72,7 @@ type childStructs struct {
SinceSeconds *int64 `json:"sinceSeconds,omitempty"` SinceSeconds *int64 `json:"sinceSeconds,omitempty"`
SinceTime *metav1.Time `json:"sinceTime,omitempty"` SinceTime *metav1.Time `json:"sinceTime,omitempty"`
EmptyTime *metav1.Time `json:"emptyTime"` EmptyTime *metav1.Time `json:"emptyTime"`
NonPointerTime metav1.Time `json:"nonPointerTime"`
} }
func (obj *childStructs) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind } func (obj *childStructs) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }
@ -183,8 +184,9 @@ func TestConvert(t *testing.T) {
SinceSeconds: &sinceSeconds, SinceSeconds: &sinceSeconds,
SinceTime: &sinceTime, // test a custom marshaller SinceTime: &sinceTime, // test a custom marshaller
EmptyTime: nil, // test a nil custom marshaller without omitempty EmptyTime: nil, // test a nil custom marshaller without omitempty
NonPointerTime: sinceTime,
}, },
expected: url.Values{"container": {"mycontainer"}, "follow": {"true"}, "previous": {"true"}, "sinceSeconds": {"123"}, "sinceTime": {"2000-01-01T12:34:56Z"}, "emptyTime": {""}}, expected: url.Values{"container": {"mycontainer"}, "follow": {"true"}, "previous": {"true"}, "sinceSeconds": {"123"}, "sinceTime": {"2000-01-01T12:34:56Z"}, "emptyTime": {""}, "nonPointerTime": {"2000-01-01T12:34:56Z"}},
}, },
{ {
input: &childStructs{ input: &childStructs{
@ -193,8 +195,9 @@ func TestConvert(t *testing.T) {
Previous: true, Previous: true,
SinceSeconds: &sinceSeconds, SinceSeconds: &sinceSeconds,
SinceTime: nil, // test a nil custom marshaller with omitempty SinceTime: nil, // test a nil custom marshaller with omitempty
NonPointerTime: sinceTime,
}, },
expected: url.Values{"container": {"mycontainer"}, "follow": {"true"}, "previous": {"true"}, "sinceSeconds": {"123"}, "emptyTime": {""}}, expected: url.Values{"container": {"mycontainer"}, "follow": {"true"}, "previous": {"true"}, "sinceSeconds": {"123"}, "emptyTime": {""}, "nonPointerTime": {"2000-01-01T12:34:56Z"}},
}, },
} }