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

deepcopy: val.DeepCopyinterface undefined #50

Closed
antoineco opened this issue Jul 17, 2018 · 5 comments
Closed

deepcopy: val.DeepCopyinterface undefined #50

antoineco opened this issue Jul 17, 2018 · 5 comments

Comments

@antoineco
Copy link
Contributor

The following types:

type Foo struct {
	metav1.TypeMeta
	metav1.ObjectMeta
	Spec FooSpec
}

type FooSpec struct {
	Parameters map[string]interface{}
}

yield the following code:

// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *FooSpec) DeepCopyInto(out *FooSpec) {
	*out = *in
	if in.Parameters != nil {
		in, out := &in.Parameters, &out.Parameters
		*out = make(map[string]interface{}, len(*in))
		for key, val := range *in {
			if val == nil {
				(*out)[key] = nil
			} else {
				(*out)[key] = val.DeepCopyinterface{}()
			}
		}
	}
	return
}

which yields the following compiler error:

val.DeepCopyinterface undefined (type interface {} is interface with no methods

@antoineco
Copy link
Contributor Author

I understand why now. Interfaces must implement DeepCopy<Type-Name>(), so one must rewrite the above FooSpec type like:

type FooSpec struct {
	Parameters map[string]P
}

type P interface {
	DeepCopyP() P
}

@yuchengwu
Copy link

But interface{} and P are different things. interface{} is an empty interface may hold values of any type, P holds values of any type that must implement DeepCopyP() method.

@antoineco
Copy link
Contributor Author

Yes, I created a dummy type because I couldn't think of a better way.

@yuchengwu
Copy link

Me too, currently I manually replace val.DeepCopyinterface{}() with Copy(val), not a solution but it works

@lzcmoody
Copy link

lzcmoody commented Sep 12, 2019

Hi, I found a workaround to solve this issue, anyway it's works.

type FooSpec struct { Parameters Values json:"parameters,omitempty" }
type Values struct { Objects map[string]interface{} json:"Objects,omitempty" }
func (in *Values) DeepCopyInto(out *Values) { // here's you deepcopy implementation }

If you define your own DeepCopyInfo in types.go, zz_generated.deepcopy.go will not auto-generate this function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants