-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[pdata] Implement Go 1.23 style iterators #11982
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
Comments
@yurishkuro good call, will do when we move to minimum supported version to be 1.23 |
I think this is a perfect use case for iterators, the way this is implemented today is quite verbose to write and almost every single use case requires looping over the entries in some way. I suggest we go with an Since this involves changes in autogenerated code, there might be oportunities for reusing existing iterator routines from the standard library, for example |
I started working on this main...mauri870:opentelemetry-collector:pdata-iterator-all. It requires Go 1.23 so we cannot accept it until all the other collector packages are bumped to 1.23. |
Now that the ecosystem moved to Go 1.23 I will resume working on this. I should have a PR up this week. |
#### Description This PR adds support for iterators to Slice and Map types and their autogenerated counterparts. Iterators were stabilized in Go 1.23. The All method is analogous to [maps.All](https://pkg.go.dev/maps#All) and [slices.All ](https://pkg.go.dev/slices#All) and is a more idiomatic alternative to the more verbose `for i := 0; i < es.Len(); i++ { z := es.At(i) }` way of looping. Code that is written like this today: ```go for i := 0; i < ld.ResourceLogs().Len(); i++ { rl := ld.ResourceLogs().At(i) for j := 0; j < rl.ScopeLogs().Len(); j++ { sl := rl.ScopeLogs().At(j) for k := 0; k < sl.LogRecords().Len(); k++ { lr := sl.LogRecords().At(k) // use lr } } } ``` Can now be written like this: ```go for _, rl := range ld.ResourceLogs().All() { for _, sl := range rl.ScopeLogs().All() { for _, lr := range sl.LogRecords().All() { // use lr } } } ``` #### Link to tracking issue Fixes #11982
Uh oh!
There was an error while loading. Please reload this page.
Is your feature request related to a problem? Please describe.
Iterating through
pdata
structures today is pretty convoluted, e.g.Describe the solution you'd like
Go 1.23 introduced the ability to use iterators for looping through custom structs. The above code could look like this, if we introduce iterator functions to pdata, e.g.
Seq()
andSeq2()
:The text was updated successfully, but these errors were encountered: