-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
[chore][docs/coding-guidelines] Document approach to optional fields #12352
base: main
Are you sure you want to change the base?
Conversation
0a8c498
to
258c11c
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #12352 +/- ##
=======================================
Coverage 91.96% 91.96%
=======================================
Files 467 467
Lines 25459 25459
=======================================
Hits 23414 23414
Misses 1639 1639
Partials 406 406 ☔ View full report in Codecov by Sentry. |
When representing optional fields in the configuration, we distinguish two cases: | ||
1. For scalars (e.g. `int`, `string`, `bool`, `time.Duration`...), the zero value of the base type | ||
is used to represent the absence of the field. The default value for this field may be different | ||
from the zero value, so, when using the Go API you MUST follow the guidance on the "Default | ||
Configuration" section below. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When representing optional fields in the configuration, we distinguish two cases: | |
1. For scalars (e.g. `int`, `string`, `bool`, `time.Duration`...), the zero value of the base type | |
is used to represent the absence of the field. The default value for this field may be different | |
from the zero value, so, when using the Go API you MUST follow the guidance on the "Default | |
Configuration" section below. | |
When representing optional fields in the configuration for which the zero value indicates the absence of the field, we distinguish two cases: | |
1. For scalars (e.g. `int`, `string`, `bool`, `time.Duration`...), the zero value of the base type | |
should be used to represent the absence of the field instead of a pointer. The default value for this field may be different | |
from the zero value, so, when using the Go API you MUST follow the guidance on the "Default | |
Configuration" section below. |
I think we should limit this to only applying to the case where the zero value is not a valid value for the field and can indicate absence of the field. For cases where this doesn't apply, I think we still need to evaluate whether to use pointers or an optional type.
is used to represent the absence of the field. The default value for this field may be different | ||
from the zero value, so, when using the Go API you MUST follow the guidance on the "Default | ||
Configuration" section below. | ||
2. For struct fields, you may use a pointer to struct to represent an optional sections. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if we should suggest just using non-pointer values for now so we can be consistent across structs.
Our stable config modules don't use pointers for any struct fields, so this would allow us to be consistent. Once we've arrived at a decision, we can later indicate that pointer values (or optional types) should be used for types where zero values don't indicate absence of the field.
@yurishkuro Sorry, #11755 stalling is on me. I'd like to confirm that we can confidently say it's not a breaking change for proceeding. I will try to get back to it soon and see if we can progress it further, thank you for your patience. The goal of this PR is to unblock the decision of whether to adopt optional types from stabilizing a few config modules so we can consider it separately from our 1.0 efforts. I would still like them, but this allows us to be more deliberate. |
@evan-bradley tldr is it's using
|
I agree about the benefits of the What this solves is cases like #12323 where we are working with values where zero doesn't indicate a valid value for the field. In particular with that PR, the underlying Go standard library we are configuring considers a value of I think the case you're calling out is more complex and would benefit from an optional type. |
@yurishkuro Do you think |
@mx-psi definitely any complex defaults would benefit, because right now they require overriding Unmarshal with some type-unsafe checks for field names. For the primitive fields I don't have a strong opinion. On one hand, if we focused on actually landing |
Because it would be an API breaking change to change it in the future and we want to avoid doing a v2 just because of that. |
I see. Changing to |
Description
Documents the approach discussed on 2025-02-10 stability meeting.
Link to tracking issue
I argue that this, together with #12323, fixes #9478.
We may still want to add an optional wrapper in the future, this is covered by #10266