Skip to content

Commit

Permalink
Add support for slices to non-pointers, use non-nullable AnyValue
Browse files Browse the repository at this point in the history
Before:
```
goos: darwin
goarch: amd64
pkg: go.opentelemetry.io/collector/consumer/pdata
BenchmarkTracesFromOtlp
BenchmarkTracesFromOtlp-16    	     584	   1805875 ns/op	  940352 B/op	   29954 allocs/op
PASS
```

After:
```
goos: darwin
goarch: amd64
pkg: go.opentelemetry.io/collector/consumer/pdata
BenchmarkTracesFromOtlp
BenchmarkTracesFromOtlp-16    	     681	   1575996 ns/op	  857915 B/op	   24802 allocs/op
PASS
```

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu committed Nov 20, 2020
1 parent cc0a999 commit 1376e55
Show file tree
Hide file tree
Showing 30 changed files with 981 additions and 731 deletions.
62 changes: 55 additions & 7 deletions cmd/pdatagen/internal/base_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ type baseField interface {
generateCopyToValue(sb *strings.Builder)
}

type sliceField struct {
type slicePtrField struct {
fieldName string
originFieldName string
returnSlice *sliceStruct
returnSlice *slicePtrStruct
}

func (sf *sliceField) generateAccessors(ms baseStruct, sb *strings.Builder) {
func (sf *slicePtrField) generateAccessors(ms baseStruct, sb *strings.Builder) {
sb.WriteString(os.Expand(accessorSliceTemplate, func(name string) string {
switch name {
case "structName":
Expand All @@ -146,7 +146,7 @@ func (sf *sliceField) generateAccessors(ms baseStruct, sb *strings.Builder) {
}))
}

func (sf *sliceField) generateAccessorsTest(ms baseStruct, sb *strings.Builder) {
func (sf *slicePtrField) generateAccessorsTest(ms baseStruct, sb *strings.Builder) {
sb.WriteString(os.Expand(accessorsSliceTestTemplate, func(name string) string {
switch name {
case "structName":
Expand All @@ -161,15 +161,63 @@ func (sf *sliceField) generateAccessorsTest(ms baseStruct, sb *strings.Builder)
}))
}

func (sf *sliceField) generateSetWithTestValue(sb *strings.Builder) {
func (sf *slicePtrField) generateSetWithTestValue(sb *strings.Builder) {
sb.WriteString("\tfillTest" + sf.returnSlice.structName + "(tv." + sf.fieldName + "())")
}

func (sf *sliceField) generateCopyToValue(sb *strings.Builder) {
func (sf *slicePtrField) generateCopyToValue(sb *strings.Builder) {
sb.WriteString("\tms." + sf.fieldName + "().CopyTo(dest." + sf.fieldName + "())")
}

var _ baseField = (*sliceField)(nil)
var _ baseField = (*slicePtrField)(nil)

type sliceValueField struct {
fieldName string
originFieldName string
returnSlice *slicePtrStruct
}

func (sf *sliceValueField) generateAccessors(ms baseStruct, sb *strings.Builder) {
sb.WriteString(os.Expand(accessorSliceTemplate, func(name string) string {
switch name {
case "structName":
return ms.getName()
case "fieldName":
return sf.fieldName
case "returnType":
return sf.returnSlice.structName
case "originFieldName":
return sf.originFieldName
default:
panic(name)
}
}))
}

func (sf *sliceValueField) generateAccessorsTest(ms baseStruct, sb *strings.Builder) {
sb.WriteString(os.Expand(accessorsSliceTestTemplate, func(name string) string {
switch name {
case "structName":
return ms.getName()
case "fieldName":
return sf.fieldName
case "returnType":
return sf.returnSlice.structName
default:
panic(name)
}
}))
}

func (sf *sliceValueField) generateSetWithTestValue(sb *strings.Builder) {
sb.WriteString("\tfillTest" + sf.returnSlice.structName + "(tv." + sf.fieldName + "())")
}

func (sf *sliceValueField) generateCopyToValue(sb *strings.Builder) {
sb.WriteString("\tms." + sf.fieldName + "().CopyTo(dest." + sf.fieldName + "())")
}

var _ baseField = (*sliceValueField)(nil)

type messagePtrField struct {
fieldName string
Expand Down
Loading

0 comments on commit 1376e55

Please sign in to comment.