Skip to content

Commit e1f6708

Browse files
authored
Add MoveAndAppendTo to pdata primitive slices (open-telemetry#13074)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description pdata primitive slices are missing `MoveAndAppendTo`, and we're going to start needing it with the profiles proto upgrade (which has a dictionary object with primitive type slices that we will need to merge at some point)
1 parent 605011a commit e1f6708

15 files changed

+257
-0
lines changed
Lines changed: 25 additions & 0 deletions
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: Introduce `MoveAndAppendTo` methods to the generated primitive slices
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [13074]
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]

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,20 @@ func (ms {{ .structName }}) MoveTo(dest {{ .structName }}) {
113113
*ms.getOrig() = nil
114114
}
115115

116+
// MoveAndAppendTo moves all elements from the current slice and appends them to the dest.
117+
// The current slice will be cleared.
118+
func (ms {{ .structName }}) MoveAndAppendTo(dest {{ .structName }}) {
119+
ms.getState().AssertMutable()
120+
dest.getState().AssertMutable()
121+
if *dest.getOrig() == nil {
122+
// We can simply move the entire vector and avoid any allocations.
123+
*dest.getOrig() = *ms.getOrig()
124+
} else {
125+
*dest.getOrig() = append(*dest.getOrig(), *ms.getOrig()...)
126+
}
127+
*ms.getOrig() = nil
128+
}
129+
116130
// CopyTo copies all elements from the current slice overriding the destination.
117131
func (ms {{ .structName }}) CopyTo(dest {{ .structName }}) {
118132
dest.getState().AssertMutable()

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,26 @@ func Test{{ .structName }}All(t *testing.T) {
132132
assert.Equal(t, ms.Len(), c, "All elements should have been visited")
133133
}
134134

135+
136+
func Test{{ .structName }}MoveAndAppendTo(t *testing.T) {
137+
// Test moving from an empty slice
138+
ms := New{{ .structName }}()
139+
ms2 := New{{ .structName }}()
140+
ms.MoveAndAppendTo(ms2)
141+
assert.Equal(t, New{{ .structName }}(), ms2)
142+
assert.Equal(t, ms.Len(), 0)
143+
144+
// Test moving to empty slice
145+
ms.FromRaw([]{{ .itemType }}{ {{ .testOrigVal }} })
146+
ms.MoveAndAppendTo(ms2)
147+
assert.Equal(t, ms2.Len(), 3)
148+
149+
// Test moving to a non empty slice
150+
ms.FromRaw([]{{ .itemType }}{ {{ .testOrigVal }} })
151+
ms.MoveAndAppendTo(ms2)
152+
assert.Equal(t, ms2.Len(), 6)
153+
}
154+
135155
func Test{{ .structName }}Equal(t *testing.T) {
136156
ms := New{{ .structName }}()
137157
ms2 := New{{ .structName }}()

pdata/pcommon/generated_byteslice.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_byteslice_test.go

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_float64slice.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_float64slice_test.go

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_int32slice.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_int32slice_test.go

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pcommon/generated_int64slice.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)