Skip to content

Commit

Permalink
Drop ForEachWhile, merge ForEach and ForEachIndex into Range
Browse files Browse the repository at this point in the history
  • Loading branch information
djaglowski committed Dec 15, 2023
1 parent 4c27ff8 commit 91fe031
Show file tree
Hide file tree
Showing 44 changed files with 221 additions and 1,365 deletions.
25 changes: 25 additions & 0 deletions .chloggen/pdata-slice-range-only.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 "Range" method to slices

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

# (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]
16 changes: 8 additions & 8 deletions exporter/internal/otlptext/databuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (b *dataBuffer) logMetricDataPoints(m pmetric.Metric) {
}

func (b *dataBuffer) logNumberDataPoints(ps pmetric.NumberDataPointSlice) {
ps.ForEachIndex(func(i int, p pmetric.NumberDataPoint) {
ps.Range(func(i int, p pmetric.NumberDataPoint) {
b.logEntry("NumberDataPoints #%d", i)
b.logDataPointAttributes(p.Attributes())

Expand All @@ -107,7 +107,7 @@ func (b *dataBuffer) logNumberDataPoints(ps pmetric.NumberDataPointSlice) {
}

func (b *dataBuffer) logHistogramDataPoints(ps pmetric.HistogramDataPointSlice) {
ps.ForEachIndex(func(i int, p pmetric.HistogramDataPoint) {
ps.Range(func(i int, p pmetric.HistogramDataPoint) {
b.logEntry("HistogramDataPoints #%d", i)
b.logDataPointAttributes(p.Attributes())

Expand Down Expand Up @@ -140,7 +140,7 @@ func (b *dataBuffer) logHistogramDataPoints(ps pmetric.HistogramDataPointSlice)
}

func (b *dataBuffer) logExponentialHistogramDataPoints(ps pmetric.ExponentialHistogramDataPointSlice) {
ps.ForEachIndex(func(i int, p pmetric.ExponentialHistogramDataPoint) {
ps.Range(func(i int, p pmetric.ExponentialHistogramDataPoint) {
b.logEntry("ExponentialHistogramDataPoints #%d", i)
b.logDataPointAttributes(p.Attributes())

Expand Down Expand Up @@ -198,7 +198,7 @@ func (b *dataBuffer) logExponentialHistogramDataPoints(ps pmetric.ExponentialHis
}

func (b *dataBuffer) logDoubleSummaryDataPoints(ps pmetric.SummaryDataPointSlice) {
ps.ForEachIndex(func(i int, p pmetric.SummaryDataPoint) {
ps.Range(func(i int, p pmetric.SummaryDataPoint) {
b.logEntry("SummaryDataPoints #%d", i)
b.logDataPointAttributes(p.Attributes())

Expand All @@ -207,7 +207,7 @@ func (b *dataBuffer) logDoubleSummaryDataPoints(ps pmetric.SummaryDataPointSlice
b.logEntry("Count: %d", p.Count())
b.logEntry("Sum: %f", p.Sum())

p.QuantileValues().ForEachIndex(func(i int, quantile pmetric.SummaryDataPointValueAtQuantile) {
p.QuantileValues().Range(func(i int, quantile pmetric.SummaryDataPointValueAtQuantile) {
b.logEntry("QuantileValue #%d: Quantile %f, Value %f", i, quantile.Quantile(), quantile.Value())
})
})
Expand All @@ -223,7 +223,7 @@ func (b *dataBuffer) logEvents(description string, se ptrace.SpanEventSlice) {
}

b.logEntry("%s:", description)
se.ForEachIndex(func(i int, el ptrace.SpanEvent) {
se.Range(func(i int, el ptrace.SpanEvent) {
e := se.At(i)
b.logEntry("SpanEvent #%d", i)
b.logEntry(" -> Name: %s", e.Name())
Expand All @@ -240,7 +240,7 @@ func (b *dataBuffer) logLinks(description string, sl ptrace.SpanLinkSlice) {

b.logEntry("%s:", description)

sl.ForEachIndex(func(i int, l ptrace.SpanLink) {
sl.Range(func(i int, l ptrace.SpanLink) {
b.logEntry("SpanLink #%d", i)
b.logEntry(" -> Trace ID: %s", l.TraceID())
b.logEntry(" -> ID: %s", l.SpanID())
Expand All @@ -257,7 +257,7 @@ func (b *dataBuffer) logExemplars(description string, se pmetric.ExemplarSlice)

b.logEntry("%s:", description)

se.ForEachIndex(func(i int, e pmetric.Exemplar) {
se.Range(func(i int, e pmetric.Exemplar) {
b.logEntry("Exemplar #%d", i)
b.logEntry(" -> Trace ID: %s", e.TraceID())
b.logEntry(" -> Span ID: %s", e.SpanID())
Expand Down
6 changes: 3 additions & 3 deletions exporter/internal/otlptext/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ type textLogsMarshaler struct{}
// MarshalLogs plog.Logs to OTLP text.
func (textLogsMarshaler) MarshalLogs(ld plog.Logs) ([]byte, error) {
buf := dataBuffer{}
ld.ResourceLogs().ForEachIndex(func(i int, rl plog.ResourceLogs) {
ld.ResourceLogs().Range(func(i int, rl plog.ResourceLogs) {
buf.logEntry("ResourceLog #%d", i)
buf.logEntry("Resource SchemaURL: %s", rl.SchemaUrl())
buf.logAttributes("Resource attributes", rl.Resource().Attributes())
rl.ScopeLogs().ForEachIndex(func(j int, ils plog.ScopeLogs) {
rl.ScopeLogs().Range(func(j int, ils plog.ScopeLogs) {
buf.logEntry("ScopeLogs #%d", j)
buf.logEntry("ScopeLogs SchemaURL: %s", ils.SchemaUrl())
buf.logInstrumentationScope(ils.Scope())
ils.LogRecords().ForEachIndex(func(k int, lr plog.LogRecord) {
ils.LogRecords().Range(func(k int, lr plog.LogRecord) {
buf.logEntry("LogRecord #%d", k)
buf.logEntry("ObservedTimestamp: %s", lr.ObservedTimestamp())
buf.logEntry("Timestamp: %s", lr.Timestamp())
Expand Down
6 changes: 3 additions & 3 deletions exporter/internal/otlptext/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ type textMetricsMarshaler struct{}
// MarshalMetrics pmetric.Metrics to OTLP text.
func (textMetricsMarshaler) MarshalMetrics(md pmetric.Metrics) ([]byte, error) {
buf := dataBuffer{}
md.ResourceMetrics().ForEachIndex(func(i int, rm pmetric.ResourceMetrics) {
md.ResourceMetrics().Range(func(i int, rm pmetric.ResourceMetrics) {
buf.logEntry("ResourceMetrics #%d", i)
buf.logEntry("Resource SchemaURL: %s", rm.SchemaUrl())
buf.logAttributes("Resource attributes", rm.Resource().Attributes())
rm.ScopeMetrics().ForEachIndex(func(j int, ilm pmetric.ScopeMetrics) {
rm.ScopeMetrics().Range(func(j int, ilm pmetric.ScopeMetrics) {
buf.logEntry("ScopeMetrics #%d", j)
buf.logEntry("ScopeMetrics SchemaURL: %s", ilm.SchemaUrl())
buf.logInstrumentationScope(ilm.Scope())
ilm.Metrics().ForEachIndex(func(k int, metric pmetric.Metric) {
ilm.Metrics().Range(func(k int, metric pmetric.Metric) {
buf.logEntry("Metric #%d", k)
buf.logMetricDescriptor(metric)
buf.logMetricDataPoints(metric)
Expand Down
6 changes: 3 additions & 3 deletions exporter/internal/otlptext/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ type textTracesMarshaler struct{}
// MarshalTraces ptrace.Traces to OTLP text.
func (textTracesMarshaler) MarshalTraces(td ptrace.Traces) ([]byte, error) {
buf := dataBuffer{}
td.ResourceSpans().ForEachIndex(func(i int, rs ptrace.ResourceSpans) {
td.ResourceSpans().Range(func(i int, rs ptrace.ResourceSpans) {
buf.logEntry("ResourceSpans #%d", i)
buf.logEntry("Resource SchemaURL: %s", rs.SchemaUrl())
buf.logAttributes("Resource attributes", rs.Resource().Attributes())
rs.ScopeSpans().ForEachIndex(func(j int, ils ptrace.ScopeSpans) {
rs.ScopeSpans().Range(func(j int, ils ptrace.ScopeSpans) {
buf.logEntry("ScopeSpans #%d", j)
buf.logEntry("ScopeSpans SchemaURL: %s", ils.SchemaUrl())
buf.logInstrumentationScope(ils.Scope())
ils.Spans().ForEachIndex(func(k int, span ptrace.Span) {
ils.Spans().Range(func(k int, span ptrace.Span) {
buf.logEntry("Span #%d", k)
buf.logAttr("Trace ID", span.TraceID())
buf.logAttr("Parent ID", span.ParentSpanID())
Expand Down
78 changes: 7 additions & 71 deletions pdata/internal/cmd/pdatagen/internal/base_slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,30 +158,13 @@ func (es {{ .structName }}) Sort(less func(a, b {{ .elementName }}) bool) {
}
{{- end }}
// ForEach iterates over {{ .structName }} and executes f against each element.
func (es {{ .structName }}) ForEach(f func({{ .elementName }})) {
for i := 0; i < es.Len(); i++ {
f(es.At(i))
}
}
// ForEachIndex iterates over {{ .structName }} and executes f against each element.
// Range iterates over {{ .structName }} and executes f against each element.
// The function also passes the iteration index.
func (es {{ .structName }}) ForEachIndex(f func(int, {{ .elementName }})) {
func (es {{ .structName }}) Range(f func(int, {{ .elementName }})) {
for i := 0; i < es.Len(); i++ {
f(i, es.At(i))
}
}
// ForEachWhile iterates over {{ .structName }} and executes f against each element.
// The function also passes the iteration index.
func (es {{ .structName }}) ForEachWhile(f func({{ .elementName }}) bool) bool {
for i := 0; i < es.Len(); i++ {
if !f(es.At(i)) {
return false
}
}
return true
}`

const sliceTestTemplate = `func Test{{ .structName }}(t *testing.T) {
Expand Down Expand Up @@ -309,67 +292,20 @@ func Test{{ .structName }}_Sort(t *testing.T) {
}
{{- end }}
func Test{{ .structName }}_ForEach(t *testing.T) {
// Test ForEach on empty slice
emptySlice := New{{ .structName }}()
emptySlice.ForEach(func(el {{ .elementName }}) {
t.Fail()
})
// Test ForEach
slice := generateTest{{ .structName }}()
count := 0
slice.ForEach(func(el {{ .elementName }}) {
count++
})
assert.Equal(t, 7, count)
}
func Test{{ .structName }}_ForEachIndex(t *testing.T) {
// Test _ForEachIndex on empty slice
func Test{{ .structName }}_Range(t *testing.T) {
// Test _Range on empty slice
emptySlice := New{{ .structName }}()
emptySlice.ForEachIndex(func(i int, el {{ .elementName }}) {
emptySlice.Range(func(i int, el {{ .elementName }}) {
t.Fail()
})
// Test _ForEachIndex
// Test _Range
slice := generateTest{{ .structName }}()
total := 0
slice.ForEachIndex(func(i int, el {{ .elementName }}) {
slice.Range(func(i int, el {{ .elementName }}) {
total+=i
})
assert.Equal(t, 0+1+2+3+4+5+6, total)
}
func Test{{ .structName }}_ForEachWhile(t *testing.T) {
// Test ForEach on empty slice
emptySlice := New{{ .structName }}()
emptySlice.ForEachWhile(func(el {{ .elementName }}) bool {
t.Fail()
return false
})
// Test ForEachWhile stops short
slice := generateTest{{ .structName }}()
last := 0
proceed := slice.ForEachWhile(func(el {{ .elementName }}) bool{
last++
if last == 4 {
return false
}
return true
})
assert.False(t, proceed)
assert.Equal(t, 4, last)
// Test ForEachWhile completes
last = 0
proceed = slice.ForEachWhile(func(el {{ .elementName }}) bool{
last++
return true
})
assert.True(t, proceed)
assert.Equal(t, 7, last)
}`

const sliceGenerateTest = `func generateTest{{ .structName }}() {{ .structName }} {
Expand Down
22 changes: 2 additions & 20 deletions pdata/plog/generated_logrecordslice.go

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

57 changes: 5 additions & 52 deletions pdata/plog/generated_logrecordslice_test.go

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

22 changes: 2 additions & 20 deletions pdata/plog/generated_resourcelogsslice.go

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

Loading

0 comments on commit 91fe031

Please sign in to comment.