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 stringSlice and int64Slice to pcommon, and let tests handle non-number slices #10148

Merged
merged 6 commits into from
May 21, 2024
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
25 changes: 25 additions & 0 deletions .chloggen/pcommon-string-int64-slices.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: pdata

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Introduce string and int64 slices to pcommon

# One or more tracking issues or pull requests related to the change
issues: [10148]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]
52 changes: 43 additions & 9 deletions pdata/internal/cmd/pdatagen/internal/pcommon_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ var pcommon = &Package{
byteSlice,
float64Slice,
uInt64Slice,
int64Slice,
stringSlice,
},
}

Expand Down Expand Up @@ -164,19 +166,51 @@ var resourceField = &messageValueField{
}

var byteSlice = &primitiveSliceStruct{
structName: "ByteSlice",
packageName: "pcommon",
itemType: "byte",
structName: "ByteSlice",
packageName: "pcommon",
itemType: "byte",
testOrigVal: "1, 2, 3",
testInterfaceOrigVal: []interface{}{1, 2, 3},
testSetVal: "5",
testNewVal: "1, 5, 3",
}

var float64Slice = &primitiveSliceStruct{
structName: "Float64Slice",
packageName: "pcommon",
itemType: "float64",
structName: "Float64Slice",
packageName: "pcommon",
itemType: "float64",
testOrigVal: "1, 2, 3",
testInterfaceOrigVal: []interface{}{1, 2, 3},
testSetVal: "5",
testNewVal: "1, 5, 3",
}

var uInt64Slice = &primitiveSliceStruct{
structName: "UInt64Slice",
packageName: "pcommon",
itemType: "uint64",
structName: "UInt64Slice",
packageName: "pcommon",
itemType: "uint64",
testOrigVal: "1, 2, 3",
testInterfaceOrigVal: []interface{}{1, 2, 3},
testSetVal: "5",
testNewVal: "1, 5, 3",
}

var int64Slice = &primitiveSliceStruct{
structName: "Int64Slice",
packageName: "pcommon",
itemType: "int64",
testOrigVal: "1, 2, 3",
testInterfaceOrigVal: []interface{}{1, 2, 3},
testSetVal: "5",
testNewVal: "1, 5, 3",
}

var stringSlice = &primitiveSliceStruct{
structName: "StringSlice",
packageName: "pcommon",
itemType: "string",
testOrigVal: `"a", "b", "c"`,
testInterfaceOrigVal: []interface{}{`"a"`, `"b"`, `"c"`},
testSetVal: `"d"`,
testNewVal: `"a", "d", "c"`,
}
73 changes: 46 additions & 27 deletions pdata/internal/cmd/pdatagen/internal/primitive_slice_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ func (ms {{ .structName }}) EnsureCapacity(newCap int) {
}

// Append appends extra elements to {{ .structName }}.
// Equivalent of {{ .lowerStructName }} = append({{ .lowerStructName }}, elms...)
// Equivalent of {{ .lowerStructName }} = append({{ .lowerStructName }}, elms...)
func (ms {{ .structName }}) Append(elms ...{{ .itemType }}) {
ms.getState().AssertMutable()
*ms.getOrig() = append(*ms.getOrig(), elms...)
}

