Skip to content

Commit

Permalink
[pdata] Add SetLen method to primitive slices
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitryax committed Sep 13, 2022
1 parent 38826ae commit 965858e
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
- Deprecate `*DataPoint.[Set]FlagsImmutable()` funcs in favor of `*DataPoint.[Set]Flags()`. (#6017)
- Deprecate `LogRecord.FlagsStruct()` and `LogRecord.SetFlagsStruct()` in favor of `LogRecord.Flags()` and `LogRecord.SetFlags()`. (#6007)
- Deprecate `config.Unmarshallable` in favor of `confmap.Unmarshaler`. (#6031)
- Primitive slice wrapper are now mutable (#5971):
- Primitive slice wrapper are now mutable (#5971, #6053):
- `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`
Expand Down
24 changes: 24 additions & 0 deletions pdata/internal/cmd/pdatagen/internal/primitive_slice_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ func (ms ${structName}) SetAt(i int, val ${itemType}) {
(*ms.getOrig())[i] = val
}
// SetLen sets length of ${structName}.
func (ms ${structName}) SetLen(l int) {
switch {
case l < len(*ms.getOrig()):
*ms.getOrig() = (*ms.getOrig())[:l]
case len(*ms.getOrig()) < l:
buf := *ms.getOrig()
*ms.getOrig() = make([]${itemType}, l)
copy(*ms.getOrig(), buf)
}
}
// MoveTo moves ${structName} to another instance.
func (ms ${structName}) MoveTo(dest ${structName}) {
*dest.getOrig() = *ms.getOrig()
Expand Down Expand Up @@ -111,6 +123,18 @@ const immutableSliceTestTemplate = `func TestNew${structName}(t *testing.T) {
ms.MoveTo(mv)
assert.Equal(t, 3, mv.Len())
assert.Equal(t, ${itemType}(1), mv.At(0))
}
func Test${structName}SetLen(t *testing.T) {
ms := New${structName}()
ms.FromRaw([]${itemType}{1, 2, 3})
ms.SetLen(1)
assert.Equal(t, 1, ms.Len())
assert.Equal(t, ${itemType}(1), ms.At(0))
ms.SetLen(3)
assert.Equal(t, 3, ms.Len())
assert.Equal(t, ${itemType}(1), ms.At(0))
assert.Equal(t, ${itemType}(0), ms.At(2))
}`

const primitiveSliceInternalTemplate = `
Expand Down
36 changes: 36 additions & 0 deletions pdata/pcommon/generated_primitive_slice.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions pdata/pcommon/generated_primitive_slice_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 965858e

Please sign in to comment.