diff --git a/CHANGELOG.md b/CHANGELOG.md index 578a8b4ac9bf..b5097b50625f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,16 @@ - `processorhelper.New[Traces|Metrics|Logs]ProcessorWithCreateSettings` - `component.NewExtensionFactoryWithStabilityLevel` +### 🚩 Deprecations 🚩 + +- Primitive slice wrapper are now mutable (#5971): + - `pcommon.ImmutableByteSlice` is deprecated in favor of `pcommon.ByteSlice` + - `pcommon.ImmutableFloat64Slice` is deprecated in favor of `pcommon.Float64Slice` + - `pcommon.ImmutableUInt64Slice` is deprecated in favor of `pcommon.UInt64Slice` + - Temporarily deprecate `pcommon.NewValueBytes` that will be replaced with `pcommon.NewValueBytesEmpty` in 0.60.0 + - Deprecate `pcommon.Map.UpsertBytes` in favor of `pcommon.Map.UpsertEmptyBytes` + - Deprecate `pcommon.Value.SetBytesVal` in favor of `pcommon.Value.SetEmptyBytesVal` + ## v0.59.0 Beta ### 🛑 Breaking changes 🛑 diff --git a/internal/testdata/metric.go b/internal/testdata/metric.go index 42589205cbb5..0e29b45bc5a2 100644 --- a/internal/testdata/metric.go +++ b/internal/testdata/metric.go @@ -197,12 +197,12 @@ func initHistogramMetric(hm pmetric.Metric) { hdp1.SetSum(15) hdp1.SetMin(15) hdp1.SetMax(15) - hdp1.SetBucketCounts(pcommon.NewImmutableUInt64Slice([]uint64{0, 1})) + hdp1.BucketCounts().CopyFromRaw([]uint64{0, 1}) exemplar := hdp1.Exemplars().AppendEmpty() exemplar.SetTimestamp(metricExemplarTimestamp) exemplar.SetDoubleVal(15) initMetricExemplarAttributes(exemplar.FilteredAttributes()) - hdp1.SetExplicitBounds(pcommon.NewImmutableFloat64Slice([]float64{1})) + hdp1.ExplicitBounds().CopyFromRaw([]float64{1}) } func initExponentialHistogramMetric(hm pmetric.Metric) { @@ -220,10 +220,10 @@ func initExponentialHistogramMetric(hm pmetric.Metric) { // positive index 1 and 2 are values sqrt(2), 2 at scale 1 hdp0.Positive().SetOffset(1) - hdp0.Positive().SetBucketCounts(pcommon.NewImmutableUInt64Slice([]uint64{1, 1})) + hdp0.Positive().BucketCounts().CopyFromRaw([]uint64{1, 1}) // negative index -1 and 0 are values -1/sqrt(2), -1 at scale 1 hdp0.Negative().SetOffset(-1) - hdp0.Negative().SetBucketCounts(pcommon.NewImmutableUInt64Slice([]uint64{1, 1})) + hdp0.Negative().BucketCounts().CopyFromRaw([]uint64{1, 1}) // The above will print: // Bucket (-1.414214, -1.000000], Count: 1 @@ -245,7 +245,7 @@ func initExponentialHistogramMetric(hm pmetric.Metric) { // index -1 and 0 are values 0.25, 1 at scale -1 hdp1.Positive().SetOffset(-1) - hdp1.Positive().SetBucketCounts(pcommon.NewImmutableUInt64Slice([]uint64{1, 1})) + hdp1.Positive().BucketCounts().CopyFromRaw([]uint64{1, 1}) // The above will print: // Bucket [0, 0], Count: 1 diff --git a/pdata/internal/cmd/pdatagen/internal/base_fields.go b/pdata/internal/cmd/pdatagen/internal/base_fields.go index 14ea6cc33b6e..595702adde51 100644 --- a/pdata/internal/cmd/pdatagen/internal/base_fields.go +++ b/pdata/internal/cmd/pdatagen/internal/base_fields.go @@ -62,12 +62,20 @@ const copyToPrimitiveSliceTestTemplate = ` if len(ms.getOrig().${originFieldName const accessorsPrimitiveSliceTemplate = `// ${fieldName} returns the ${lowerFieldName} associated with this ${structName}. func (ms ${structName}) ${fieldName}() ${packageName}${returnType} { - return ${packageName}${returnType}(internal.New${returnType}(ms.getOrig().${originFieldName})) + return ${packageName}${returnType}(internal.New${returnType}(&ms.getOrig().${originFieldName})) } // Set${fieldName} replaces the ${lowerFieldName} associated with this ${structName}. +// Deprecated: [0.59.0] Use ${fieldName}().CopyFromRaw() instead func (ms ${structName}) Set${fieldName}(v ${packageName}${returnType}) { - ms.getOrig().${originFieldName} = internal.GetOrig${returnType}(internal.${returnType}(v)) + ms.getOrig().${originFieldName} = *internal.GetOrig${returnType}(internal.${returnType}(v)) +}` + +const accessorsPrimitiveSliceTestTemplate = `func Test${structName}_${fieldName}(t *testing.T) { + ms := New${structName}() + assert.Equal(t, ${defaultVal}, ms.${fieldName}().AsRaw()) + ms.${fieldName}().CopyFromRaw(${testValue}) + assert.Equal(t, ${testValue}, ms.${fieldName}().AsRaw()) }` const oneOfTypeAccessorHeaderTemplate = `// ${originFieldName}Type returns the type of the ${lowerOriginFieldName} for this ${structName}. @@ -558,7 +566,7 @@ func (psf *primitiveSliceField) generateAccessors(ms baseStruct, sb *strings.Bui } func (psf *primitiveSliceField) generateAccessorsTest(ms baseStruct, sb *strings.Builder) { - sb.WriteString(os.Expand(accessorsPrimitiveStructTestTemplate, func(name string) string { + sb.WriteString(os.Expand(accessorsPrimitiveSliceTestTemplate, func(name string) string { switch name { case "structName": return ms.getName() diff --git a/pdata/internal/cmd/pdatagen/internal/files.go b/pdata/internal/cmd/pdatagen/internal/files.go index 0e35e27afbec..a66f2f719106 100644 --- a/pdata/internal/cmd/pdatagen/internal/files.go +++ b/pdata/internal/cmd/pdatagen/internal/files.go @@ -42,7 +42,7 @@ var AllFiles = []*File{ commonFile, metricsFile, resourceFile, - immutableSliceFile, + primitiveSliceFile, traceFile, logFile, } diff --git a/pdata/internal/cmd/pdatagen/internal/immutable_slice_structs.go b/pdata/internal/cmd/pdatagen/internal/immutable_slice_structs.go deleted file mode 100644 index e4aa378c81cb..000000000000 --- a/pdata/internal/cmd/pdatagen/internal/immutable_slice_structs.go +++ /dev/null @@ -1,199 +0,0 @@ -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package internal // import "go.opentelemetry.io/collector/pdata/internal/cmd/pdatagen/internal" - -import ( - "os" - "strings" -) - -const immutableSliceTemplate = `// ${structName} represents a []${itemType} slice that cannot be mutated. -// The instance of ${structName} can be assigned to multiple objects since it's immutable. -type ${structName} internal.${structName} - -func (ms ${structName}) getOrig() []${itemType} { - return internal.GetOrig${structName}(internal.${structName}(ms)) -} - -// New${structName} creates a new ${structName} by copying the provided []${itemType} slice. -func New${structName}(orig []${itemType}) ${structName} { - if len(orig) == 0 { - return ${structName}(internal.New${structName}(nil)) - } - copyOrig := make([]${itemType}, len(orig)) - copy(copyOrig, orig) - return ${structName}(internal.New${structName}(copyOrig)) -} - -// AsRaw returns a copy of the []${itemType} slice. -func (ms ${structName}) AsRaw() []${itemType} { - orig := ms.getOrig() - if len(orig) == 0 { - return nil - } - copyOrig := make([]${itemType}, len(orig)) - copy(copyOrig, orig) - return copyOrig -} - -// Len returns length of the []${itemType} slice value. -func (ms ${structName}) Len() int { - return len(ms.getOrig()) -} - -// At returns an item from particular index. -func (ms ${structName}) At(i int) ${itemType} { - return ms.getOrig()[i] -}` - -const immutableSliceTestTemplate = `func TestNew${structName}(t *testing.T) { - tests := []struct { - name string - orig []${itemType} - want []${itemType} - }{ - { - name: "nil", - orig: nil, - want: nil, - }, - { - name: "empty", - orig: []${itemType}{}, - want: nil, - }, - { - name: "copy", - orig: []${itemType}{1, 2, 3}, - want: []${itemType}{1, 2, 3}, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - s := New${structName}(tt.orig) - assert.Equal(t, tt.want, s.AsRaw()) - assert.Equal(t, len(s.getOrig()), s.Len()) - if len(tt.orig) > 0 { - // verify that orig mutation doesn't have any effect - tt.orig[0] = ${itemType}(0) - assert.Equal(t, ${itemType}(1), s.At(0)) - } - }) - } -}` - -const immutableSliceInternalTemplate = ` -type ${structName} struct { - orig []${itemType} -} - -func GetOrig${structName}(ms ${structName}) []${itemType} { - return ms.orig -} - -func New${structName}(orig []${itemType}) ${structName} { - return ${structName}{orig: orig} -}` - -type immutableSliceStruct struct { - structName string - packageName string - itemType string -} - -func (iss *immutableSliceStruct) getName() string { - return iss.structName -} - -func (iss *immutableSliceStruct) getPackageName() string { - return iss.packageName -} - -func (iss *immutableSliceStruct) generateStruct(sb *strings.Builder) { - sb.WriteString(os.Expand(immutableSliceTemplate, func(name string) string { - switch name { - case "structName": - return iss.structName - case "itemType": - return iss.itemType - default: - panic(name) - } - })) -} - -func (iss *immutableSliceStruct) generateTests(sb *strings.Builder) { - sb.WriteString(os.Expand(immutableSliceTestTemplate, func(name string) string { - switch name { - case "structName": - return iss.structName - case "itemType": - return iss.itemType - default: - panic(name) - } - })) -} - -func (iss *immutableSliceStruct) generateTestValueHelpers(*strings.Builder) {} - -func (iss *immutableSliceStruct) generateInternal(sb *strings.Builder) { - sb.WriteString(os.Expand(immutableSliceInternalTemplate, func(name string) string { - switch name { - case "structName": - return iss.structName - case "itemType": - return iss.itemType - default: - panic(name) - } - })) -} - -var immutableSliceFile = &File{ - Name: "immutable_slice", - PackageName: "pcommon", - testImports: []string{ - `"testing"`, - ``, - `"github.com/stretchr/testify/assert"`, - ``, - `"go.opentelemetry.io/collector/pdata/internal"`, - }, - structs: []baseStruct{ - immutableByteSliceStruct, - immutableFloat64SliceStruct, - immutableUInt64SliceStruct, - }, -} - -var immutableByteSliceStruct = &immutableSliceStruct{ - structName: "ImmutableByteSlice", - packageName: "pcommon", - itemType: "byte", -} - -var immutableFloat64SliceStruct = &immutableSliceStruct{ - structName: "ImmutableFloat64Slice", - packageName: "pcommon", - itemType: "float64", -} - -var immutableUInt64SliceStruct = &immutableSliceStruct{ - structName: "ImmutableUInt64Slice", - packageName: "pcommon", - itemType: "uint64", -} diff --git a/pdata/internal/cmd/pdatagen/internal/metrics_structs.go b/pdata/internal/cmd/pdatagen/internal/metrics_structs.go index c114addc49bd..12baf81f56fe 100644 --- a/pdata/internal/cmd/pdatagen/internal/metrics_structs.go +++ b/pdata/internal/cmd/pdatagen/internal/metrics_structs.go @@ -534,7 +534,7 @@ var valueFloat64Field = &primitiveField{ var bucketCountsField = &primitiveSliceField{ fieldName: "BucketCounts", originFieldName: "BucketCounts", - returnType: "ImmutableUInt64Slice", + returnType: "UInt64Slice", returnPackageName: "pcommon", defaultVal: "[]uint64(nil)", rawType: "[]uint64", @@ -544,7 +544,7 @@ var bucketCountsField = &primitiveSliceField{ var explicitBoundsField = &primitiveSliceField{ fieldName: "ExplicitBounds", originFieldName: "ExplicitBounds", - returnType: "ImmutableFloat64Slice", + returnType: "Float64Slice", returnPackageName: "pcommon", defaultVal: "[]float64(nil)", rawType: "[]float64", diff --git a/pdata/internal/cmd/pdatagen/internal/primitive_slice_structs.go b/pdata/internal/cmd/pdatagen/internal/primitive_slice_structs.go new file mode 100644 index 000000000000..d6f46a4b3539 --- /dev/null +++ b/pdata/internal/cmd/pdatagen/internal/primitive_slice_structs.go @@ -0,0 +1,195 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package internal // import "go.opentelemetry.io/collector/pdata/internal/cmd/pdatagen/internal" + +import ( + "os" + "strings" +) + +const primitiveSliceTemplate = `// ${structName} represents a []${itemType} slice. +// The instance of ${structName} can be assigned to multiple objects since it's immutable. +// +// Must use New${structName} function to create new instances. +// Important: zero-initialized instance is not valid for use. +type ${structName} internal.${structName} + +// Deprecated: [0.59.0] Use ${structName} instead. +type Immutable${structName} = ${structName} + +func (ms ${structName}) getOrig() *[]${itemType} { + return internal.GetOrig${structName}(internal.${structName}(ms)) +} + +// New${structName} creates a new empty ${structName}. +func New${structName}() ${structName} { + orig := []${itemType}(nil) + return ${structName}(internal.New${structName}(&orig)) +} + +// NewImmutable${structName} creates a new ${structName} by copying the provided []${itemType} slice. +// Deprecated: [0.59.0] Use New{structName}() and {structName}.CopyFromRaw([]${itemType}) instead +func NewImmutable${structName}(orig []${itemType}) ${structName} { + return ${structName}(internal.New${structName}(&orig)) +} + +// AsRaw returns a copy of the []${itemType} slice. +func (ms ${structName}) AsRaw() []${itemType} { + orig := *ms.getOrig() + if len(orig) == 0 { + return nil + } + copyOrig := make([]${itemType}, len(orig)) + copy(copyOrig, orig) + return copyOrig +} + +// CopyFromRaw copies raw []${itemType} into the slice ${structName}. +func (ms ${structName}) CopyFromRaw(val []${itemType}) { + copyVal := make([]${itemType}, len(val)) + copy(copyVal, val) + *ms.getOrig() = copyVal +} + +// Len returns length of the []${itemType} slice value. +func (ms ${structName}) Len() int { + return len(*ms.getOrig()) +} + +// At returns an item from particular index. +func (ms ${structName}) At(i int) ${itemType} { + return (*ms.getOrig())[i] +} + +// SetAt sets ${itemType} item at particular index. +func (ms ${structName}) SetAt(i int, val ${itemType}) { + (*ms.getOrig())[i] = val +}` + +const immutableSliceTestTemplate = `func TestNew${structName}(t *testing.T) { + ms := New${structName}() + assert.Equal(t, 0, ms.Len()) + ms.CopyFromRaw([]${itemType}{1, 2, 3}) + assert.Equal(t, 3, ms.Len()) + assert.Equal(t, []${itemType}{1, 2, 3}, ms.AsRaw()) + ms.SetAt(1, ${itemType}(5)) + assert.Equal(t, []${itemType}{1, 5, 3}, ms.AsRaw()) + ms.CopyFromRaw([]${itemType}{3}) + assert.Equal(t, 1, ms.Len()) + assert.Equal(t, ${itemType}(3), ms.At(0)) +}` + +const primitiveSliceInternalTemplate = ` +type ${structName} struct { + orig *[]${itemType} +} + +func GetOrig${structName}(ms ${structName}) *[]${itemType} { + return ms.orig +} + +func New${structName}(orig *[]${itemType}) ${structName} { + return ${structName}{orig: orig} +}` + +type primitiveSliceStruct struct { + structName string + packageName string + itemType string +} + +func (iss *primitiveSliceStruct) getName() string { + return iss.structName +} + +func (iss *primitiveSliceStruct) getPackageName() string { + return iss.packageName +} + +func (iss *primitiveSliceStruct) generateStruct(sb *strings.Builder) { + sb.WriteString(os.Expand(primitiveSliceTemplate, func(name string) string { + switch name { + case "structName": + return iss.structName + case "itemType": + return iss.itemType + default: + panic(name) + } + })) +} + +func (iss *primitiveSliceStruct) generateTests(sb *strings.Builder) { + sb.WriteString(os.Expand(immutableSliceTestTemplate, func(name string) string { + switch name { + case "structName": + return iss.structName + case "itemType": + return iss.itemType + default: + panic(name) + } + })) +} + +func (iss *primitiveSliceStruct) generateTestValueHelpers(*strings.Builder) {} + +func (iss *primitiveSliceStruct) generateInternal(sb *strings.Builder) { + sb.WriteString(os.Expand(primitiveSliceInternalTemplate, func(name string) string { + switch name { + case "structName": + return iss.structName + case "itemType": + return iss.itemType + default: + panic(name) + } + })) +} + +var primitiveSliceFile = &File{ + Name: "primitive_slice", + PackageName: "pcommon", + testImports: []string{ + `"testing"`, + ``, + `"github.com/stretchr/testify/assert"`, + ``, + `"go.opentelemetry.io/collector/pdata/internal"`, + }, + structs: []baseStruct{ + byteSliceStruct, + float64SliceStruct, + uInt64SliceStruct, + }, +} + +var byteSliceStruct = &primitiveSliceStruct{ + structName: "ByteSlice", + packageName: "pcommon", + itemType: "byte", +} + +var float64SliceStruct = &primitiveSliceStruct{ + structName: "Float64Slice", + packageName: "pcommon", + itemType: "float64", +} + +var uInt64SliceStruct = &primitiveSliceStruct{ + structName: "UInt64Slice", + packageName: "pcommon", + itemType: "uint64", +} diff --git a/pdata/internal/generated_wrapper_immutable_slice.go b/pdata/internal/generated_wrapper_primitive_slice.go similarity index 55% rename from pdata/internal/generated_wrapper_immutable_slice.go rename to pdata/internal/generated_wrapper_primitive_slice.go index d898f0722f6b..7274a94c7641 100644 --- a/pdata/internal/generated_wrapper_immutable_slice.go +++ b/pdata/internal/generated_wrapper_primitive_slice.go @@ -17,38 +17,38 @@ package internal -type ImmutableByteSlice struct { - orig []byte +type ByteSlice struct { + orig *[]byte } -func GetOrigImmutableByteSlice(ms ImmutableByteSlice) []byte { +func GetOrigByteSlice(ms ByteSlice) *[]byte { return ms.orig } -func NewImmutableByteSlice(orig []byte) ImmutableByteSlice { - return ImmutableByteSlice{orig: orig} +func NewByteSlice(orig *[]byte) ByteSlice { + return ByteSlice{orig: orig} } -type ImmutableFloat64Slice struct { - orig []float64 +type Float64Slice struct { + orig *[]float64 } -func GetOrigImmutableFloat64Slice(ms ImmutableFloat64Slice) []float64 { +func GetOrigFloat64Slice(ms Float64Slice) *[]float64 { return ms.orig } -func NewImmutableFloat64Slice(orig []float64) ImmutableFloat64Slice { - return ImmutableFloat64Slice{orig: orig} +func NewFloat64Slice(orig *[]float64) Float64Slice { + return Float64Slice{orig: orig} } -type ImmutableUInt64Slice struct { - orig []uint64 +type UInt64Slice struct { + orig *[]uint64 } -func GetOrigImmutableUInt64Slice(ms ImmutableUInt64Slice) []uint64 { +func GetOrigUInt64Slice(ms UInt64Slice) *[]uint64 { return ms.orig } -func NewImmutableUInt64Slice(orig []uint64) ImmutableUInt64Slice { - return ImmutableUInt64Slice{orig: orig} +func NewUInt64Slice(orig *[]uint64) UInt64Slice { + return UInt64Slice{orig: orig} } diff --git a/pdata/pcommon/common.go b/pdata/pcommon/common.go index 22af8e4cd8a2..dc6405d91484 100644 --- a/pdata/pcommon/common.go +++ b/pdata/pcommon/common.go @@ -120,9 +120,16 @@ func NewValueSlice() Value { return newValue(&otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_ArrayValue{ArrayValue: &otlpcommon.ArrayValue{}}}) } -// NewValueBytes creates a new Value with the given ImmutableByteSlice value. -func NewValueBytes(v ImmutableByteSlice) Value { - return newValue(&otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_BytesValue{BytesValue: v.getOrig()}}) +// NewValueBytes creates a new Value with the given ByteSlice value. +// Deprecated: [0.59.0] Use NewValueBytesEmpty and CopyTo. +func NewValueBytes(v ByteSlice) Value { + return newValue(&otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_BytesValue{BytesValue: *v.getOrig()}}) +} + +// NewValueBytesEmpty creates a new empty Value of byte type. +// NOTE: The function will be deprecated and renamed to NewValueBytes in 0.60.0. +func NewValueBytesEmpty() Value { + return newValue(&otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_BytesValue{BytesValue: nil}}) } func newValue(orig *otlpcommon.AnyValue) Value { @@ -166,7 +173,9 @@ func newValueFromRaw(iv interface{}) Value { case bool: return NewValueBool(tv) case []byte: - return NewValueBytes(NewImmutableByteSlice(tv)) + bv := NewValueBytesEmpty() + bv.BytesVal().CopyFromRaw(tv) + return bv case map[string]interface{}: mv := NewValueMap() NewMapFromRaw(tv).CopyTo(mv.MapVal()) @@ -257,10 +266,16 @@ func (v Value) SliceVal() Slice { } // BytesVal returns the ImmutableByteSlice value associated with this Value. -// If the Type() is not ValueTypeBytes then returns an empty slice. +// If the Type() is not ValueTypeBytes then returns an invalid ByteSlice object. Note that using +// such slice can cause panic. +// // Calling this function on zero-initialized Value will cause a panic. -func (v Value) BytesVal() ImmutableByteSlice { - return ImmutableByteSlice(internal.NewImmutableByteSlice(v.getOrig().GetBytesValue())) +func (v Value) BytesVal() ByteSlice { + bv, ok := v.getOrig().GetValue().(*otlpcommon.AnyValue_BytesValue) + if !ok { + return ByteSlice{} + } + return ByteSlice(internal.NewByteSlice(&bv.BytesValue)) } // SetStringVal replaces the string value associated with this Value, @@ -291,11 +306,20 @@ func (v Value) SetBoolVal(bv bool) { v.getOrig().Value = &otlpcommon.AnyValue_BoolValue{BoolValue: bv} } -// SetBytesVal replaces the ImmutableByteSlice value associated with this Value, +// SetBytesVal replaces the ByteSlice value associated with this Value, // it also changes the type to be ValueTypeBytes. // Calling this function on zero-initialized Value will cause a panic. -func (v Value) SetBytesVal(bv ImmutableByteSlice) { - v.getOrig().Value = &otlpcommon.AnyValue_BytesValue{BytesValue: bv.getOrig()} +// Deprecated: [0.59.0] Use SetEmptyBytesVal().CopyFromRaw(v) instead. +func (v Value) SetBytesVal(bv ByteSlice) { + v.getOrig().Value = &otlpcommon.AnyValue_BytesValue{BytesValue: *bv.getOrig()} +} + +// SetEmptyBytesVal sets value to an empty byte slice and returns it. +// Calling this function on zero-initialized Value will cause a panic. +func (v Value) SetEmptyBytesVal() ByteSlice { + bv := otlpcommon.AnyValue_BytesValue{BytesValue: nil} + v.getOrig().Value = &bv + return ByteSlice(internal.NewByteSlice(&bv.BytesValue)) } // SetEmptyMapVal sets value to an empty map and returns it. @@ -448,7 +472,7 @@ func (v Value) AsString() string { return string(jsonStr) case ValueTypeBytes: - return base64.StdEncoding.EncodeToString(v.BytesVal().getOrig()) + return base64.StdEncoding.EncodeToString(*v.BytesVal().getOrig()) case ValueTypeSlice: jsonStr, _ := json.Marshal(v.SliceVal().AsRaw()) @@ -722,7 +746,8 @@ func (m Map) InsertBool(k string, v bool) { // InsertBytes adds the ImmutableByteSlice Value to the map when the key does not exist. // No action is applied to the map where the key already exists. -func (m Map) InsertBytes(k string, v ImmutableByteSlice) { +// Deprecated: [0.59.0] Use Get(k) and BytesVal().CopyFromRaw(v) instead. +func (m Map) InsertBytes(k string, v ByteSlice) { if _, existing := m.Get(k); !existing { *m.getOrig() = append(*m.getOrig(), newAttributeKeyValueBytes(k, v)) } @@ -784,7 +809,8 @@ func (m Map) UpdateBool(k string, v bool) { // UpdateBytes updates an existing ImmutableByteSlice Value with a value. // No action is applied to the map where the key does not exist. -func (m Map) UpdateBytes(k string, v ImmutableByteSlice) { +// Deprecated: [0.59.0] Use Get(k) and BytesVal().CopyFromRaw(v) instead. +func (m Map) UpdateBytes(k string, v ByteSlice) { if av, existing := m.Get(k); existing { av.SetBytesVal(v) } @@ -869,7 +895,8 @@ func (m Map) UpsertBool(k string, v bool) { // UpsertBytes performs the Insert or Update action. The ImmutableByteSlice Value is // inserted to the map that did not originally have the key. The key/value is // updated to the map where the key already existed. -func (m Map) UpsertBytes(k string, v ImmutableByteSlice) { +// Deprecated: [0.59.0] Use UpsertEmptyBytes().CopyFromRaw(v) instead. +func (m Map) UpsertBytes(k string, v ByteSlice) { if av, existing := m.Get(k); existing { av.SetBytesVal(v) } else { @@ -877,6 +904,17 @@ func (m Map) UpsertBytes(k string, v ImmutableByteSlice) { } } +// UpsertEmptyBytes inserts or updates an empty byte slice under given key and returns it. +func (m Map) UpsertEmptyBytes(k string) ByteSlice { + bv := otlpcommon.AnyValue_BytesValue{} + if av, existing := m.Get(k); existing { + av.getOrig().Value = &bv + } else { + *m.getOrig() = append(*m.getOrig(), otlpcommon.KeyValue{Key: k, Value: otlpcommon.AnyValue{Value: &bv}}) + } + return ByteSlice(internal.NewByteSlice(&bv.BytesValue)) +} + // UpsertEmptyMap inserts or updates an empty map under given key and returns it. func (m Map) UpsertEmptyMap(k string) Map { kvl := otlpcommon.AnyValue_KvlistValue{KvlistValue: &otlpcommon.KeyValueList{Values: []otlpcommon.KeyValue(nil)}} diff --git a/pdata/pcommon/common_test.go b/pdata/pcommon/common_test.go index 7b435d90c3d3..fba87f4b80d6 100644 --- a/pdata/pcommon/common_test.go +++ b/pdata/pcommon/common_test.go @@ -43,9 +43,8 @@ func TestValue(t *testing.T) { assert.EqualValues(t, ValueTypeBool, v.Type()) assert.True(t, v.BoolVal()) - v = NewValueBytes(NewImmutableByteSlice([]byte{1, 2, 3, 4})) + v = NewValueBytesEmpty() assert.EqualValues(t, ValueTypeBytes, v.Type()) - assert.EqualValues(t, NewImmutableByteSlice([]byte{1, 2, 3, 4}), v.BytesVal()) v = NewValueEmpty() assert.EqualValues(t, ValueTypeEmpty, v.Type()) @@ -164,8 +163,8 @@ func TestNilOrigSetValue(t *testing.T) { assert.EqualValues(t, 1.23, av.DoubleVal()) av = NewValueEmpty() - av.SetBytesVal(NewImmutableByteSlice([]byte{1, 2, 3})) - assert.Equal(t, NewImmutableByteSlice([]byte{1, 2, 3}), av.BytesVal()) + av.SetEmptyBytesVal().CopyFromRaw([]byte{1, 2, 3}) + assert.Equal(t, []byte{1, 2, 3}, av.BytesVal().AsRaw()) av = NewValueEmpty() av.SetEmptyMapVal().UpsertString("k", "v") @@ -222,14 +221,16 @@ func TestValueEqual(t *testing.T) { av1 = NewValueBool(false) assert.True(t, av1.Equal(av2)) - av2 = NewValueBytes(NewImmutableByteSlice([]byte{1, 2, 3})) + av2 = NewValueBytesEmpty() + av2.BytesVal().CopyFromRaw([]byte{1, 2, 3}) assert.False(t, av1.Equal(av2)) assert.False(t, av2.Equal(av1)) - av1 = NewValueBytes(NewImmutableByteSlice([]byte{1, 2, 4})) + av1 = NewValueBytesEmpty() + av1.BytesVal().CopyFromRaw([]byte{1, 2, 4}) assert.False(t, av1.Equal(av2)) - av1 = NewValueBytes(NewImmutableByteSlice([]byte{1, 2, 3})) + av1.BytesVal().SetAt(2, 3) assert.True(t, av1.Equal(av2)) av1 = NewValueSlice() @@ -416,6 +417,27 @@ func TestMapUpsertEmptySlice(t *testing.T) { }), m) } +func TestMapUpsertEmptyBytes(t *testing.T) { + m := NewMap() + b := m.UpsertEmptyBytes("k") + bv, ok := m.Get("k") + assert.True(t, ok) + assert.Equal(t, []byte(nil), bv.BytesVal().AsRaw()) + b.CopyFromRaw([]byte{1, 2, 3}) + bv, ok = m.Get("k") + assert.True(t, ok) + assert.Equal(t, []byte{1, 2, 3}, bv.BytesVal().AsRaw()) + + m.UpsertEmptyBytes("k") + bv, ok = m.Get("k") + assert.True(t, ok) + assert.Equal(t, []byte(nil), bv.BytesVal().AsRaw()) + bv.BytesVal().CopyFromRaw([]byte{3, 2, 1}) + bv, ok = m.Get("k") + assert.True(t, ok) + assert.Equal(t, []byte{3, 2, 1}, bv.BytesVal().AsRaw()) +} + func TestMapWithEmpty(t *testing.T) { origWithNil := []otlpcommon.KeyValue{ {}, @@ -927,7 +949,7 @@ func TestAsString(t *testing.T) { }, { name: "bytes", - input: NewValueBytes(NewImmutableByteSlice([]byte("String bytes"))), + input: generateTestValueBytes(), expected: base64.StdEncoding.EncodeToString([]byte("String bytes")), }, } @@ -962,8 +984,8 @@ func TestValueAsRaw(t *testing.T) { }, { name: "bytes", - input: NewValueBytes(NewImmutableByteSlice([]byte("bytes"))), - expected: []byte("bytes"), + input: generateTestValueBytes(), + expected: []byte("String bytes"), }, { name: "empty", @@ -1211,3 +1233,9 @@ func generateTestValueSlice() Value { attrArr.AppendEmpty() return ret } + +func generateTestValueBytes() Value { + v := NewValueBytesEmpty() + v.BytesVal().CopyFromRaw([]byte("String bytes")) + return v +} diff --git a/pdata/pcommon/generated_immutable_slice.go b/pdata/pcommon/generated_immutable_slice.go deleted file mode 100644 index 5c141c8dea5b..000000000000 --- a/pdata/pcommon/generated_immutable_slice.go +++ /dev/null @@ -1,137 +0,0 @@ -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// Code generated by "model/internal/cmd/pdatagen/main.go". DO NOT EDIT. -// To regenerate this file run "go run model/internal/cmd/pdatagen/main.go". - -package pcommon - -import "go.opentelemetry.io/collector/pdata/internal" - -// ImmutableByteSlice represents a []byte slice that cannot be mutated. -// The instance of ImmutableByteSlice can be assigned to multiple objects since it's immutable. -type ImmutableByteSlice internal.ImmutableByteSlice - -func (ms ImmutableByteSlice) getOrig() []byte { - return internal.GetOrigImmutableByteSlice(internal.ImmutableByteSlice(ms)) -} - -// NewImmutableByteSlice creates a new ImmutableByteSlice by copying the provided []byte slice. -func NewImmutableByteSlice(orig []byte) ImmutableByteSlice { - if len(orig) == 0 { - return ImmutableByteSlice(internal.NewImmutableByteSlice(nil)) - } - copyOrig := make([]byte, len(orig)) - copy(copyOrig, orig) - return ImmutableByteSlice(internal.NewImmutableByteSlice(copyOrig)) -} - -// AsRaw returns a copy of the []byte slice. -func (ms ImmutableByteSlice) AsRaw() []byte { - orig := ms.getOrig() - if len(orig) == 0 { - return nil - } - copyOrig := make([]byte, len(orig)) - copy(copyOrig, orig) - return copyOrig -} - -// Len returns length of the []byte slice value. -func (ms ImmutableByteSlice) Len() int { - return len(ms.getOrig()) -} - -// At returns an item from particular index. -func (ms ImmutableByteSlice) At(i int) byte { - return ms.getOrig()[i] -} - -// ImmutableFloat64Slice represents a []float64 slice that cannot be mutated. -// The instance of ImmutableFloat64Slice can be assigned to multiple objects since it's immutable. -type ImmutableFloat64Slice internal.ImmutableFloat64Slice - -func (ms ImmutableFloat64Slice) getOrig() []float64 { - return internal.GetOrigImmutableFloat64Slice(internal.ImmutableFloat64Slice(ms)) -} - -// NewImmutableFloat64Slice creates a new ImmutableFloat64Slice by copying the provided []float64 slice. -func NewImmutableFloat64Slice(orig []float64) ImmutableFloat64Slice { - if len(orig) == 0 { - return ImmutableFloat64Slice(internal.NewImmutableFloat64Slice(nil)) - } - copyOrig := make([]float64, len(orig)) - copy(copyOrig, orig) - return ImmutableFloat64Slice(internal.NewImmutableFloat64Slice(copyOrig)) -} - -// AsRaw returns a copy of the []float64 slice. -func (ms ImmutableFloat64Slice) AsRaw() []float64 { - orig := ms.getOrig() - if len(orig) == 0 { - return nil - } - copyOrig := make([]float64, len(orig)) - copy(copyOrig, orig) - return copyOrig -} - -// Len returns length of the []float64 slice value. -func (ms ImmutableFloat64Slice) Len() int { - return len(ms.getOrig()) -} - -// At returns an item from particular index. -func (ms ImmutableFloat64Slice) At(i int) float64 { - return ms.getOrig()[i] -} - -// ImmutableUInt64Slice represents a []uint64 slice that cannot be mutated. -// The instance of ImmutableUInt64Slice can be assigned to multiple objects since it's immutable. -type ImmutableUInt64Slice internal.ImmutableUInt64Slice - -func (ms ImmutableUInt64Slice) getOrig() []uint64 { - return internal.GetOrigImmutableUInt64Slice(internal.ImmutableUInt64Slice(ms)) -} - -// NewImmutableUInt64Slice creates a new ImmutableUInt64Slice by copying the provided []uint64 slice. -func NewImmutableUInt64Slice(orig []uint64) ImmutableUInt64Slice { - if len(orig) == 0 { - return ImmutableUInt64Slice(internal.NewImmutableUInt64Slice(nil)) - } - copyOrig := make([]uint64, len(orig)) - copy(copyOrig, orig) - return ImmutableUInt64Slice(internal.NewImmutableUInt64Slice(copyOrig)) -} - -// AsRaw returns a copy of the []uint64 slice. -func (ms ImmutableUInt64Slice) AsRaw() []uint64 { - orig := ms.getOrig() - if len(orig) == 0 { - return nil - } - copyOrig := make([]uint64, len(orig)) - copy(copyOrig, orig) - return copyOrig -} - -// Len returns length of the []uint64 slice value. -func (ms ImmutableUInt64Slice) Len() int { - return len(ms.getOrig()) -} - -// At returns an item from particular index. -func (ms ImmutableUInt64Slice) At(i int) uint64 { - return ms.getOrig()[i] -} diff --git a/pdata/pcommon/generated_immutable_slice_test.go b/pdata/pcommon/generated_immutable_slice_test.go deleted file mode 100644 index c42f8ae0443b..000000000000 --- a/pdata/pcommon/generated_immutable_slice_test.go +++ /dev/null @@ -1,135 +0,0 @@ -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// Code generated by "model/internal/cmd/pdatagen/main.go". DO NOT EDIT. -// To regenerate this file run "go run model/internal/cmd/pdatagen/main.go". - -package pcommon - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestNewImmutableByteSlice(t *testing.T) { - tests := []struct { - name string - orig []byte - want []byte - }{ - { - name: "nil", - orig: nil, - want: nil, - }, - { - name: "empty", - orig: []byte{}, - want: nil, - }, - { - name: "copy", - orig: []byte{1, 2, 3}, - want: []byte{1, 2, 3}, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - s := NewImmutableByteSlice(tt.orig) - assert.Equal(t, tt.want, s.AsRaw()) - assert.Equal(t, len(s.getOrig()), s.Len()) - if len(tt.orig) > 0 { - // verify that orig mutation doesn't have any effect - tt.orig[0] = byte(0) - assert.Equal(t, byte(1), s.At(0)) - } - }) - } -} - -func TestNewImmutableFloat64Slice(t *testing.T) { - tests := []struct { - name string - orig []float64 - want []float64 - }{ - { - name: "nil", - orig: nil, - want: nil, - }, - { - name: "empty", - orig: []float64{}, - want: nil, - }, - { - name: "copy", - orig: []float64{1, 2, 3}, - want: []float64{1, 2, 3}, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - s := NewImmutableFloat64Slice(tt.orig) - assert.Equal(t, tt.want, s.AsRaw()) - assert.Equal(t, len(s.getOrig()), s.Len()) - if len(tt.orig) > 0 { - // verify that orig mutation doesn't have any effect - tt.orig[0] = float64(0) - assert.Equal(t, float64(1), s.At(0)) - } - }) - } -} - -func TestNewImmutableUInt64Slice(t *testing.T) { - tests := []struct { - name string - orig []uint64 - want []uint64 - }{ - { - name: "nil", - orig: nil, - want: nil, - }, - { - name: "empty", - orig: []uint64{}, - want: nil, - }, - { - name: "copy", - orig: []uint64{1, 2, 3}, - want: []uint64{1, 2, 3}, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - s := NewImmutableUInt64Slice(tt.orig) - assert.Equal(t, tt.want, s.AsRaw()) - assert.Equal(t, len(s.getOrig()), s.Len()) - if len(tt.orig) > 0 { - // verify that orig mutation doesn't have any effect - tt.orig[0] = uint64(0) - assert.Equal(t, uint64(1), s.At(0)) - } - }) - } -} diff --git a/pdata/pcommon/generated_primitive_slice.go b/pdata/pcommon/generated_primitive_slice.go new file mode 100644 index 000000000000..c18c00f7308b --- /dev/null +++ b/pdata/pcommon/generated_primitive_slice.go @@ -0,0 +1,197 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by "model/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "go run model/internal/cmd/pdatagen/main.go". + +package pcommon + +import "go.opentelemetry.io/collector/pdata/internal" + +// ByteSlice represents a []byte slice. +// The instance of ByteSlice can be assigned to multiple objects since it's immutable. +// +// Must use NewByteSlice function to create new instances. +// Important: zero-initialized instance is not valid for use. +type ByteSlice internal.ByteSlice + +// Deprecated: [0.59.0] Use ByteSlice instead. +type ImmutableByteSlice = ByteSlice + +func (ms ByteSlice) getOrig() *[]byte { + return internal.GetOrigByteSlice(internal.ByteSlice(ms)) +} + +// NewByteSlice creates a new empty ByteSlice. +func NewByteSlice() ByteSlice { + orig := []byte(nil) + return ByteSlice(internal.NewByteSlice(&orig)) +} + +// NewImmutableByteSlice creates a new ByteSlice by copying the provided []byte slice. +// Deprecated: [0.59.0] Use New{structName}() and {structName}.CopyFromRaw([]byte) instead +func NewImmutableByteSlice(orig []byte) ByteSlice { + return ByteSlice(internal.NewByteSlice(&orig)) +} + +// AsRaw returns a copy of the []byte slice. +func (ms ByteSlice) AsRaw() []byte { + orig := *ms.getOrig() + if len(orig) == 0 { + return nil + } + copyOrig := make([]byte, len(orig)) + copy(copyOrig, orig) + return copyOrig +} + +// CopyFromRaw copies raw []byte into the slice ByteSlice. +func (ms ByteSlice) CopyFromRaw(val []byte) { + copyVal := make([]byte, len(val)) + copy(copyVal, val) + *ms.getOrig() = copyVal +} + +// Len returns length of the []byte slice value. +func (ms ByteSlice) Len() int { + return len(*ms.getOrig()) +} + +// At returns an item from particular index. +func (ms ByteSlice) At(i int) byte { + return (*ms.getOrig())[i] +} + +// SetAt sets byte item at particular index. +func (ms ByteSlice) SetAt(i int, val byte) { + (*ms.getOrig())[i] = val +} + +// Float64Slice represents a []float64 slice. +// The instance of Float64Slice can be assigned to multiple objects since it's immutable. +// +// Must use NewFloat64Slice function to create new instances. +// Important: zero-initialized instance is not valid for use. +type Float64Slice internal.Float64Slice + +// Deprecated: [0.59.0] Use Float64Slice instead. +type ImmutableFloat64Slice = Float64Slice + +func (ms Float64Slice) getOrig() *[]float64 { + return internal.GetOrigFloat64Slice(internal.Float64Slice(ms)) +} + +// NewFloat64Slice creates a new empty Float64Slice. +func NewFloat64Slice() Float64Slice { + orig := []float64(nil) + return Float64Slice(internal.NewFloat64Slice(&orig)) +} + +// NewImmutableFloat64Slice creates a new Float64Slice by copying the provided []float64 slice. +// Deprecated: [0.59.0] Use New{structName}() and {structName}.CopyFromRaw([]float64) instead +func NewImmutableFloat64Slice(orig []float64) Float64Slice { + return Float64Slice(internal.NewFloat64Slice(&orig)) +} + +// AsRaw returns a copy of the []float64 slice. +func (ms Float64Slice) AsRaw() []float64 { + orig := *ms.getOrig() + if len(orig) == 0 { + return nil + } + copyOrig := make([]float64, len(orig)) + copy(copyOrig, orig) + return copyOrig +} + +// CopyFromRaw copies raw []float64 into the slice Float64Slice. +func (ms Float64Slice) CopyFromRaw(val []float64) { + copyVal := make([]float64, len(val)) + copy(copyVal, val) + *ms.getOrig() = copyVal +} + +// Len returns length of the []float64 slice value. +func (ms Float64Slice) Len() int { + return len(*ms.getOrig()) +} + +// At returns an item from particular index. +func (ms Float64Slice) At(i int) float64 { + return (*ms.getOrig())[i] +} + +// SetAt sets float64 item at particular index. +func (ms Float64Slice) SetAt(i int, val float64) { + (*ms.getOrig())[i] = val +} + +// UInt64Slice represents a []uint64 slice. +// The instance of UInt64Slice can be assigned to multiple objects since it's immutable. +// +// Must use NewUInt64Slice function to create new instances. +// Important: zero-initialized instance is not valid for use. +type UInt64Slice internal.UInt64Slice + +// Deprecated: [0.59.0] Use UInt64Slice instead. +type ImmutableUInt64Slice = UInt64Slice + +func (ms UInt64Slice) getOrig() *[]uint64 { + return internal.GetOrigUInt64Slice(internal.UInt64Slice(ms)) +} + +// NewUInt64Slice creates a new empty UInt64Slice. +func NewUInt64Slice() UInt64Slice { + orig := []uint64(nil) + return UInt64Slice(internal.NewUInt64Slice(&orig)) +} + +// NewImmutableUInt64Slice creates a new UInt64Slice by copying the provided []uint64 slice. +// Deprecated: [0.59.0] Use New{structName}() and {structName}.CopyFromRaw([]uint64) instead +func NewImmutableUInt64Slice(orig []uint64) UInt64Slice { + return UInt64Slice(internal.NewUInt64Slice(&orig)) +} + +// AsRaw returns a copy of the []uint64 slice. +func (ms UInt64Slice) AsRaw() []uint64 { + orig := *ms.getOrig() + if len(orig) == 0 { + return nil + } + copyOrig := make([]uint64, len(orig)) + copy(copyOrig, orig) + return copyOrig +} + +// CopyFromRaw copies raw []uint64 into the slice UInt64Slice. +func (ms UInt64Slice) CopyFromRaw(val []uint64) { + copyVal := make([]uint64, len(val)) + copy(copyVal, val) + *ms.getOrig() = copyVal +} + +// Len returns length of the []uint64 slice value. +func (ms UInt64Slice) Len() int { + return len(*ms.getOrig()) +} + +// At returns an item from particular index. +func (ms UInt64Slice) At(i int) uint64 { + return (*ms.getOrig())[i] +} + +// SetAt sets uint64 item at particular index. +func (ms UInt64Slice) SetAt(i int, val uint64) { + (*ms.getOrig())[i] = val +} diff --git a/pdata/pcommon/generated_primitive_slice_test.go b/pdata/pcommon/generated_primitive_slice_test.go new file mode 100644 index 000000000000..3c9d8bde3b9d --- /dev/null +++ b/pdata/pcommon/generated_primitive_slice_test.go @@ -0,0 +1,63 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by "model/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "go run model/internal/cmd/pdatagen/main.go". + +package pcommon + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNewByteSlice(t *testing.T) { + ms := NewByteSlice() + assert.Equal(t, 0, ms.Len()) + ms.CopyFromRaw([]byte{1, 2, 3}) + assert.Equal(t, 3, ms.Len()) + assert.Equal(t, []byte{1, 2, 3}, ms.AsRaw()) + ms.SetAt(1, byte(5)) + assert.Equal(t, []byte{1, 5, 3}, ms.AsRaw()) + ms.CopyFromRaw([]byte{3}) + assert.Equal(t, 1, ms.Len()) + assert.Equal(t, byte(3), ms.At(0)) +} + +func TestNewFloat64Slice(t *testing.T) { + ms := NewFloat64Slice() + assert.Equal(t, 0, ms.Len()) + ms.CopyFromRaw([]float64{1, 2, 3}) + assert.Equal(t, 3, ms.Len()) + assert.Equal(t, []float64{1, 2, 3}, ms.AsRaw()) + ms.SetAt(1, float64(5)) + assert.Equal(t, []float64{1, 5, 3}, ms.AsRaw()) + ms.CopyFromRaw([]float64{3}) + assert.Equal(t, 1, ms.Len()) + assert.Equal(t, float64(3), ms.At(0)) +} + +func TestNewUInt64Slice(t *testing.T) { + ms := NewUInt64Slice() + assert.Equal(t, 0, ms.Len()) + ms.CopyFromRaw([]uint64{1, 2, 3}) + assert.Equal(t, 3, ms.Len()) + assert.Equal(t, []uint64{1, 2, 3}, ms.AsRaw()) + ms.SetAt(1, uint64(5)) + assert.Equal(t, []uint64{1, 5, 3}, ms.AsRaw()) + ms.CopyFromRaw([]uint64{3}) + assert.Equal(t, 1, ms.Len()) + assert.Equal(t, uint64(3), ms.At(0)) +} diff --git a/pdata/pmetric/generated_metrics.go b/pdata/pmetric/generated_metrics.go index c8643e50571b..f6a86dc3efb3 100644 --- a/pdata/pmetric/generated_metrics.go +++ b/pdata/pmetric/generated_metrics.go @@ -1492,23 +1492,25 @@ func (ms HistogramDataPoint) SetSum(v float64) { } // BucketCounts returns the bucketcounts associated with this HistogramDataPoint. -func (ms HistogramDataPoint) BucketCounts() pcommon.ImmutableUInt64Slice { - return pcommon.ImmutableUInt64Slice(internal.NewImmutableUInt64Slice(ms.getOrig().BucketCounts)) +func (ms HistogramDataPoint) BucketCounts() pcommon.UInt64Slice { + return pcommon.UInt64Slice(internal.NewUInt64Slice(&ms.getOrig().BucketCounts)) } // SetBucketCounts replaces the bucketcounts associated with this HistogramDataPoint. -func (ms HistogramDataPoint) SetBucketCounts(v pcommon.ImmutableUInt64Slice) { - ms.getOrig().BucketCounts = internal.GetOrigImmutableUInt64Slice(internal.ImmutableUInt64Slice(v)) +// Deprecated: [0.59.0] Use BucketCounts().CopyFromRaw() instead +func (ms HistogramDataPoint) SetBucketCounts(v pcommon.UInt64Slice) { + ms.getOrig().BucketCounts = *internal.GetOrigUInt64Slice(internal.UInt64Slice(v)) } // ExplicitBounds returns the explicitbounds associated with this HistogramDataPoint. -func (ms HistogramDataPoint) ExplicitBounds() pcommon.ImmutableFloat64Slice { - return pcommon.ImmutableFloat64Slice(internal.NewImmutableFloat64Slice(ms.getOrig().ExplicitBounds)) +func (ms HistogramDataPoint) ExplicitBounds() pcommon.Float64Slice { + return pcommon.Float64Slice(internal.NewFloat64Slice(&ms.getOrig().ExplicitBounds)) } // SetExplicitBounds replaces the explicitbounds associated with this HistogramDataPoint. -func (ms HistogramDataPoint) SetExplicitBounds(v pcommon.ImmutableFloat64Slice) { - ms.getOrig().ExplicitBounds = internal.GetOrigImmutableFloat64Slice(internal.ImmutableFloat64Slice(v)) +// Deprecated: [0.59.0] Use ExplicitBounds().CopyFromRaw() instead +func (ms HistogramDataPoint) SetExplicitBounds(v pcommon.Float64Slice) { + ms.getOrig().ExplicitBounds = *internal.GetOrigFloat64Slice(internal.Float64Slice(v)) } // Exemplars returns the Exemplars associated with this HistogramDataPoint. @@ -1968,13 +1970,14 @@ func (ms Buckets) SetOffset(v int32) { } // BucketCounts returns the bucketcounts associated with this Buckets. -func (ms Buckets) BucketCounts() pcommon.ImmutableUInt64Slice { - return pcommon.ImmutableUInt64Slice(internal.NewImmutableUInt64Slice(ms.getOrig().BucketCounts)) +func (ms Buckets) BucketCounts() pcommon.UInt64Slice { + return pcommon.UInt64Slice(internal.NewUInt64Slice(&ms.getOrig().BucketCounts)) } // SetBucketCounts replaces the bucketcounts associated with this Buckets. -func (ms Buckets) SetBucketCounts(v pcommon.ImmutableUInt64Slice) { - ms.getOrig().BucketCounts = internal.GetOrigImmutableUInt64Slice(internal.ImmutableUInt64Slice(v)) +// Deprecated: [0.59.0] Use BucketCounts().CopyFromRaw() instead +func (ms Buckets) SetBucketCounts(v pcommon.UInt64Slice) { + ms.getOrig().BucketCounts = *internal.GetOrigUInt64Slice(internal.UInt64Slice(v)) } // CopyTo copies all properties from the current struct to the dest. diff --git a/pdata/pmetric/generated_metrics_test.go b/pdata/pmetric/generated_metrics_test.go index 626f35e8a0a0..e76120581610 100644 --- a/pdata/pmetric/generated_metrics_test.go +++ b/pdata/pmetric/generated_metrics_test.go @@ -1073,18 +1073,16 @@ func TestHistogramDataPoint_Sum(t *testing.T) { func TestHistogramDataPoint_BucketCounts(t *testing.T) { ms := NewHistogramDataPoint() - assert.Equal(t, pcommon.NewImmutableUInt64Slice([]uint64(nil)), ms.BucketCounts()) - testValBucketCounts := pcommon.NewImmutableUInt64Slice([]uint64{1, 2, 3}) - ms.SetBucketCounts(testValBucketCounts) - assert.Equal(t, testValBucketCounts, ms.BucketCounts()) + assert.Equal(t, []uint64(nil), ms.BucketCounts().AsRaw()) + ms.BucketCounts().CopyFromRaw([]uint64{1, 2, 3}) + assert.Equal(t, []uint64{1, 2, 3}, ms.BucketCounts().AsRaw()) } func TestHistogramDataPoint_ExplicitBounds(t *testing.T) { ms := NewHistogramDataPoint() - assert.Equal(t, pcommon.NewImmutableFloat64Slice([]float64(nil)), ms.ExplicitBounds()) - testValExplicitBounds := pcommon.NewImmutableFloat64Slice([]float64{1, 2, 3}) - ms.SetExplicitBounds(testValExplicitBounds) - assert.Equal(t, testValExplicitBounds, ms.ExplicitBounds()) + assert.Equal(t, []float64(nil), ms.ExplicitBounds().AsRaw()) + ms.ExplicitBounds().CopyFromRaw([]float64{1, 2, 3}) + assert.Equal(t, []float64{1, 2, 3}, ms.ExplicitBounds().AsRaw()) } func TestHistogramDataPoint_Exemplars(t *testing.T) { @@ -1363,10 +1361,9 @@ func TestBuckets_Offset(t *testing.T) { func TestBuckets_BucketCounts(t *testing.T) { ms := NewBuckets() - assert.Equal(t, pcommon.NewImmutableUInt64Slice([]uint64(nil)), ms.BucketCounts()) - testValBucketCounts := pcommon.NewImmutableUInt64Slice([]uint64{1, 2, 3}) - ms.SetBucketCounts(testValBucketCounts) - assert.Equal(t, testValBucketCounts, ms.BucketCounts()) + assert.Equal(t, []uint64(nil), ms.BucketCounts().AsRaw()) + ms.BucketCounts().CopyFromRaw([]uint64{1, 2, 3}) + assert.Equal(t, []uint64{1, 2, 3}, ms.BucketCounts().AsRaw()) } func TestSummaryDataPointSlice(t *testing.T) { diff --git a/pdata/pmetric/json_test.go b/pdata/pmetric/json_test.go index 23457c921f4f..2181b3a805f6 100644 --- a/pdata/pmetric/json_test.go +++ b/pdata/pmetric/json_test.go @@ -88,7 +88,7 @@ var metricsSumOTLPFull = func() Metrics { datapoint.Attributes().UpsertBool("bool", true) datapoint.Attributes().UpsertInt("int", 1) datapoint.Attributes().UpsertDouble("double", 1.1) - datapoint.Attributes().UpsertBytes("bytes", pcommon.NewImmutableByteSlice([]byte("foo"))) + datapoint.Attributes().UpsertEmptyBytes("bytes").CopyFromRaw([]byte("foo")) exemplar := datapoint.Exemplars().AppendEmpty() exemplar.SetDoubleVal(99.3) exemplar.SetTimestamp(pcommon.NewTimestampFromTime(time.Now())) @@ -127,7 +127,7 @@ var metricsGaugeOTLPFull = func() Metrics { datapoint.Attributes().UpsertBool("bool", true) datapoint.Attributes().UpsertInt("int", 1) datapoint.Attributes().UpsertDouble("double", 1.1) - datapoint.Attributes().UpsertBytes("bytes", pcommon.NewImmutableByteSlice([]byte("foo"))) + datapoint.Attributes().UpsertEmptyBytes("bytes").CopyFromRaw([]byte("foo")) exemplar := datapoint.Exemplars().AppendEmpty() exemplar.SetDoubleVal(99.3) exemplar.SetTimestamp(pcommon.NewTimestampFromTime(time.Now())) @@ -166,11 +166,11 @@ var metricsHistogramOTLPFull = func() Metrics { datapoint.Attributes().UpsertBool("bool", true) datapoint.Attributes().UpsertInt("int", 1) datapoint.Attributes().UpsertDouble("double", 1.1) - datapoint.Attributes().UpsertBytes("bytes", pcommon.NewImmutableByteSlice([]byte("foo"))) + datapoint.Attributes().UpsertEmptyBytes("bytes").CopyFromRaw([]byte("foo")) datapoint.SetCount(4) datapoint.SetSum(345) - datapoint.SetBucketCounts(pcommon.NewImmutableUInt64Slice([]uint64{1, 1, 2})) - datapoint.SetExplicitBounds(pcommon.NewImmutableFloat64Slice([]float64{10, 100})) + datapoint.BucketCounts().CopyFromRaw([]uint64{1, 1, 2}) + datapoint.ExplicitBounds().CopyFromRaw([]float64{10, 100}) exemplar := datapoint.Exemplars().AppendEmpty() exemplar.SetDoubleVal(99.3) exemplar.SetTimestamp(pcommon.NewTimestampFromTime(time.Now())) @@ -212,10 +212,10 @@ var metricsExponentialHistogramOTLPFull = func() Metrics { datapoint.Attributes().UpsertBool("bool", true) datapoint.Attributes().UpsertInt("int", 1) datapoint.Attributes().UpsertDouble("double", 1.1) - datapoint.Attributes().UpsertBytes("bytes", pcommon.NewImmutableByteSlice([]byte("foo"))) + datapoint.Attributes().UpsertEmptyBytes("bytes").CopyFromRaw([]byte("foo")) datapoint.SetCount(4) datapoint.SetSum(345) - datapoint.Positive().SetBucketCounts(pcommon.NewImmutableUInt64Slice([]uint64{1, 1, 2})) + datapoint.Positive().BucketCounts().CopyFromRaw([]uint64{1, 1, 2}) datapoint.Positive().SetOffset(2) exemplar := datapoint.Exemplars().AppendEmpty() exemplar.SetDoubleVal(99.3) @@ -227,7 +227,7 @@ var metricsExponentialHistogramOTLPFull = func() Metrics { exemplar.SetTraceID(traceID) exemplar.FilteredAttributes().UpsertString("service.name", "testService") datapoint.SetMax(float64(time.Now().Unix())) - datapoint.Negative().SetBucketCounts(pcommon.NewImmutableUInt64Slice([]uint64{1, 1, 2})) + datapoint.Negative().BucketCounts().CopyFromRaw([]uint64{1, 1, 2}) datapoint.Negative().SetOffset(2) datapoint.SetZeroCount(5) datapoint.SetTimestamp(pcommon.NewTimestampFromTime(time.Now())) @@ -264,7 +264,7 @@ var metricsSummaryOTLPFull = func() Metrics { datapoint.Attributes().UpsertBool("bool", true) datapoint.Attributes().UpsertInt("int", 1) datapoint.Attributes().UpsertDouble("double", 1.1) - datapoint.Attributes().UpsertBytes("bytes", pcommon.NewImmutableByteSlice([]byte("foo"))) + datapoint.Attributes().UpsertEmptyBytes("bytes").CopyFromRaw([]byte("foo")) datapoint.SetTimestamp(pcommon.NewTimestampFromTime(time.Now())) return metric } diff --git a/pdata/pmetric/metrics_test.go b/pdata/pmetric/metrics_test.go index cfb902d134e4..24e65e1a49c9 100644 --- a/pdata/pmetric/metrics_test.go +++ b/pdata/pmetric/metrics_test.go @@ -562,9 +562,9 @@ func TestOtlpToFromInternalHistogramMutating(t *testing.T) { histogramDataPoints.At(0).Attributes().Remove("key0") histogramDataPoints.At(0).Attributes().UpsertString("k", "v") assert.EqualValues(t, newAttributes, histogramDataPoints.At(0).Attributes()) - histogramDataPoints.At(0).SetExplicitBounds(pcommon.NewImmutableFloat64Slice([]float64{1})) + histogramDataPoints.At(0).ExplicitBounds().CopyFromRaw([]float64{1}) assert.EqualValues(t, []float64{1}, histogramDataPoints.At(0).ExplicitBounds().AsRaw()) - histogramDataPoints.At(0).SetBucketCounts(pcommon.NewImmutableUInt64Slice([]uint64{21, 32})) + histogramDataPoints.At(0).BucketCounts().CopyFromRaw([]uint64{21, 32}) // Test that everything is updated. assert.EqualValues(t, &otlpmetrics.MetricsData{ ResourceMetrics: []*otlpmetrics.ResourceMetrics{ diff --git a/pdata/ptrace/json_test.go b/pdata/ptrace/json_test.go index 4c95d11715d4..aae4b4bb0f9c 100644 --- a/pdata/ptrace/json_test.go +++ b/pdata/ptrace/json_test.go @@ -94,7 +94,7 @@ var tracesOTLPFull = func() Traces { sp.Attributes().UpsertBool("bool", true) sp.Attributes().UpsertInt("int", 1) sp.Attributes().UpsertDouble("double", 1.1) - sp.Attributes().UpsertBytes("bytes", pcommon.NewImmutableByteSlice([]byte("foo"))) + sp.Attributes().UpsertEmptyBytes("bytes").CopyFromRaw([]byte("foo")) arr := sp.Attributes().UpsertEmptySlice("array") arr.AppendEmpty().SetIntVal(1) arr.AppendEmpty().SetStringVal("str") @@ -110,7 +110,7 @@ var tracesOTLPFull = func() Traces { event.Attributes().UpsertBool("bool", true) event.Attributes().UpsertInt("int", 1) event.Attributes().UpsertDouble("double", 1.1) - event.Attributes().UpsertBytes("bytes", pcommon.NewImmutableByteSlice([]byte("foo"))) + event.Attributes().UpsertEmptyBytes("bytes").CopyFromRaw([]byte("foo")) // Add links. link := sp.Links().AppendEmpty() link.SetTraceState("state") @@ -121,7 +121,7 @@ var tracesOTLPFull = func() Traces { link.Attributes().UpsertBool("bool", true) link.Attributes().UpsertInt("int", 1) link.Attributes().UpsertDouble("double", 1.1) - link.Attributes().UpsertBytes("bytes", pcommon.NewImmutableByteSlice([]byte("foo"))) + link.Attributes().UpsertEmptyBytes("bytes").CopyFromRaw([]byte("foo")) // Add another span. sp2 := il.Spans().AppendEmpty() sp2.SetName("testSpan2")