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

pdata: add iterator All method to slice and map types #12380

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .chloggen/pdata-iterator-all.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: pdata

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add iterator All method to pdata slices and map types.

# One or more tracking issues or pull requests related to the change
issues: [11982]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]
5 changes: 2 additions & 3 deletions exporter/debugexporter/internal/normal/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import (

// writeAttributes returns a slice of strings in the form "attrKey=attrValue"
func writeAttributes(attributes pcommon.Map) (attributeStrings []string) {
attributes.Range(func(k string, v pcommon.Value) bool {
for k, v := range attributes.All() {
attribute := fmt.Sprintf("%s=%s", k, v.AsString())
attributeStrings = append(attributeStrings, attribute)
return true
})
}
return attributeStrings
}
10 changes: 4 additions & 6 deletions exporter/debugexporter/internal/otlptext/databuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ func (b *dataBuffer) logAttributes(header string, m pcommon.Map) {
attrPrefix = headerParts[0] + attrPrefix
}

m.Range(func(k string, v pcommon.Value) bool {
for k, v := range m.All() {
b.logEntry("%s %s: %s", attrPrefix, k, valueToString(v))
return true
})
}
}

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

m.Range(func(k string, v pcommon.Value) bool {
for k, v := range m.All() {
b.logEntry("%s %s: %s", attrPrefix, k, valueToString(v))
return true
})
}
}

func (b *dataBuffer) logInstrumentationScope(il pcommon.InstrumentationScope) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ func (ms {{ .structName }}) At(i int) {{ .itemType }} {
return (*ms.getOrig())[i]
}

// All returns an iterator over index-value pairs in the slice.
func (ms {{ .structName }}) All() iter.Seq2[int, {{ .itemType }}] {
return func(yield func(int, {{ .itemType }}) bool) {
for i := 0; i < ms.Len(); i++ {
if !yield(i, ms.At(i)) {
return
}
}
}
}

// SetAt sets {{ .itemType }} item at particular index.
// Equivalent of {{ .lowerStructName }}[i] = val
func (ms {{ .structName }}) SetAt(i int, val {{ .itemType }}) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,16 @@ func Test{{ .structName }}EnsureCapacity(t *testing.T) {
ms.EnsureCapacity(2)
assert.Equal(t, 4, cap(*ms.getOrig()))
}

func Test{{ .structName }}All(t *testing.T) {
ms := New{{ .structName }}()
ms.FromRaw([]{{ .itemType }}{ {{ .testOrigVal }} })
assert.NotEmpty(t, ms.Len())

var c int
for i, v := range ms.All() {
assert.Equal(t, ms.At(i), v, "element should match")
c++
}
assert.Equal(t, ms.Len(), c, "All elements should have been visited")
}
15 changes: 15 additions & 0 deletions pdata/internal/cmd/pdatagen/internal/templates/slice.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ func (es {{ .structName }}) At(i int) {{ .elementName }} {
return {{ .newElement }}
}

// All returns an iterator over index-value pairs in the slice.
//
// for i, v := range es.All() {
// ... // Do something with index-value pair
// }
func (es {{ .structName }}) All() iter.Seq2[int, {{ .elementName }}] {
return func(yield func(int, {{ .elementName }}) bool) {
for i := 0; i < es.Len(); i++ {
if !yield(i, es.At(i)) {
return
}
}
}
}

// EnsureCapacity is an operation that ensures the slice has at least the specified capacity.
// 1. If the newCap <= cap then no change in capacity.
// 2. If the newCap > cap then the slice capacity will be expanded to equal newCap.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ func Test{{ .structName }}_RemoveIf(t *testing.T) {
assert.Equal(t, 5, filtered.Len())
}

func Test{{ .structName }}All(t *testing.T) {
ms := generateTest{{ .structName }}()
assert.NotEmpty(t, ms.Len())

var c int
for i, v := range ms.All() {
assert.Equal(t, ms.At(i), v, "element should match")
c++
}
assert.Equal(t, ms.Len(), c, "All elements should have been visited")
}

{{ if eq .type "sliceOfPtrs" -}}
func Test{{ .structName }}_Sort(t *testing.T) {
es := generateTest{{ .structName }}()
Expand Down
13 changes: 13 additions & 0 deletions pdata/pcommon/generated_byteslice.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions pdata/pcommon/generated_byteslice_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions pdata/pcommon/generated_float64slice.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions pdata/pcommon/generated_float64slice_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions pdata/pcommon/generated_int32slice.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions pdata/pcommon/generated_int32slice_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions pdata/pcommon/generated_int64slice.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions pdata/pcommon/generated_int64slice_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions pdata/pcommon/generated_stringslice.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions pdata/pcommon/generated_stringslice_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions pdata/pcommon/generated_uint64slice.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading