Files
Mahad ZaryabandGitHub d7ca046182 Fix Helpers For Marshalling And Unmarshalling pcommon.Value (#13945)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description
- This PR fixes the helpers for AnyValue by changing the API to accept
`pcommon.Value`. Since `otlpcommon.AnyValue` is an internal type, the
current helpers are not useful as a public API.

<!-- Issue number if applicable -->
#### Link to tracking issue
Fixes #13837 (original issue #12826)

<!--Describe what testing was performed and which tests were added.-->
#### Testing
- Updated unit tests

---------

Signed-off-by: Mahad Zaryab <[email protected]>
2025-10-13 16:26:18 +00:00

90 lines
2.4 KiB
Go

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package xpdata
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/pcommon"
)
func TestMarshalAndUnmarshalValue(t *testing.T) {
for name, src := range genTestEncodingValues() {
t.Run(name, func(t *testing.T) {
m := &JSONMarshaler{}
b, err := m.MarshalValue(src)
require.NoError(t, err)
u := &JSONUnmarshaler{}
dest, err := u.UnmarshalValue(b)
require.NoError(t, err)
require.Equal(t, src, dest)
})
}
}
func TestUnmarshalValueUnknown(t *testing.T) {
m := &JSONUnmarshaler{}
b, err := m.UnmarshalValue([]byte(`{"unknown": "string"}`))
require.NoError(t, err)
assert.Equal(t, pcommon.NewValueEmpty(), b)
}
func genTestEncodingValues() map[string]pcommon.Value {
return map[string]pcommon.Value{
"empty": pcommon.NewValueEmpty(),
"StringValue/default": pcommon.NewValueStr(""),
"StringValue/test": pcommon.NewValueStr("test_stringvalue"),
"BoolValue/default": pcommon.NewValueBool(false),
"BoolValue/test": pcommon.NewValueBool(true),
"IntValue/default": pcommon.NewValueInt(0),
"IntValue/test": pcommon.NewValueInt(13),
"DoubleValue/default": pcommon.NewValueDouble(0),
"DoubleValue/test": pcommon.NewValueDouble(3.1415926),
"ArrayValue/default": pcommon.NewValueSlice(),
"ArrayValue/test": func() pcommon.Value {
s := pcommon.NewValueSlice()
s.Slice().AppendEmpty().SetStr("test1")
s.Slice().AppendEmpty().SetInt(13)
s.Slice().AppendEmpty().SetBool(true)
s.Slice().AppendEmpty().SetDouble(3.1415926)
s1 := s.Slice().AppendEmpty().SetEmptySlice()
s1.AppendEmpty().SetStr("nested")
m := s.Slice().AppendEmpty().SetEmptyMap()
m.PutStr("key1", "value1")
return s
}(),
"KvlistValue/default": pcommon.NewValueMap(),
"KvlistValue/test": func() pcommon.Value {
m := pcommon.NewValueMap()
m.Map().PutStr("key1", "value1")
m.Map().PutInt("key2", 13)
m.Map().PutBool("key3", true)
m.Map().PutDouble("key4", 3.1415926)
m.Map().PutEmpty("key5").SetStr("value5")
s := m.Map().PutEmptySlice("key6")
s.AppendEmpty().SetStr("nested1")
m1 := m.Map().PutEmptyMap("key6")
m1.PutStr("nestedKey1", "nestedValue1")
return m
}(),
"BytesValue/test": func() pcommon.Value {
v := pcommon.NewValueBytes()
v.Bytes().FromRaw([]byte{1, 2, 3})
return v
}(),
}
}