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 DeepCopy for AppSpec #165

Merged
merged 1 commit into from
May 14, 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
16 changes: 6 additions & 10 deletions drivers/scheduler/spec/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io/ioutil"
"path"

"github.com/mohae/deepcopy"
"github.com/portworx/torpedo/pkg/errors"
"github.com/sirupsen/logrus"
)
Expand All @@ -29,11 +28,10 @@ func (f *Factory) register(id string, app *AppSpec) {
// Get returns a registered application
func (f *Factory) Get(id string) (*AppSpec, error) {
if d, ok := appSpecFactory[id]; ok && d.Enabled {
dCopy := deepcopy.Copy(d)
if copy, ok := dCopy.(*AppSpec); ok {
return copy, nil
if copy := d.DeepCopy(); copy != nil {
return d.DeepCopy(), nil
}
return nil, fmt.Errorf("error creating copy of spec: %v", d)
return nil, fmt.Errorf("error creating copy of app: %v", d)
}

return nil, &errors.ErrNotFound{
Expand All @@ -47,11 +45,9 @@ func (f *Factory) GetAll() []*AppSpec {
var specs []*AppSpec
for _, val := range appSpecFactory {
if val.Enabled {
valCopy := deepcopy.Copy(val)
if copy, ok := valCopy.(*AppSpec); ok {
specs = append(specs, copy)
} else {
logrus.Errorf("Error creating copy of spec: %v", val)
valCopy := val.DeepCopy()
if valCopy != nil {
specs = append(specs, valCopy)
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions drivers/scheduler/spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,18 @@ type AppSpec struct {
// Enabled indicates if the application is enabled in the factory
Enabled bool
}

// DeepCopy Creates a copy of the AppSpec
func (in *AppSpec) DeepCopy() *AppSpec {
if in == nil {
return nil
}
out := new(AppSpec)
out.Key = in.Key
out.Enabled = in.Enabled
out.SpecList = make([]interface{}, 0)
for _, spec := range in.SpecList {
out.SpecList = append(out.SpecList, spec)
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure how this will work when spec is a pointer
as we discussed, in future when we run tests in parallel, and modify the specs, it could be a problem

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, will need to be changed when we start running tests in parallel.

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay; let's put a TODO here and/or create an issue to track it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Created Issue #166

}
return out
}