From c79916b1ba92590482f7df477c8492080de4413a Mon Sep 17 00:00:00 2001 From: Simon Bein Date: Mon, 24 Apr 2023 20:19:33 +0200 Subject: [PATCH] Add DeepCopy for ResourceMeta and ObjectMeta --- kyaml/yaml/types.go | 50 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/kyaml/yaml/types.go b/kyaml/yaml/types.go index 0955883e0c..897e2edaaf 100644 --- a/kyaml/yaml/types.go +++ b/kyaml/yaml/types.go @@ -247,3 +247,53 @@ type MergeOptions struct { // source list to destination or append. ListIncreaseDirection MergeOptionsListIncreaseDirection } + +// Since ObjectMeta and TypeMeta are stable, we manually create DeepCopy funcs for ResourceMeta and ObjectMeta. +// For TypeMeta and NameMeta no DeepCopy funcs are required, as they only contain basic types. + +// DeepCopyInto copies the receiver, writing into out. in must be non-nil. +func (in *ObjectMeta) DeepCopyInto(out *ObjectMeta) { + *out = *in + out.NameMeta = in.NameMeta + if in.Labels != nil { + in, out := &in.Labels, &out.Labels + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } + if in.Annotations != nil { + in, out := &in.Annotations, &out.Annotations + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } +} + +// DeepCopy copies the receiver, creating a new ObjectMeta. +func (in *ObjectMeta) DeepCopy() *ObjectMeta { + if in == nil { + return nil + } + out := new(ObjectMeta) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto copies the receiver, writing into out. in must be non-nil. +func (in *ResourceMeta) DeepCopyInto(out *ResourceMeta) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) +} + +// DeepCopy copies the receiver, creating a new ResourceMeta. +func (in *ResourceMeta) DeepCopy() *ResourceMeta { + if in == nil { + return nil + } + out := new(ResourceMeta) + in.DeepCopyInto(out) + return out +}