Skip to content

Commit

Permalink
[chore] [pdatagen] Allow slices in the pcommon package (open-telemetr…
Browse files Browse the repository at this point in the history
…y#11474)

Currently, pdatagen doesn't allow defining slices in the common package,
while all other types do allow it. This is needed for
open-telemetry#11282
  • Loading branch information
dmitryax authored Oct 18, 2024
1 parent c90e8d8 commit ba4ab80
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 83 deletions.
28 changes: 14 additions & 14 deletions pdata/internal/cmd/pdatagen/internal/base_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ func (sf *sliceField) templateFields(ms *messageValueStruct) map[string]any {
return ""
}(),
"returnType": sf.returnSlice.getName(),
"origAccessor": origAccessor(ms),
"stateAccessor": stateAccessor(ms),
"origAccessor": origAccessor(ms.packageName),
"stateAccessor": stateAccessor(ms.packageName),
"isCommon": usedByOtherDataTypes(sf.returnSlice.getPackageName()),
"isBaseStructCommon": usedByOtherDataTypes(ms.packageName),
"originFieldName": func() string {
Expand Down Expand Up @@ -394,8 +394,8 @@ func (mf *messageValueField) templateFields(ms *messageValueStruct) map[string]a
}
return ""
}(),
"origAccessor": origAccessor(ms),
"stateAccessor": stateAccessor(ms),
"origAccessor": origAccessor(ms.packageName),
"stateAccessor": stateAccessor(ms.packageName),
}
}

