From 3ca4cafbb67ba143613faee3f6d2a07700359101 Mon Sep 17 00:00:00 2001 From: Peter Deng Date: Fri, 16 Aug 2024 03:28:34 +0800 Subject: [PATCH] fix nil value conversion (#34673) **Description:** fix nil value conversion **Link to tracking Issue:** Fixes #34672 **Testing:** added. **Documentation:** n/a --- .chloggen/pkg-stanza-nil-handling.yaml | 27 ++++++++++++++++++++++++++ pkg/stanza/adapter/converter.go | 1 + pkg/stanza/adapter/converter_test.go | 7 ++++++- 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 .chloggen/pkg-stanza-nil-handling.yaml diff --git a/.chloggen/pkg-stanza-nil-handling.yaml b/.chloggen/pkg-stanza-nil-handling.yaml new file mode 100644 index 0000000000000..77d9b87a480e8 --- /dev/null +++ b/.chloggen/pkg-stanza-nil-handling.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: pkg/stanza + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: fix nil value conversion + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [34672] + +# (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: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# 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: [] diff --git a/pkg/stanza/adapter/converter.go b/pkg/stanza/adapter/converter.go index de7c259d648d3..1a8aed3f8e90d 100644 --- a/pkg/stanza/adapter/converter.go +++ b/pkg/stanza/adapter/converter.go @@ -322,6 +322,7 @@ func upsertToAttributeVal(value any, dest pcommon.Value) { upsertToMap(t, dest.SetEmptyMap()) case []any: upsertToSlice(t, dest.SetEmptySlice()) + case nil: default: dest.SetStr(fmt.Sprintf("%v", t)) } diff --git a/pkg/stanza/adapter/converter_test.go b/pkg/stanza/adapter/converter_test.go index 586340c882282..8092cb6775920 100644 --- a/pkg/stanza/adapter/converter_test.go +++ b/pkg/stanza/adapter/converter_test.go @@ -566,6 +566,7 @@ func TestConvertMetadata(t *testing.T) { "int": 123, "double": 12.34, "string": "hello", + "empty": nil, }, } e.Body = true @@ -595,7 +596,7 @@ func TestConvertMetadata(t *testing.T) { require.True(t, ok) mapVal := attVal.Map() - require.Equal(t, 4, mapVal.Len()) + require.Equal(t, 5, mapVal.Len()) attVal, ok = mapVal.Get("bool") require.True(t, ok) @@ -613,6 +614,10 @@ func TestConvertMetadata(t *testing.T) { require.True(t, ok) require.Equal(t, "hello", attVal.Str()) + attVal, ok = mapVal.Get("empty") + require.True(t, ok) + require.Equal(t, pcommon.ValueTypeEmpty, attVal.Type()) + bod := result.Body() require.Equal(t, pcommon.ValueTypeBool, bod.Type()) require.True(t, bod.Bool())