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 for mutation parser.Node types #1121

Merged
merged 6 commits into from
Feb 13, 2021

Conversation

brycecr
Copy link
Contributor

@brycecr brycecr commented Feb 6, 2021

What this PR does / why we need it:

Add DeepCopy Methods to Node-implementing types List, Path, and Object. Per #933, some pointers on these objects might not be properly copied in AssignMetadata.DeepCopy (where a slice of nodes was copied with copy); in general, these seem like useful methods to have.

Which issue(s) this PR fixes:
Fixes #993

@brycecr brycecr changed the title deepcopy deepcopy for Node types Feb 6, 2021
@brycecr brycecr changed the title deepcopy for Node types deepcopy for mutation parser.Node types Feb 6, 2021
@codecov-io
Copy link

Codecov Report

Merging #1121 (ab31d4c) into master (db5a580) will increase coverage by 0.48%.
The diff coverage is 91.30%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1121      +/-   ##
==========================================
+ Coverage   47.84%   48.33%   +0.48%     
==========================================
  Files          62       62              
  Lines        4264     4283      +19     
==========================================
+ Hits         2040     2070      +30     
+ Misses       1966     1959       -7     
+ Partials      258      254       -4     
Flag Coverage Δ
unittests 48.33% <91.30%> (+0.48%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
pkg/mutation/assignmeta_mutator.go 47.05% <0.00%> (+1.34%) ⬆️
pkg/mutation/path/parser/node.go 77.77% <100.00%> (+77.77%) ⬆️
...onstrainttemplate/constrainttemplate_controller.go 55.66% <0.00%> (+2.91%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update db5a580...ab31d4c. Read the comment docs.

}
for i := 0; i < len(r.Nodes); i++ {
var c Node
switch n := r.Nodes[i].(type) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We could have the signature for DeepCopy() be:

DeepCopy() Node

and add it to the interface, that would eliminate the need for the switch statement.

This is similar to what K8s does for objects, though I think it's a bit different than what we're doing for other parts of mutation.

Curious to hear others' thoughts on returning an interface vs. an object from DeepCopy

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I'd be curious what others thoughts are as well as I was trying to decide what to do here. I think if you add DeepCopy() Node to the interface, it is not fulfilled by DeepCopy() Path, for instance, so one has to actually specify Node as the return type.

That means that uses of DeepCopy that want to assign to specific types have to assert/cast (this describes the current use in assignMetadata), but it also means that you don't need type-switch statements if you're copying Nodes without knowing their concrete type.

I went this way after some cursory looking through client-go, where it seems like DeepCopy is defined on all of the concrete types...but it's possible this isn't the way to go or that I read that code a bit wrong :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, you're totally right. K8s uses DeepCopyObject when it expects a return type if the runtime.Object interface.

Copy link
Contributor

Choose a reason for hiding this comment

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

So by that analog we could add a

func (n TypeGoesHere) DeepCopyNode() Node {
    return n.DeepCopy()
}

to avoid the type switch and provide both options?

Copy link
Contributor

Choose a reason for hiding this comment

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

Either way lgtm.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like that, like having a cake and DeepCopying it so you can eat it too

See new patch.

Signed-off-by: Bryce Cronkite-Ratcliff <brycecr@gmail.com>
Signed-off-by: Bryce Cronkite-Ratcliff <brycecr@gmail.com>
Signed-off-by: Bryce Cronkite-Ratcliff <brycecr@gmail.com>
Copy link
Contributor

@shomron shomron left a comment

Choose a reason for hiding this comment

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

🍰 🍰 🍰

@maxsmythe
Copy link
Contributor

for some reason the mutation e2e tests are breaking now

Signed-off-by: Bryce Cronkite-Ratcliff <brycecr@gmail.com>
@maxsmythe
Copy link
Contributor

Good find!

@brycecr brycecr merged commit 7a7df04 into open-policy-agent:master Feb 13, 2021
@brycecr brycecr deleted the brycecr-deepcopy branch February 13, 2021 01:51
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

Successfully merging this pull request may close these issues.

Mutation Path and Node objects Need Deepcopy Methods
5 participants