Skip to content

Commit

Permalink
[pdata] Add AppendEmpty and EnsureCapacity method to primitive slices
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitryax committed Sep 14, 2022
1 parent fa45027 commit 9f5c741
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### 💡 Enhancements 💡

- Add AppendEmpty and EnsureCapacity method to primitive pdata slices (#6060)

## v0.60.0 Beta

### 🛑 Breaking changes 🛑
Expand Down
33 changes: 33 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,25 @@ func (ms ${structName}) SetAt(i int, val ${itemType}) {
(*ms.getOrig())[i] = val
}
// EnsureCapacity ensures ${structName} has at least the specified capacity.
// 1. If the newCap <= cap, then is no change in capacity.
// 2. If the newCap > cap, then the slice capacity will be expanded to the provided value.
func (ms ${structName}) EnsureCapacity(newCap int) {
oldCap := cap(*ms.getOrig())
if newCap <= oldCap {
return
}
newOrig := make([]${itemType}, len(*ms.getOrig()), newCap)
copy(newOrig, *ms.getOrig())
*ms.getOrig() = newOrig
}
// Append appends extra elements to ${structName}.
func (ms ${structName}) Append(elms ...${itemType}) {
*ms.getOrig() = append(*ms.getOrig(), elms...)
}
// MoveTo moves ${structName} to another instance.
func (ms ${structName}) MoveTo(dest ${structName}) {
*dest.getOrig() = *ms.getOrig()
Expand Down Expand Up @@ -106,6 +125,20 @@ 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}Append(t *testing.T) {
ms := New${structName}()
ms.FromRaw([]${itemType}{1, 2, 3})
ms.Append(4, 5)
assert.Equal(t, 5, ms.Len())
assert.Equal(t, ${itemType}(5), ms.At(4))
}
func Test${structName}EnsureCapacity(t *testing.T) {
ms := New${structName}()
ms.EnsureCapacity(4)
assert.Equal(t, 4, cap(*ms.getOrig()))
}`

const primitiveSliceInternalTemplate = `
Expand Down
57 changes: 57 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.

42 changes: 42 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 9f5c741

Please sign in to comment.