Skip to content

Commit

Permalink
Add Append to Generated pdata Structs (#1157)
Browse files Browse the repository at this point in the history
This makes it a bit more convenient to assemble data structures.

It is also congruent with the existing Go slice functionality with
the same name.
  • Loading branch information
benkeith-splunk authored Jun 30, 2020
1 parent 28d10c8 commit 55b6458
Show file tree
Hide file tree
Showing 7 changed files with 425 additions and 0 deletions.
25 changes: 25 additions & 0 deletions cmd/pdatagen/internal/base_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ func (es ${structName}) Resize(newLen int) {
oldOrig = append(oldOrig, &extraOrigs[i])
}
(*es.orig) = oldOrig
}
// Append will increase the length of the ${structName} by one and set the
// given ${elementName} at that new position. The original ${elementName}
// could still be referenced so do not reuse it after passing it to this
// method.
func (es ${structName}) Append(e *${elementName}) {
(*es.orig) = append((*es.orig), *e.orig)
}`

const sliceTestTemplate = `func Test${structName}(t *testing.T) {
Expand Down Expand Up @@ -233,6 +241,23 @@ func Test${structName}_Resize(t *testing.T) {
// Test Resize 0 elements.
es.Resize(0)
assert.EqualValues(t, New${structName}(), es)
}
func Test${structName}_Append(t *testing.T) {
es := generateTest${structName}()
emptyVal := New${elementName}()
emptyVal.InitEmpty()
es.Append(&emptyVal)
assert.EqualValues(t, *(es.At(7)).orig, *emptyVal.orig)
emptyVal2:= New${elementName}()
emptyVal2.InitEmpty()
es.Append(&emptyVal2)
assert.EqualValues(t, *(es.At(8)).orig, *emptyVal2.orig)
assert.Equal(t, 9, es.Len())
}`

const sliceGenerateTest = `func generateTest${structName}() ${structName} {
Expand Down
16 changes: 16 additions & 0 deletions consumer/pdata/generated_log.go

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

34 changes: 34 additions & 0 deletions consumer/pdata/generated_log_test.go

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

72 changes: 72 additions & 0 deletions consumer/pdata/generated_metrics.go

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

Loading

0 comments on commit 55b6458

Please sign in to comment.