Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AppendEmpty and deprecate Append for slices #2970

Merged
merged 1 commit into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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