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

remove overwrite from initProvider-forProvider merge #260

Merged

Conversation

lsviben
Copy link
Contributor

@lsviben lsviben commented Aug 18, 2023

Description of your changes

This resolves a TODO left from #237 where during the spec.initProvider to spec.forProvider merge the fields in spec.initProvider would overwrite the spec.forProvider ones, instead
of skipping those fields.

Although we would expect and suggest not to set the same fields for both, it still good to fix this behaviour.

To merge the 2, we used mergo, with the option SliceDeepCopy to merge
the slices from initProvider to forProvider.

err = mergo.Merge(&params, initParams, mergo.WithSliceDeepCopy)

Looking deeper, we can see the the mergo.WithSliceDeepCopy also sets the Overwrite to true

So simply setting the overwrite to false does the trick.

err = mergo.Merge(&params, initParams, mergo.WithSliceDeepCopy, func(c *mergo.Config) {
	c.Overwrite = false
})

Fixes #240

I have:

  • Read and followed Crossplane's contribution process.
  • Run make reviewable to ensure this PR is ready for review.
  • [] Added backport release-x.y labels to auto-backport this PR if necessary.

How has this code been tested

Extended the unit test to test for overwrites.

Signed-off-by: lsviben <sviben.lovro@gmail.com>
Copy link
Collaborator

@ulucinar ulucinar left a comment

Choose a reason for hiding this comment

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

Thanks @lsviben, lgtm.
Looks like we need to fix the reporting of some tests in pkg/terraform/files_test.go but this is not a blocker for this PR. E.g.,

diff --git a/pkg/terraform/files_test.go b/pkg/terraform/files_test.go
index ec38af8..353fd31 100644
--- a/pkg/terraform/files_test.go
+++ b/pkg/terraform/files_test.go
@@ -488,8 +488,8 @@ func TestWriteMainTF(t *testing.T) {
                        if err = json.JSParser.Unmarshal([]byte(tc.want.maintf), &wantJson); err != nil {
                                t.Errorf("cannot unmarshal want main.tf.json: %v", err)
                        }
-                       if diff := cmp.Diff(res, wantJson, test.EquateConditions()); diff != "" {
-                               t.Errorf("\n%s\nWriteMainTF(...): -want error, +got error:\n%s", tc.reason, diff)
+                       if diff := cmp.Diff(wantJson, res); diff != "" {
+                               t.Errorf("\n%s\nWriteMainTF(...): -want main.tf.json, +got main.tf.json:\n%s", tc.reason, diff)
                        }
                })
        }

@ulucinar ulucinar merged commit 1a41e99 into crossplane:main Aug 21, 2023
5 checks passed
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.

Improve spec.initProvider to spec.forProvider merge
2 participants