Expand Down Expand Up @@ -448,8 +448,8 @@ func (pf *primitiveField) templateFields(ms *messageValueStruct) map[string]any
"lowerFieldName": strings.ToLower(pf.fieldName),
"testValue": pf.testVal,
"returnType": pf.returnType,
"origAccessor": origAccessor(ms),
"stateAccessor": stateAccessor(ms),
"origAccessor": origAccessor(ms.packageName),
"stateAccessor": stateAccessor(ms.packageName),
"originStructName": ms.originFullName,
"originFieldName": func() string {
if pf.originFieldName == "" {
Expand Down Expand Up @@ -583,8 +583,8 @@ func (psf *primitiveSliceField) templateFields(ms *messageValueStruct) map[strin
"fieldName": psf.fieldName,
"lowerFieldName": strings.ToLower(psf.fieldName),
"testValue": psf.testVal,
"origAccessor": origAccessor(ms),
"stateAccessor": stateAccessor(ms),
"origAccessor": origAccessor(ms.packageName),
"stateAccessor": stateAccessor(ms.packageName),
}
}

Expand Down Expand Up @@ -647,8 +647,8 @@ func (of *oneOfField) templateFields(ms *messageValueStruct) map[string]any {
"typeName": of.typeName,
"originFieldName": of.originFieldName,
"lowerOriginFieldName": strings.ToLower(of.originFieldName),
"origAccessor": origAccessor(ms),
"stateAccessor": stateAccessor(ms),
"origAccessor": origAccessor(ms.packageName),
"stateAccessor": stateAccessor(ms.packageName),
"values": of.values,
"originTypePrefix": ms.originFullName + "_",
}
Expand Down Expand Up @@ -846,15 +846,15 @@ func (opv *optionalPrimitiveValue) templateFields(ms *messageValueStruct) map[st

var _ baseField = (*optionalPrimitiveValue)(nil)

func origAccessor(bs *messageValueStruct) string {
if usedByOtherDataTypes(bs.packageName) {
func origAccessor(packageName string) string {
if usedByOtherDataTypes(packageName) {
return "getOrig()"
}
return "orig"
}

func stateAccessor(bs *messageValueStruct) string {
if usedByOtherDataTypes(bs.packageName) {
func stateAccessor(packageName string) string {
if usedByOtherDataTypes(packageName) {
return "getState()"
}
return "state"
Expand Down
29 changes: 23 additions & 6 deletions pdata/internal/cmd/pdatagen/internal/base_slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,31 @@ func (ss *sliceOfPtrs) generateTests(packageInfo *PackageInfo) []byte {
}

func (ss *sliceOfPtrs) templateFields(packageInfo *PackageInfo) map[string]any {
orig := origAccessor(ss.packageName)
state := stateAccessor(ss.packageName)
return map[string]any{
"type": "sliceOfPtrs",
"isCommon": usedByOtherDataTypes(ss.packageName),
"structName": ss.structName,
"elementName": ss.element.structName,
"originName": ss.element.originFullName,
"originElementType": "*" + ss.element.originFullName,
"emptyOriginElement": "&" + ss.element.originFullName + "{}",
"newElement": "new" + ss.element.structName + "((*es.orig)[i], es.state)",
"newElement": "new" + ss.element.structName + "((*es." + orig + ")[i], es." + state + ")",
"origAccessor": orig,
"stateAccessor": state,
"packageName": packageInfo.name,
"imports": packageInfo.imports,
"testImports": packageInfo.testImports,
}
}

func (ss *sliceOfPtrs) generateInternal(*PackageInfo) []byte {
return nil
func (ss *sliceOfPtrs) generateInternal(packageInfo *PackageInfo) []byte {
var sb bytes.Buffer
if err := sliceInternalTemplate.Execute(&sb, ss.templateFields(packageInfo)); err != nil {
panic(err)
}
return sb.Bytes()
}

var _ baseStruct = (*sliceOfPtrs)(nil)
Expand Down Expand Up @@ -96,22 +105,30 @@ func (ss *sliceOfValues) generateTests(packageInfo *PackageInfo) []byte {
}

func (ss *sliceOfValues) templateFields(packageInfo *PackageInfo) map[string]any {
orig := origAccessor(ss.packageName)
state := stateAccessor(ss.packageName)
return map[string]any{
"type": "sliceOfValues",
"structName": ss.structName,
"elementName": ss.element.structName,
"originName": ss.element.originFullName,
"originElementType": ss.element.originFullName,
"emptyOriginElement": ss.element.originFullName + "{}",
"newElement": "new" + ss.element.structName + "(&(*es.orig)[i], es.state)",
"newElement": "new" + ss.element.structName + "(&(*es." + orig + ")[i], es." + state + ")",
"origAccessor": orig,
"stateAccessor": state,
"packageName": packageInfo.name,
"imports": packageInfo.imports,
"testImports": packageInfo.testImports,
}
}

func (ss *sliceOfValues) generateInternal(*PackageInfo) []byte {
return nil
func (ss *sliceOfValues) generateInternal(packageInfo *PackageInfo) []byte {
var sb bytes.Buffer
if err := sliceInternalTemplate.Execute(&sb, ss.templateFields(packageInfo)); err != nil {
panic(err)
}
return sb.Bytes()
}

var _ baseStruct = (*sliceOfValues)(nil)
19 changes: 7 additions & 12 deletions pdata/internal/cmd/pdatagen/internal/base_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,13 @@ func (ms *messageValueStruct) templateFields(packageInfo *PackageInfo) map[strin
"fields": ms.fields,
"structName": ms.structName,
"originName": ms.originFullName,
"generateTestData": func() string {
if usedByOtherDataTypes(ms.packageName) {
return ms.structName + "(internal.GenerateTest" + ms.structName + "())"
}
return "generateTest" + ms.structName + "()"
}(),
"description": ms.description,
"isCommon": usedByOtherDataTypes(ms.packageName),
"origAccessor": origAccessor(ms),
"packageName": packageInfo.name,
"imports": packageInfo.imports,
"testImports": packageInfo.testImports,
"description": ms.description,
"isCommon": usedByOtherDataTypes(ms.packageName),
"origAccessor": origAccessor(ms.packageName),
"stateAccessor": stateAccessor(ms.packageName),
"packageName": packageInfo.name,
"imports": packageInfo.imports,
"testImports": packageInfo.testImports,
}
}

Expand Down
4 changes: 4 additions & 0 deletions pdata/internal/cmd/pdatagen/internal/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ var (
sliceTemplateBytes []byte
sliceTemplate = parseTemplate("slice.go", sliceTemplateBytes)

//go:embed templates/slice_internal.go.tmpl
sliceInternalTemplateBytes []byte
sliceInternalTemplate = parseTemplate("slice_internal.go", sliceInternalTemplateBytes)

//go:embed templates/slice_test.go.tmpl
sliceTestTemplateBytes []byte
sliceTestTemplate = parseTemplate("slice_test.go", sliceTestTemplateBytes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func New{{ .structName }}() {{ .structName }} {
// MoveTo moves all properties from the current struct overriding the destination and
// resetting the current instance to its zero value
func (ms {{ .structName }}) MoveTo(dest {{ .structName }}) {
ms.{{- if .isCommon }}getState(){{ else }}state{{ end }}.AssertMutable()
dest.{{- if .isCommon }}getState(){{ else }}state{{ end }}.AssertMutable()
ms.{{ .stateAccessor }}.AssertMutable()
dest.{{ .stateAccessor }}.AssertMutable()
*dest.{{ .origAccessor }} = *ms.{{ .origAccessor }}
*ms.{{ .origAccessor }} = {{ .originName }}{}
}
Expand All @@ -70,7 +70,7 @@ func (ms {{ .structName }}) getState() *internal.State {

// CopyTo copies all properties from the current struct overriding the destination.
func (ms {{ .structName }}) CopyTo(dest {{ .structName }}) {
dest.{{- if .isCommon }}getState(){{ else }}state{{ end }}.AssertMutable()
dest.{{ .stateAccessor }}.AssertMutable()
{{- range .fields }}
{{ .GenerateCopyToValue $.messageStruct }}
{{- end }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (
)

func Test{{ .structName }}_MoveTo(t *testing.T) {
ms := {{ .generateTestData }}
ms := generateTest{{ .structName }}()
dest := New{{ .structName }}()
ms.MoveTo(dest)
assert.Equal(t, New{{ .structName }}(), ms)
assert.Equal(t, {{ .generateTestData }}, dest)
assert.Equal(t, generateTest{{ .structName }}(), dest)
sharedState := internal.StateReadOnly
assert.Panics(t, func() { ms.MoveTo(new{{ .structName }}(&{{ .originName }}{}, &sharedState)) })
assert.Panics(t, func() { new{{ .structName }}(&{{ .originName }}{}, &sharedState).MoveTo(dest) })
Expand All @@ -28,7 +28,7 @@ func Test{{ .structName }}_CopyTo(t *testing.T) {
orig := New{{ .structName }}()
orig.CopyTo(ms)
assert.Equal(t, orig, ms)
orig = {{ .generateTestData }}
orig = generateTest{{ .structName }}()
orig.CopyTo(ms)
assert.Equal(t, orig, ms)
sharedState := internal.StateReadOnly
Expand All @@ -39,13 +39,17 @@ func Test{{ .structName }}_CopyTo(t *testing.T) {
{{ .GenerateAccessorsTest $.messageStruct }}
{{ end }}

{{- if not .isCommon }}
func generateTest{{ .structName }}() {{ .structName }} {
{{- if .isCommon }}
return {{ .structName }}(internal.GenerateTest{{ .structName }}())
{{- else }}
tv := New{{ .structName }}()
fillTest{{ .structName }}(tv)
return tv
{{- end }}
}

{{ if not .isCommon -}}
func fillTest{{ .structName }}(tv {{ .structName }}) {
{{- range .fields }}
{{ .GenerateSetWithTestValue $.messageStruct }}
Expand Down
Loading

0 comments on commit ba4ab80

Please sign in to comment.