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

📖 API conventions: add note about when struct fields should be pointer #5782

Conversation

sbueringer
Copy link
Member

Signed-off-by: Stefan Büringer [email protected]

What this PR does / why we need it:

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Dec 3, 2021
@k8s-ci-robot k8s-ci-robot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Dec 3, 2021
CONTRIBUTING.md Outdated
* Fields SHOULD be pointers if there is a good reason for it, e.g:
* the nil and the zero values (by Go standards) have semantic differences.
* Note: This doesn't apply to map or slice types as they are assignable to `nil`.
* the field is of a struct type and would show up as an empty object after marshalling (e.g. `kubectl get`)
Copy link
Member

Choose a reason for hiding this comment

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

What about

Suggested change
* the field is of a struct type and would show up as an empty object after marshalling (e.g. `kubectl get`)
* the field is of a struct type containing all omitempty values, and you want to prevent it to show up as an empty object after marshalling (e.g. `kubectl get`)

Copy link
Member Author

@sbueringer sbueringer Dec 3, 2021

Choose a reason for hiding this comment

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

So the empty object wouldn't show up if one of the fields doesn't have omitempty? (as that field would then be always rendered and thus the object wouldn't be empty).

I'm not 100% sure if we should couple the decision to the current state of the fields as the fields can change over time but changing something to a pointer is a breaking change. (which I think requires deprecating the whole field)

Probably there is no future proof way to do this.

Copy link
Member

Choose a reason for hiding this comment

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

This kinda comes down to generic kube recommendation, if it's optional make it a pointer.

Copy link
Member

Choose a reason for hiding this comment

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

An optional struct might have required fields but still be a pointer though?

Copy link
Member Author

Choose a reason for hiding this comment

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

This kinda comes down to generic kube recommendation, if it's optional make it a pointer.

I would only consider this for structs though, for basic types it's imho too painful to handle and has no upside if you don't care about the difference between unset and the default value.

An optional struct might have required fields but still be a pointer though?

Yup exactly. I think that would be fine too.

Copy link
Member Author

Choose a reason for hiding this comment

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

Btw, it's a nuance, but I intentionally wrote e.g.. So the general idea is, hey please let's only make these a pointer if there is a reason for it (aka we know what we're doing). It's not meant as an exhaustive list.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks Stefan, let's incorporate Fabrizio suggestion and proceed.

Copy link
Member Author

Choose a reason for hiding this comment

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

@vincepri fine for you too? Given that it's "just" an example

Copy link
Member Author

Choose a reason for hiding this comment

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

Rephrased accordingly. @fabriziopandini @enxebre @vincepri PTAL :)

@sbueringer sbueringer force-pushed the pr-api-conventions-struct-pointer branch from 91f64eb to 48099aa Compare December 10, 2021 08:26
@enxebre
Copy link
Member

enxebre commented Dec 10, 2021

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Dec 10, 2021
Copy link
Member

@vincepri vincepri left a comment

Choose a reason for hiding this comment

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

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: vincepri

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Dec 10, 2021
@k8s-ci-robot k8s-ci-robot merged commit 801e6a7 into kubernetes-sigs:main Dec 10, 2021
@k8s-ci-robot k8s-ci-robot added this to the v1.1 milestone Dec 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants