Skip to content

Commit e4c4d2f

Browse files
authored
pdata: add iterator All method to slice and map types (#12380)
#### 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
1 parent de3b40f commit e4c4d2f

File tree

81 files changed

+1120
-9
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+1120
-9
lines changed

.chloggen/pdata-iterator-all.yaml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: pdata
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Add iterator All method to pdata slices and map types.
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [11982]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: [api]

exporter/debugexporter/internal/normal/common.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ import (
1111

1212
// writeAttributes returns a slice of strings in the form "attrKey=attrValue"
1313
func writeAttributes(attributes pcommon.Map) (attributeStrings []string) {
14-
attributes.Range(func(k string, v pcommon.Value) bool {
14+
for k, v := range attributes.All() {
1515
attribute := fmt.Sprintf("%s=%s", k, v.AsString())
1616
attributeStrings = append(attributeStrings, attribute)
17-
return true
18-
})
17+
}
1918
return attributeStrings
2019
}

exporter/debugexporter/internal/otlptext/databuffer.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,9 @@ func (b *dataBuffer) logAttributes(header string, m pcommon.Map) {
4242
attrPrefix = headerParts[0] + attrPrefix
4343
}
4444

45-
m.Range(func(k string, v pcommon.Value) bool {
45+
for k, v := range m.All() {
4646
b.logEntry("%s %s: %s", attrPrefix, k, valueToString(v))
47-
return true
48-
})
47+
}
4948
}
5049

5150
func (b *dataBuffer) logAttributesWithIndentation(header string, m pcommon.Map, indentVal int) {
@@ -64,10 +63,9 @@ func (b *dataBuffer) logAttributesWithIndentation(header string, m pcommon.Map,
6463
attrPrefix = headerParts[0] + attrPrefix
6564
}
6665

67-
m.Range(func(k string, v pcommon.Value) bool {
66+
for k, v := range m.All() {
6867
b.logEntry("%s %s: %s", attrPrefix, k, valueToString(v))
69-
return true
70-
})
68+
}
7169
}
7270

7371
func (b *dataBuffer) logInstrumentationScope(il pcommon.InstrumentationScope) {

pdata/internal/cmd/pdatagen/internal/templates/primitive_slice.go.tmpl

+11
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ func (ms {{ .structName }}) At(i int) {{ .itemType }} {
5757
return (*ms.getOrig())[i]
5858
}
5959

60+
// All returns an iterator over index-value pairs in the slice.
61+
func (ms {{ .structName }}) All() iter.Seq2[int, {{ .itemType }}] {
62+
return func(yield func(int, {{ .itemType }}) bool) {
63+
for i := 0; i < ms.Len(); i++ {
64+
if !yield(i, ms.At(i)) {
65+
return
66+
}
67+
}
68+
}
69+
}
70+
6071
// SetAt sets {{ .itemType }} item at particular index.
6172
// Equivalent of {{ .lowerStructName }}[i] = val
6273
func (ms {{ .structName }}) SetAt(i int, val {{ .itemType }}) {

pdata/internal/cmd/pdatagen/internal/templates/primitive_slice_test.go.tmpl

+13
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,19 @@ func Test{{ .structName }}EnsureCapacity(t *testing.T) {
112112
assert.Equal(t, 4, cap(*ms.getOrig()))
113113
}
114114

115+
func Test{{ .structName }}All(t *testing.T) {
116+
ms := New{{ .structName }}()
117+
ms.FromRaw([]{{ .itemType }}{ {{ .testOrigVal }} })
118+
assert.NotEmpty(t, ms.Len())
119+
120+
var c int
121+
for i, v := range ms.All() {
122+
assert.Equal(t, ms.At(i), v, "element should match")
123+
c++
124+
}
125+
assert.Equal(t, ms.Len(), c, "All elements should have been visited")
126+
}
127+
115128
func Test{{ .structName }}Equal(t *testing.T) {
116129
ms := New{{ .structName }}()
117130
ms2 := New{{ .structName }}()

pdata/internal/cmd/pdatagen/internal/templates/slice.go.tmpl

+15
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@ func (es {{ .structName }}) At(i int) {{ .elementName }} {
6262
return {{ .newElement }}
6363
}
6464

65+
// All returns an iterator over index-value pairs in the slice.
66+
//
67+
// for i, v := range es.All() {
68+
// ... // Do something with index-value pair
69+
// }
70+
func (es {{ .structName }}) All() iter.Seq2[int, {{ .elementName }}] {
71+
return func(yield func(int, {{ .elementName }}) bool) {
72+
for i := 0; i < es.Len(); i++ {
73+
if !yield(i, es.At(i)) {
74+
return
75+
}
76+
}
77+
}
78+
}
79+
6580
// EnsureCapacity is an operation that ensures the slice has at least the specified capacity.
6681
// 1. If the newCap <= cap then no change in capacity.
6782
// 2. If the newCap > cap then the slice capacity will be expanded to equal newCap.

pdata/internal/cmd/pdatagen/internal/templates/slice_test.go.tmpl

+12
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,18 @@ func Test{{ .structName }}_RemoveIf(t *testing.T) {
127127
assert.Equal(t, 5, filtered.Len())
128128
}
129129

130+
func Test{{ .structName }}All(t *testing.T) {
131+
ms := generateTest{{ .structName }}()
132+
assert.NotEmpty(t, ms.Len())
133+
134+
var c int
135+
for i, v := range ms.All() {
136+
assert.Equal(t, ms.At(i), v, "element should match")
137+
c++
138+
}
139+
assert.Equal(t, ms.Len(), c, "All elements should have been visited")
140+
}
141+
130142
{{ if eq .type "sliceOfPtrs" -}}
131143
func Test{{ .structName }}_Sort(t *testing.T) {
132144
es := generateTest{{ .structName }}()

pdata/pcommon/generated_byteslice.go

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_byteslice_test.go

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_float64slice.go

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_float64slice_test.go

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_int32slice.go

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_int32slice_test.go

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_int64slice.go

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_int64slice_test.go

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_stringslice.go

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_stringslice_test.go

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_uint64slice.go

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)