Skip to content

Commit 9a54388

Browse files
authored
Add MoveTo to pcommon.Value, only type missing this (open-telemetry#12877)
Signed-off-by: Bogdan Drutu <[email protected]>
1 parent 4610126 commit 9a54388

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

.chloggen/value_move.yaml

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: Add MoveTo to pcommon.Value, only type missing this
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [12877]
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/pcommon/value.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,16 @@ func (v Value) SetEmptySlice() Slice {
318318
return newSlice(&av.ArrayValue.Values, v.getState())
319319
}
320320

321+
// MoveTo moves the Value from current overriding the destination and
322+
// resetting the current instance to empty value.
323+
// Calling this function on zero-initialized Value will cause a panic.
324+
func (v Value) MoveTo(dest Value) {
325+
v.getState().AssertMutable()
326+
dest.getState().AssertMutable()
327+
*dest.getOrig() = *v.getOrig()
328+
v.getOrig().Value = nil
329+
}
330+
321331
// CopyTo copies the Value instance overriding the destination.
322332
// Calling this function on zero-initialized Value will cause a panic.
323333
func (v Value) CopyTo(dest Value) {

pdata/pcommon/value_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,21 @@ func TestNilOrigSetValue(t *testing.T) {
231231
assert.Equal(t, []any{int64(1), "val"}, av.Slice().AsRaw())
232232
}
233233

234+
func TestValue_MoveTo(t *testing.T) {
235+
src := NewValueMap()
236+
src.Map().PutStr("key", "value")
237+
238+
dest := NewValueEmpty()
239+
assert.True(t, dest.Equal(NewValueEmpty()))
240+
241+
src.MoveTo(dest)
242+
assert.True(t, src.Equal(NewValueEmpty()))
243+
244+
expected := NewValueMap()
245+
expected.Map().PutStr("key", "value")
246+
assert.True(t, dest.Equal(expected))
247+
}
248+
234249
func TestValue_CopyTo(t *testing.T) {
235250
state := internal.StateMutable
236251

0 commit comments

Comments
 (0)