// MoveTo moves all elements from the current slice overriding the destination and
// MoveTo moves all elements from the current slice overriding the destination and
// resetting the current instance to its zero value.
func (ms {{ .structName }}) MoveTo(dest {{ .structName }}) {
ms.getState().AssertMutable()
Expand All @@ -109,42 +109,42 @@ func copy{{ .structName }}(dst, src []{{ .itemType }}) []{{ .itemType }} {
const immutableSliceTestTemplate = `func TestNew{{ .structName }}(t *testing.T) {
ms := New{{ .structName }}()
assert.Equal(t, 0, ms.Len())
ms.FromRaw([]{{ .itemType }}{1, 2, 3})
ms.FromRaw([]{{ .itemType }}{ {{ .testOrigVal }} })
assert.Equal(t, 3, ms.Len())
assert.Equal(t, []{{ .itemType }}{1, 2, 3}, ms.AsRaw())
ms.SetAt(1, {{ .itemType }}(5))
assert.Equal(t, []{{ .itemType }}{1, 5, 3}, ms.AsRaw())
ms.FromRaw([]{{ .itemType }}{3})
assert.Equal(t, []{{ .itemType }}{ {{ .testOrigVal }} }, ms.AsRaw())
ms.SetAt(1, {{ .itemType }}( {{ .testSetVal }} ))
assert.Equal(t, []{{ .itemType }}{ {{ .testNewVal }} }, ms.AsRaw())
ms.FromRaw([]{{ .itemType }}{ {{ index .testInterfaceOrigVal 2 }} })
assert.Equal(t, 1, ms.Len())
assert.Equal(t, {{ .itemType }}(3), ms.At(0))
assert.Equal(t, {{ .itemType }}( {{ index .testInterfaceOrigVal 2 }} ), ms.At(0))

cp := New{{ .structName }}()
ms.CopyTo(cp)
ms.SetAt(0, {{ .itemType }}(2))
assert.Equal(t, {{ .itemType }}(2), ms.At(0))
assert.Equal(t, {{ .itemType }}(3), cp.At(0))
ms.SetAt(0, {{ .itemType }}( {{ index .testInterfaceOrigVal 1 }} ))
assert.Equal(t, {{ .itemType }}( {{ index .testInterfaceOrigVal 1 }} ), ms.At(0))
assert.Equal(t, {{ .itemType }}({{ index .testInterfaceOrigVal 2 }}), cp.At(0))
ms.CopyTo(cp)
assert.Equal(t, {{ .itemType }}(2), cp.At(0))
assert.Equal(t, {{ .itemType }}({{ index .testInterfaceOrigVal 1 }}), cp.At(0))

mv := New{{ .structName }}()
ms.MoveTo(mv)
assert.Equal(t, 0, ms.Len())
assert.Equal(t, 1, mv.Len())
assert.Equal(t, {{ .itemType }}(2), mv.At(0))
ms.FromRaw([]{{ .itemType }}{1, 2, 3})
assert.Equal(t, {{ .itemType }}({{ index .testInterfaceOrigVal 1 }}), mv.At(0))
ms.FromRaw([]{{ .itemType }}{ {{ .testOrigVal }} })
ms.MoveTo(mv)
assert.Equal(t, 3, mv.Len())
assert.Equal(t, {{ .itemType }}(1), mv.At(0))
assert.Equal(t, {{ .itemType }}({{ index .testInterfaceOrigVal 0 }}), mv.At(0))
}

func Test{{ .structName }}ReadOnly(t *testing.T) {
raw := []{{ .itemType }}{1, 2, 3}
raw := []{{ .itemType }}{ {{ .testOrigVal }}}
state := internal.StateReadOnly
ms := {{ .structName }}(internal.New{{ .structName }}(&raw, &state))

assert.Equal(t, 3, ms.Len())
assert.Equal(t, {{ .itemType }}(1), ms.At(0))
assert.Panics(t, func() { ms.Append(1) })
assert.Equal(t, {{ .itemType }}({{ index .testInterfaceOrigVal 0 }}), ms.At(0))
assert.Panics(t, func() { ms.Append({{ index .testInterfaceOrigVal 0 }}) })
assert.Panics(t, func() { ms.EnsureCapacity(2) })
assert.Equal(t, raw, ms.AsRaw())
assert.Panics(t, func() { ms.FromRaw(raw) })
Expand All @@ -153,17 +153,17 @@ func Test{{ .structName }}ReadOnly(t *testing.T) {
ms.CopyTo(ms2)
assert.Equal(t, ms.AsRaw(), ms2.AsRaw())
assert.Panics(t, func() { ms2.CopyTo(ms) })

assert.Panics(t, func() { ms.MoveTo(ms2) })
assert.Panics(t, func() { ms2.MoveTo(ms) })
}

func Test{{ .structName }}Append(t *testing.T) {
ms := New{{ .structName }}()
ms.FromRaw([]{{ .itemType }}{1, 2, 3})
ms.Append(4, 5)
ms.FromRaw([]{{ .itemType }}{ {{ .testOrigVal }} })
ms.Append({{ .testSetVal }}, {{ .testSetVal }})
assert.Equal(t, 5, ms.Len())
assert.Equal(t, {{ .itemType }}(5), ms.At(4))
assert.Equal(t, {{ .itemType }}({{ .testSetVal }}), ms.At(4))
}

func Test{{ .structName }}EnsureCapacity(t *testing.T) {
Expand All @@ -190,6 +190,16 @@ func Get{{ .structName }}State(ms {{ .structName }}) *State {

func New{{ .structName }}(orig *[]{{ .itemType }}, state *State) {{ .structName }} {
return {{ .structName }}{orig: orig, state: state}
}

func FillTest{{ .structName }}(tv {{ .structName}}) {
}

func GenerateTest{{ .structName }}() {{ .structName }} {
state := StateMutable
var orig []{{ .itemType }} = nil

return {{ .structName }}{&orig, &state}
}`

// primitiveSliceStruct generates a struct for a slice of primitive value elements. The structs are always generated
Expand All @@ -198,6 +208,11 @@ type primitiveSliceStruct struct {
structName string
packageName string
itemType string

testOrigVal string
testInterfaceOrigVal []interface{}
testSetVal string
testNewVal string
}

func (iss *primitiveSliceStruct) getName() string {
Expand Down Expand Up @@ -233,8 +248,12 @@ func (iss *primitiveSliceStruct) generateInternal(sb *bytes.Buffer) {

func (iss *primitiveSliceStruct) templateFields() map[string]any {
return map[string]any{
"structName": iss.structName,
"itemType": iss.itemType,
"lowerStructName": strings.ToLower(iss.structName[:1]) + iss.structName[1:],
"structName": iss.structName,
"itemType": iss.itemType,
"lowerStructName": strings.ToLower(iss.structName[:1]) + iss.structName[1:],
"testOrigVal": iss.testOrigVal,
"testInterfaceOrigVal": iss.testInterfaceOrigVal,
"testSetVal": iss.testSetVal,
"testNewVal": iss.testNewVal,
}
}
10 changes: 10 additions & 0 deletions pdata/internal/generated_wrapper_byteslice.go

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

10 changes: 10 additions & 0 deletions pdata/internal/generated_wrapper_float64slice.go

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

34 changes: 34 additions & 0 deletions pdata/internal/generated_wrapper_int64slice.go

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

34 changes: 34 additions & 0 deletions pdata/internal/generated_wrapper_stringslice.go

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

10 changes: 10 additions & 0 deletions pdata/internal/generated_wrapper_uint64slice.go

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

2 changes: 1 addition & 1 deletion pdata/pcommon/generated_byteslice_test.go

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

2 changes: 1 addition & 1 deletion pdata/pcommon/generated_float64slice_test.go

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

Loading
Loading