Skip to content

Commit 1440a98

Browse files
authored
[pdata] Fix copying of optional fields when the source is unset (#13268)
If destination has an optional field set to some value but source has it unset, we need to unset the field on the destination instead of keeping the old value. Found this bug while working on #13267
1 parent 1ee2c3f commit 1440a98

10 files changed

+83
-9
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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: bug_fix
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: Fix copying of optional fields when the source is unset.
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [13268]
14+
15+
# Optional: The change log or logs in which this entry should be included.
16+
# e.g. '[user]' or '[user, api]'
17+
# Include 'user' if the change is relevant to end users.
18+
# Include 'api' if there is a change to a library API.
19+
# Default: '[user]'
20+
change_logs: [user]

pdata/internal/cmd/pdatagen/internal/base_fields.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,10 @@ const accessorsOptionalPrimitiveTestTemplate = `func Test{{ .structName }}_{{ .f
281281
{{- end }}
282282
ms.Remove{{ .fieldName }}()
283283
assert.False(t, ms.Has{{ .fieldName }}())
284+
dest := New{{ .structName }}()
285+
dest.Set{{ .fieldName }}({{ .testValue }})
286+
ms.CopyTo(dest)
287+
assert.False(t, dest.Has{{ .fieldName }}())
284288
}`
285289

286290
type baseField interface {
@@ -860,7 +864,9 @@ func (opv *optionalPrimitiveValue) GenerateSetWithTestValue(ms *messageValueStru
860864
}
861865

862866
func (opv *optionalPrimitiveValue) GenerateCopyOrig(ms *messageValueStruct) string {
863-
return "if src." + opv.fieldName + "_ != nil {\n" +
867+
return "if src." + opv.fieldName + "_ == nil {\n" +
868+
"\tdest." + opv.fieldName + "_ = nil\n" +
869+
"} else {\n" +
864870
"\tdest." + opv.fieldName + "_ = &" + ms.originFullName + "_" + opv.fieldName + "{" + opv.fieldName + ": src.Get" + opv.fieldName + "()}\n" +
865871
"}"
866872
}

pdata/pmetric/generated_exponentialhistogramdatapoint.go

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

pdata/pmetric/generated_exponentialhistogramdatapoint_test.go

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

pdata/pmetric/generated_histogramdatapoint.go

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

pdata/pmetric/generated_histogramdatapoint_test.go

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

pdata/pprofile/generated_location.go

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pprofile/generated_location_test.go

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

pdata/pprofile/generated_sample.go

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdata/pprofile/generated_sample_test.go

Lines changed: 4 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)