Skip to content

Commit

Permalink
[pdata] Simplify pdatagen to rely more on the proto naming dependency (
Browse files Browse the repository at this point in the history
…#6283)

Given the documented naming recommendations for pdata, simplify pdatagen to encourage the same naming between pdata and proto fields.
  • Loading branch information
dmitryax authored Oct 11, 2022
1 parent 151b896 commit 806ba4c
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 233 deletions.
81 changes: 35 additions & 46 deletions pdata/internal/cmd/pdatagen/internal/base_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (
"strings"
)

const accessorSliceTemplate = `// ${fieldName} returns the ${originFieldName} associated with this ${structName}.
const accessorSliceTemplate = `// ${fieldName} returns the ${fieldName} associated with this ${structName}.
func (ms ${structName}) ${fieldName}() ${packageName}${returnType} {
return ${packageName}${returnType}(internal.New${returnType}(&ms.getOrig().${originFieldName}))
return ${packageName}${returnType}(internal.New${returnType}(&ms.getOrig().${fieldName}))
}`

const accessorsSliceTestTemplate = `func Test${structName}_${fieldName}(t *testing.T) {
Expand All @@ -33,7 +33,7 @@ const accessorsSliceTestTemplate = `func Test${structName}_${fieldName}(t *testi

const accessorsMessageValueTemplate = `// ${fieldName} returns the ${lowerFieldName} associated with this ${structName}.
func (ms ${structName}) ${fieldName}() ${packageName}${returnType} {
return ${packageName}${returnType}(internal.New${returnType}(&ms.getOrig().${originFieldName}))
return ${packageName}${returnType}(internal.New${returnType}(&ms.getOrig().${fieldName}))
}`

const accessorsMessageValueTestTemplate = `func Test${structName}_${fieldName}(t *testing.T) {
Expand All @@ -44,17 +44,17 @@ const accessorsMessageValueTestTemplate = `func Test${structName}_${fieldName}(t

const accessorsPrimitiveTemplate = `// ${fieldName} returns the ${lowerFieldName} associated with this ${structName}.
func (ms ${structName}) ${fieldName}() ${packageName}${returnType} {
return ms.getOrig().${originFieldName}
return ms.getOrig().${fieldName}
}
// Set${fieldName} replaces the ${lowerFieldName} associated with this ${structName}.
func (ms ${structName}) Set${fieldName}(v ${returnType}) {
ms.getOrig().${originFieldName} = v
ms.getOrig().${fieldName} = v
}`

const accessorsPrimitiveSliceTemplate = `// ${fieldName} returns the ${lowerFieldName} associated with this ${structName}.
func (ms ${structName}) ${fieldName}() ${packageName}${returnType} {
return ${packageName}${returnType}(internal.New${returnType}(&ms.getOrig().${originFieldName}))
return ${packageName}${returnType}(internal.New${returnType}(&ms.getOrig().${fieldName}))
}`

const oneOfTypeAccessorHeaderTemplate = `// ${typeFuncName} returns the type of the ${lowerOriginFieldName} for this ${structName}.
Expand All @@ -79,7 +79,7 @@ func (ms ${structName}) ${fieldName}() ${returnType} {
if !ok {
return ${returnType}{}
}
return new${returnType}(v.${originFieldName})
return new${returnType}(v.${fieldName})
}
// SetEmpty${fieldName} sets an empty ${lowerFieldName} to this ${structName}.
Expand All @@ -88,8 +88,8 @@ func (ms ${structName}) ${fieldName}() ${returnType} {
//
// Calling this function on zero-initialized ${structName} will cause a panic.
func (ms ${structName}) SetEmpty${fieldName}() ${returnType} {
val := &${originFieldPackageName}.${originFieldName}{}
ms.getOrig().${originOneOfFieldName} = &${originStructType}{${originFieldName}: val}
val := &${originFieldPackageName}.${fieldName}{}
ms.getOrig().${originOneOfFieldName} = &${originStructType}{${fieldName}: val}
return new${returnType}(val)
}`

Expand Down Expand Up @@ -188,9 +188,8 @@ type baseField interface {
}

type sliceField struct {
fieldName string
originFieldName string
returnSlice baseSlice
fieldName string
returnSlice baseSlice
}

func (sf *sliceField) generateAccessors(ms baseStruct, sb *strings.Builder) {
Expand All @@ -207,8 +206,6 @@ func (sf *sliceField) generateAccessors(ms baseStruct, sb *strings.Builder) {
return ""
case "returnType":
return sf.returnSlice.getName()
case "originFieldName":
return sf.originFieldName
default:
panic(name)
}
Expand Down Expand Up @@ -236,7 +233,7 @@ func (sf *sliceField) generateAccessorsTest(ms baseStruct, sb *strings.Builder)
}

func (sf *sliceField) generateSetWithTestValue(sb *strings.Builder) {
sb.WriteString("\tFillTest" + sf.returnSlice.getName() + "(New" + sf.returnSlice.getName() + "(&tv.orig." + sf.originFieldName + "))")
sb.WriteString("\tFillTest" + sf.returnSlice.getName() + "(New" + sf.returnSlice.getName() + "(&tv.orig." + sf.fieldName + "))")
}

func (sf *sliceField) generateCopyToValue(_ baseStruct, sb *strings.Builder) {
Expand All @@ -246,9 +243,8 @@ func (sf *sliceField) generateCopyToValue(_ baseStruct, sb *strings.Builder) {
var _ baseField = (*sliceField)(nil)

type messageValueField struct {
fieldName string
originFieldName string
returnMessage baseStruct
fieldName string
returnMessage baseStruct
}

func (mf *messageValueField) generateAccessors(ms baseStruct, sb *strings.Builder) {
Expand All @@ -267,8 +263,6 @@ func (mf *messageValueField) generateAccessors(ms baseStruct, sb *strings.Builde
return ""
case "returnType":
return mf.returnMessage.getName()
case "originFieldName":
return mf.originFieldName
default:
panic(name)
}
Expand Down Expand Up @@ -296,7 +290,7 @@ func (mf *messageValueField) generateAccessorsTest(ms baseStruct, sb *strings.Bu
}

func (mf *messageValueField) generateSetWithTestValue(sb *strings.Builder) {
sb.WriteString("\tFillTest" + mf.returnMessage.getName() + "(New" + mf.returnMessage.getName() + "(&tv.orig." + mf.originFieldName + "))")
sb.WriteString("\tFillTest" + mf.returnMessage.getName() + "(New" + mf.returnMessage.getName() + "(&tv.orig." + mf.fieldName + "))")
}

func (mf *messageValueField) generateCopyToValue(_ baseStruct, sb *strings.Builder) {
Expand All @@ -306,11 +300,10 @@ func (mf *messageValueField) generateCopyToValue(_ baseStruct, sb *strings.Build
var _ baseField = (*messageValueField)(nil)

type primitiveField struct {
fieldName string
originFieldName string
returnType string
defaultVal string
testVal string
fieldName string
returnType string
defaultVal string
testVal string
}

func (pf *primitiveField) generateAccessors(ms baseStruct, sb *strings.Builder) {
Expand All @@ -326,8 +319,6 @@ func (pf *primitiveField) generateAccessors(ms baseStruct, sb *strings.Builder)
return strings.ToLower(pf.fieldName)
case "returnType":
return pf.returnType
case "originFieldName":
return pf.originFieldName
default:
panic(name)
}
Expand All @@ -354,7 +345,7 @@ func (pf *primitiveField) generateAccessorsTest(ms baseStruct, sb *strings.Build
}

func (pf *primitiveField) generateSetWithTestValue(sb *strings.Builder) {
sb.WriteString("\ttv.orig." + pf.originFieldName + " = " + pf.testVal)
sb.WriteString("\ttv.orig." + pf.fieldName + " = " + pf.testVal)
}

func (pf *primitiveField) generateCopyToValue(_ baseStruct, sb *strings.Builder) {
Expand Down Expand Up @@ -397,6 +388,9 @@ func (ptf *primitiveTypedField) generateAccessors(ms baseStruct, sb *strings.Bui
case "rawType":
return ptf.returnType.rawType
case "originFieldName":
if ptf.originFieldName == "" {
return ptf.fieldName
}
return ptf.originFieldName
default:
panic(name)
Expand Down Expand Up @@ -429,7 +423,11 @@ func (ptf *primitiveTypedField) generateAccessorsTest(ms baseStruct, sb *strings
}

func (ptf *primitiveTypedField) generateSetWithTestValue(sb *strings.Builder) {
sb.WriteString("\ttv.orig." + ptf.originFieldName + " = " + ptf.returnType.testVal)
originFieldName := ptf.fieldName
if ptf.originFieldName != "" {
originFieldName = ptf.originFieldName
}
sb.WriteString("\ttv.orig." + originFieldName + " = " + ptf.returnType.testVal)
}

func (ptf *primitiveTypedField) generateCopyToValue(_ baseStruct, sb *strings.Builder) {
Expand All @@ -441,7 +439,6 @@ var _ baseField = (*primitiveTypedField)(nil)
// primitiveSliceField is used to generate fields for slice of primitive types
type primitiveSliceField struct {
fieldName string
originFieldName string
returnPackageName string
returnType string
defaultVal string
Expand All @@ -465,8 +462,6 @@ func (psf *primitiveSliceField) generateAccessors(ms baseStruct, sb *strings.Bui
return psf.returnPackageName + "."
}
return ""
case "originFieldName":
return psf.originFieldName
default:
panic(name)
}
Expand Down Expand Up @@ -498,7 +493,7 @@ func (psf *primitiveSliceField) generateAccessorsTest(ms baseStruct, sb *strings
}

func (psf *primitiveSliceField) generateSetWithTestValue(sb *strings.Builder) {
sb.WriteString("\ttv.orig." + psf.originFieldName + " = " + psf.testVal)
sb.WriteString("\ttv.orig." + psf.fieldName + " = " + psf.testVal)
}

func (psf *primitiveSliceField) generateCopyToValue(ms baseStruct, sb *strings.Builder) {
Expand Down Expand Up @@ -682,7 +677,6 @@ var _ oneOfValue = (*oneOfPrimitiveValue)(nil)

type oneOfMessageValue struct {
fieldName string
originFieldName string
originFieldPackageName string
returnMessage *messageValueStruct
}
Expand All @@ -694,16 +688,14 @@ func (omv *oneOfMessageValue) generateAccessors(ms baseStruct, of *oneOfField, s
return omv.fieldName
case "lowerFieldName":
return strings.ToLower(omv.fieldName)
case "originFieldName":
return omv.originFieldName
case "originOneOfTypeFuncName":
return of.typeFuncName()
case "originOneOfFieldName":
return of.originFieldName
case "originFieldPackageName":
return omv.originFieldPackageName
case "originStructType":
return of.originTypePrefix + omv.originFieldName
return of.originTypePrefix + omv.fieldName
case "returnType":
return omv.returnMessage.structName
case "structName":
Expand Down Expand Up @@ -738,8 +730,8 @@ func (omv *oneOfMessageValue) generateTests(ms baseStruct, of *oneOfField, sb *s
}

func (omv *oneOfMessageValue) generateSetWithTestValue(of *oneOfField, sb *strings.Builder) {
sb.WriteString("\ttv.orig." + of.originFieldName + " = &" + of.originTypePrefix + omv.originFieldName + "{" + omv.originFieldName + ": &" + omv.originFieldPackageName + "." + omv.originFieldName + "{}}\n")
sb.WriteString("\tFillTest" + omv.returnMessage.structName + "(New" + omv.fieldName + "(tv.orig.Get" + omv.originFieldName + "()))")
sb.WriteString("\ttv.orig." + of.originFieldName + " = &" + of.originTypePrefix + omv.fieldName + "{" + omv.fieldName + ": &" + omv.originFieldPackageName + "." + omv.fieldName + "{}}\n")
sb.WriteString("\tFillTest" + omv.returnMessage.structName + "(New" + omv.fieldName + "(tv.orig.Get" + omv.fieldName + "()))")
}

func (omv *oneOfMessageValue) generateCopyToValue(of *oneOfField, sb *strings.Builder) {
Expand All @@ -759,7 +751,7 @@ func (omv *oneOfMessageValue) generateCopyToValue(of *oneOfField, sb *strings.Bu
}

func (omv *oneOfMessageValue) generateTypeSwitchCase(of *oneOfField, sb *strings.Builder) {
sb.WriteString("\tcase *" + of.originTypePrefix + omv.originFieldName + ":\n")
sb.WriteString("\tcase *" + of.originTypePrefix + omv.fieldName + ":\n")
sb.WriteString("\t\treturn " + of.typeName + omv.fieldName + "\n")
}

Expand All @@ -770,7 +762,6 @@ type optionalPrimitiveValue struct {
defaultVal string
testVal string
returnType string
originFieldName string
originTypePrefix string
}

Expand All @@ -785,10 +776,8 @@ func (opv *optionalPrimitiveValue) generateAccessors(ms baseStruct, sb *strings.
return strings.ToLower(opv.fieldName)
case "returnType":
return opv.returnType
case "originFieldName":
return opv.originFieldName
case "originStructType":
return opv.originTypePrefix + opv.originFieldName
return opv.originTypePrefix + opv.fieldName
default:
panic(name)
}
Expand Down Expand Up @@ -817,7 +806,7 @@ func (opv *optionalPrimitiveValue) generateAccessorsTest(ms baseStruct, sb *stri
}

func (opv *optionalPrimitiveValue) generateSetWithTestValue(sb *strings.Builder) {
sb.WriteString("\ttv.orig." + opv.originFieldName + "_ = &" + opv.originTypePrefix + opv.originFieldName + "{" + opv.originFieldName + ":" + opv.testVal + "}")
sb.WriteString("\ttv.orig." + opv.fieldName + "_ = &" + opv.originTypePrefix + opv.fieldName + "{" + opv.fieldName + ":" + opv.testVal + "}")
}

func (opv *optionalPrimitiveValue) generateCopyToValue(_ baseStruct, sb *strings.Builder) {
Expand Down
37 changes: 16 additions & 21 deletions pdata/internal/cmd/pdatagen/internal/common_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ var scope = &messageValueStruct{
fields: []baseField{
nameField,
&primitiveField{
fieldName: "Version",
originFieldName: "Version",
returnType: "string",
defaultVal: `""`,
testVal: `"test_version"`,
fieldName: "Version",
returnType: "string",
defaultVal: `""`,
testVal: `"test_version"`,
},
attributes,
droppedAttributesCount,
Expand All @@ -67,9 +66,8 @@ var attributeKeyValue = &messageValueStruct{
}

var scopeField = &messageValueField{
fieldName: "Scope",
originFieldName: "Scope",
returnMessage: scope,
fieldName: "Scope",
returnMessage: scope,
}

var traceState = &messageValueStruct{
Expand Down Expand Up @@ -104,17 +102,15 @@ var endTimeField = &primitiveTypedField{
}

var attributes = &sliceField{
fieldName: "Attributes",
originFieldName: "Attributes",
returnSlice: mapStruct,
fieldName: "Attributes",
returnSlice: mapStruct,
}

var nameField = &primitiveField{
fieldName: "Name",
originFieldName: "Name",
returnType: "string",
defaultVal: `""`,
testVal: `"test_name"`,
fieldName: "Name",
returnType: "string",
defaultVal: `""`,
testVal: `"test_name"`,
}

var anyValue = &messageValueStruct{
Expand Down Expand Up @@ -163,9 +159,8 @@ var spanIDType = &primitiveType{
}

var schemaURLField = &primitiveField{
fieldName: "SchemaUrl",
originFieldName: "SchemaUrl",
returnType: "string",
defaultVal: `""`,
testVal: `"https://opentelemetry.io/schemas/1.5.0"`,
fieldName: "SchemaUrl",
returnType: "string",
defaultVal: `""`,
testVal: `"https://opentelemetry.io/schemas/1.5.0"`,
}
Loading

0 comments on commit 806ba4c

Please sign in to comment.