Skip to content

Commit

Permalink
pdata: add [Type]Slice.Sort(func) to sort slices (#3671)
Browse files Browse the repository at this point in the history
* pdata: add TypeSlice.Sort(func) to sort slices

Comparing two generated slices can be tedious when the slices are not
ordered, and order does not matter for the test.

This change adds a .Sort(func(i, j int) bool) method to generated [Type]Slice. This is
similar to method StringMap.Sort(), but takes a less function because
the order may differ by context, where the sort order of a map is obvious (by key).

* fix: improve TypeSlice.Sort(less) signature

* fix: resolve goimports CI check

* chore: make genpdata
  • Loading branch information
jacobmarble committed Jul 22, 2021
1 parent 4c92ef4 commit 6814536
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 3 deletions.
17 changes: 16 additions & 1 deletion cmd/pdatagen/internal/base_slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,22 @@ func (es ${structName}) EnsureCapacity(newCap int) {
func (es ${structName}) AppendEmpty() ${elementName} {
*es.orig = append(*es.orig, &${originName}{})
return es.At(es.Len() - 1)
} `
}
// Sort sorts the ${elementName} elements within ${structName} given the
// provided less function so that two instances of ${structName}
// can be compared.
//
// Returns the same instance to allow nicer code like:
// lessFunc := func(a, b ${elementName}) bool {
// return a.Name() < b.Name() // choose any comparison here
// }
// assert.EqualValues(t, expected.Sort(lessFunc), actual.Sort(lessFunc))
func (es ${structName}) Sort(less func(a, b ${elementName}) bool) ${structName} {
sort.SliceStable(*es.orig, func(i, j int) bool { return less(es.At(i), es.At(j)) })
return es
}
`

const slicePtrTestTemplate = `func Test${structName}(t *testing.T) {
es := New${structName}()
Expand Down
8 changes: 6 additions & 2 deletions cmd/pdatagen/internal/files.go

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

2 changes: 2 additions & 0 deletions cmd/pdatagen/internal/log_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package internal
var logFile = &File{
Name: "log",
imports: []string{
`"sort"`,
``,
`otlplogs "go.opentelemetry.io/collector/model/internal/data/protogen/logs/v1"`,
},
testImports: []string{
Expand Down
2 changes: 2 additions & 0 deletions cmd/pdatagen/internal/metrics_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package internal
var metricsFile = &File{
Name: "metrics",
imports: []string{
`"sort"`,
``,
`otlpmetrics "go.opentelemetry.io/collector/model/internal/data/protogen/metrics/v1"`,
},
testImports: []string{
Expand Down
2 changes: 2 additions & 0 deletions cmd/pdatagen/internal/trace_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package internal
var traceFile = &File{
Name: "trace",
imports: []string{
`"sort"`,
``,
`otlptrace "go.opentelemetry.io/collector/model/internal/data/protogen/trace/v1"`,
},
testImports: []string{
Expand Down
44 changes: 44 additions & 0 deletions model/pdata/generated_log.go

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

114 changes: 114 additions & 0 deletions model/pdata/generated_metrics.go

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

0 comments on commit 6814536

Please sign in to comment.