Skip to content

Commit

Permalink
Add AppendEmpty and deprecate Append for slices (#2970)
Browse files Browse the repository at this point in the history
This change helps with initialization:
* Lots of places where we used Resize(1). Some left to limit the PR.
* Lots of places where New followed by Append, this is equivalent with AppendEmpty.

Append is marked as deprecated, but we need to evaluate if we can change all calls to AppendEmpty,
or we need to revert the deprecation. The current goal is to remove it if possible.

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Apr 20, 2021
1 parent 5e14135 commit b8dccfc
Show file tree
Hide file tree
Showing 36 changed files with 493 additions and 496 deletions.
26 changes: 20 additions & 6 deletions cmd/pdatagen/internal/base_slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,16 @@ func (es ${structName}) Resize(newLen int) {
// given ${elementName} at that new position. The original ${elementName}
// could still be referenced so do not reuse it after passing it to this
// method.
// Deprecated: Use AppendEmpty.
func (es ${structName}) Append(e ${elementName}) {
*es.orig = append(*es.orig, e.orig)
}
// AppendEmpty will append to the end of the slice an empty ${elementName}.
// It returns the newly added ${elementName}.
func (es ${structName}) AppendEmpty() ${elementName} {
*es.orig = append(*es.orig, &${originName}{})
return es.At(es.Len() - 1)
}`

const slicePtrTestTemplate = `func Test${structName}(t *testing.T) {
Expand Down Expand Up @@ -235,9 +243,8 @@ func Test${structName}_Resize(t *testing.T) {
func Test${structName}_Append(t *testing.T) {
es := generateTest${structName}()
emptyVal := new${elementName}(&${originName}{})
es.Append(emptyVal)
assert.EqualValues(t, emptyVal.orig, es.At(7).orig)
es.AppendEmpty()
assert.EqualValues(t, &${originName}{}, es.At(7).orig)
value := generateTest${elementName}()
es.Append(value)
Expand Down Expand Up @@ -364,8 +371,16 @@ func (es ${structName}) Resize(newLen int) {
// given ${elementName} at that new position. The original ${elementName}
// could still be referenced so do not reuse it after passing it to this
// method.
// Deprecated: Use AppendEmpty.
func (es ${structName}) Append(e ${elementName}) {
*es.orig = append(*es.orig, *e.orig)
}
// AppendEmpty will append to the end of the slice an empty ${elementName}.
// It returns the newly added ${elementName}.
func (es ${structName}) AppendEmpty() ${elementName} {
*es.orig = append(*es.orig, ${originName}{})
return es.At(es.Len() - 1)
}`

const sliceValueTestTemplate = `func Test${structName}(t *testing.T) {
Expand Down Expand Up @@ -470,9 +485,8 @@ func Test${structName}_Resize(t *testing.T) {
func Test${structName}_Append(t *testing.T) {
es := generateTest${structName}()
emptyVal := new${elementName}(&${originName}{})
es.Append(emptyVal)
assert.EqualValues(t, emptyVal, es.At(7))
es.AppendEmpty()
assert.EqualValues(t, new${elementName}(&${originName}{}), es.At(7))
value := generateTest${elementName}()
es.Append(value)
Expand Down
6 changes: 3 additions & 3 deletions consumer/pdata/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,12 @@ func TestAttributeValueEqual(t *testing.T) {
assert.False(t, av1.Equal(av2))

av1 = NewAttributeValueArray()
av1.ArrayVal().Append(NewAttributeValueInt(123))
av1.ArrayVal().AppendEmpty().SetIntVal(123)
assert.False(t, av1.Equal(av2))
assert.False(t, av2.Equal(av1))

av2 = NewAttributeValueArray()
av2.ArrayVal().Append(NewAttributeValueDouble(123))
av2.ArrayVal().AppendEmpty().SetDoubleVal(123)
assert.False(t, av1.Equal(av2))

NewAttributeValueInt(123).CopyTo(av2.ArrayVal().At(0))
Expand Down Expand Up @@ -1210,7 +1210,7 @@ func TestAnyValueArrayWithNilValues(t *testing.T) {
assert.EqualValues(t, AttributeValueSTRING, val.Type())
assert.EqualValues(t, "test_value", val.StringVal())

sm.Append(NewAttributeValueString("other_value"))
sm.AppendEmpty().SetStringVal("other_value")
val = sm.At(2)
assert.EqualValues(t, AttributeValueSTRING, val.Type())
assert.EqualValues(t, "other_value", val.StringVal())
Expand Down
8 changes: 8 additions & 0 deletions consumer/pdata/generated_common.go

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

5 changes: 2 additions & 3 deletions consumer/pdata/generated_common_test.go

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

24 changes: 24 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.

15 changes: 6 additions & 9 deletions consumer/pdata/generated_log_test.go

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

Loading

0 comments on commit b8dccfc

Please sign in to comment.