From ef36b3e4f66c6348f8891a15880cd6b00a96ace0 Mon Sep 17 00:00:00 2001 From: kargakis Date: Thu, 24 Sep 2015 17:16:39 +0200 Subject: [PATCH] deep-copies: Structs cannot be nil --- pkg/api/deep_copy_generated.go | 2 -- pkg/api/v1/deep_copy_generated.go | 2 -- pkg/apis/extensions/deep_copy_generated.go | 2 -- .../extensions/v1beta1/deep_copy_generated.go | 2 -- pkg/runtime/deep_copy_generator.go | 18 ++++++++++++------ 5 files changed, 12 insertions(+), 14 deletions(-) diff --git a/pkg/api/deep_copy_generated.go b/pkg/api/deep_copy_generated.go index 8b73d005346..95c720831e1 100644 --- a/pkg/api/deep_copy_generated.go +++ b/pkg/api/deep_copy_generated.go @@ -2267,8 +2267,6 @@ func deepCopy_resource_Quantity(in resource.Quantity, out *resource.Quantity, c if in.Amount != nil { if newVal, err := c.DeepCopy(in.Amount); err != nil { return err - } else if newVal == nil { - out.Amount = nil } else { out.Amount = newVal.(*inf.Dec) } diff --git a/pkg/api/v1/deep_copy_generated.go b/pkg/api/v1/deep_copy_generated.go index 87cbcf4b71c..ba9ada07cb1 100644 --- a/pkg/api/v1/deep_copy_generated.go +++ b/pkg/api/v1/deep_copy_generated.go @@ -34,8 +34,6 @@ func deepCopy_resource_Quantity(in resource.Quantity, out *resource.Quantity, c if in.Amount != nil { if newVal, err := c.DeepCopy(in.Amount); err != nil { return err - } else if newVal == nil { - out.Amount = nil } else { out.Amount = newVal.(*inf.Dec) } diff --git a/pkg/apis/extensions/deep_copy_generated.go b/pkg/apis/extensions/deep_copy_generated.go index 3d2d5fe70dc..18bfadde0af 100644 --- a/pkg/apis/extensions/deep_copy_generated.go +++ b/pkg/apis/extensions/deep_copy_generated.go @@ -809,8 +809,6 @@ func deepCopy_resource_Quantity(in resource.Quantity, out *resource.Quantity, c if in.Amount != nil { if newVal, err := c.DeepCopy(in.Amount); err != nil { return err - } else if newVal == nil { - out.Amount = nil } else { out.Amount = newVal.(*inf.Dec) } diff --git a/pkg/apis/extensions/v1beta1/deep_copy_generated.go b/pkg/apis/extensions/v1beta1/deep_copy_generated.go index 90e7cec003a..c9ceebd6193 100644 --- a/pkg/apis/extensions/v1beta1/deep_copy_generated.go +++ b/pkg/apis/extensions/v1beta1/deep_copy_generated.go @@ -34,8 +34,6 @@ func deepCopy_resource_Quantity(in resource.Quantity, out *resource.Quantity, c if in.Amount != nil { if newVal, err := c.DeepCopy(in.Amount); err != nil { return err - } else if newVal == nil { - out.Amount = nil } else { out.Amount = newVal.(*inf.Dec) } diff --git a/pkg/runtime/deep_copy_generator.go b/pkg/runtime/deep_copy_generator.go index 5dee4e263ab..38fb54f5c47 100644 --- a/pkg/runtime/deep_copy_generator.go +++ b/pkg/runtime/deep_copy_generator.go @@ -403,7 +403,8 @@ func (g *deepCopyGenerator) writeDeepCopyForPtr(b *buffer, inField reflect.Struc ifStmt := fmt.Sprintf(ifFormat, inField.Name) b.addLine(ifStmt, indent) - switch inField.Type.Elem().Kind() { + kind := inField.Type.Elem().Kind() + switch kind { case reflect.Map, reflect.Ptr, reflect.Slice, reflect.Interface, reflect.Struct: if _, found := g.copyables[inField.Type.Elem()]; found { newFormat := "out.%s = new(%s)\n" @@ -420,8 +421,10 @@ func (g *deepCopyGenerator) writeDeepCopyForPtr(b *buffer, inField reflect.Struc ifStmt := fmt.Sprintf(ifFormat, inField.Name) b.addLine(ifStmt, indent+1) b.addLine("return err\n", indent+2) - b.addLine("} else if newVal == nil {\n", indent+1) - b.addLine(fmt.Sprintf("out.%s = nil\n", inField.Name), indent+2) + if kind != reflect.Struct { + b.addLine("} else if newVal == nil {\n", indent+1) + b.addLine(fmt.Sprintf("out.%s = nil\n", inField.Name), indent+2) + } b.addLine("} else {\n", indent+1) assignFormat := "out.%s = newVal.(%s)\n" assignStmt := fmt.Sprintf(assignFormat, inField.Name, g.typeName(inField.Type)) @@ -455,7 +458,8 @@ func (g *deepCopyGenerator) writeDeepCopyForSlice(b *buffer, inField reflect.Str forStmt := fmt.Sprintf(forFormat, inField.Name) b.addLine(forStmt, indent+1) - switch inField.Type.Elem().Kind() { + kind := inField.Type.Elem().Kind() + switch kind { case reflect.Map, reflect.Ptr, reflect.Slice, reflect.Interface, reflect.Struct: if _, found := g.copyables[inField.Type.Elem()]; found { assignFormat := "if err := %s(in.%s[i], &out.%s[i], c); err != nil {\n" @@ -469,8 +473,10 @@ func (g *deepCopyGenerator) writeDeepCopyForSlice(b *buffer, inField reflect.Str ifStmt := fmt.Sprintf(ifFormat, inField.Name) b.addLine(ifStmt, indent+2) b.addLine("return err\n", indent+3) - b.addLine("} else if newVal == nil {\n", indent+2) - b.addLine(fmt.Sprintf("out.%s[i] = nil\n", inField.Name), indent+3) + if kind != reflect.Struct { + b.addLine("} else if newVal == nil {\n", indent+2) + b.addLine(fmt.Sprintf("out.%s[i] = nil\n", inField.Name), indent+3) + } b.addLine("} else {\n", indent+2) assignFormat := "out.%s[i] = newVal.(%s)\n" assignStmt := fmt.Sprintf(assignFormat, inField.Name, g.typeName(inField.Type.Elem()))