-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[receiver/otlpreceiver] Use configoptional type #13119
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
[receiver/otlpreceiver] Use configoptional type #13119
Conversation
Codecov ReportAttention: Patch coverage is
❌ Your patch status has failed because the patch coverage (94.11%) is below the target coverage (95.00%). You can increase the patch coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #13119 +/- ##
==========================================
- Coverage 91.60% 91.59% -0.01%
==========================================
Files 506 506
Lines 28544 28533 -11
==========================================
- Hits 26147 26136 -11
Misses 1882 1882
Partials 515 515 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
I think this one should not have any controversy ( |
func GetOrInsertDefault[T any](t *testing.T, opt *configoptional.Optional[T]) *T { | ||
if opt.HasValue() { | ||
return opt.Get() | ||
} | ||
|
||
empty := confmap.NewFromStringMap(map[string]any{}) | ||
require.NoError(t, empty.Unmarshal(opt)) | ||
val := opt.Get() | ||
require.NotNil(t, "Expected a default value to be set for %T", val) | ||
return val | ||
} |
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 think this is useful enough to be part of the Optional type API, something like what the mo package provides:
// MapNone executes the mapper function if value is absent or returns Option.
func (o Optional[T]) MapNone(mapper func() (T, bool)) Optional[T] {
if o.hasValue {
return Some(o.value)
}
return ToOption(mapper())
}
(no need to change this PR)
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.
Yeah originally in one of my PoCs this was a method in Optional
itself, but I got feedback that if it was only used in tests we could just not have it as part of configoptional. I think it may make sense to add it to the main API/a test module, but since we don't have to decide that now, I would rather keep it here
if !r.cfg.GRPC.HasValue() { | ||
return nil | ||
} | ||
|
||
grpcCfg := r.cfg.GRPC.Get() |
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.
We could simplify these cases by extending the Get()
to return (T, bool) instead.
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.
Considering Get()
returns a *T
, I think you could just check whether it's nil.
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.
That is a good point. Since that does not change the API substantially I would prefer also doing this in a separate PR. I'll wait to see what others say and file an issue to address this before we mark configoptional 1.0
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.
What about creating another function for pointer return ToPointer
and Get
returning the value? The return type could be a private empty one return empty[T](), false
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.
Considering
Get()
returns a*T
, I think you could just check whether it's nil.
I mean, yeah, but that is not the usual Go pattern
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 was thinking about this during the PR for adding the configoptional package. I like that HasValue
gates the rest of the function and avoids nesting, but I see the value in the other approaches as well.
My thought was that we see how this is used in a few places and see which pattern ends up being the cleanest for the typical case. From what I can tell, outside the reduced API of removing HasValue
and checking whether *T
is nil or ok
is true for (T, bool)
, none of the options are substantially cleaner than the others.
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.
Let's continue over at #13160
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.
Sorry for the slow review. I like the way this looks.
I am going to merge this, although I want to make some changes to |
e8ca607
Description
Uses
configoptional.Optional
for fields inprotocols
section.Removes
Unmarshal
method since it is no longer needed.These are both breaking changes, I think it would be a bit difficult to do this in two steps, I am happy to work on fixing contrib after this is merged.
Link to tracking issue
Fixes #12980