diff --git a/pdata/internal/cmd/pdatagen/internal/base_fields.go b/pdata/internal/cmd/pdatagen/internal/base_fields.go index a0bef0bd977f..4fa86dc0e48e 100644 --- a/pdata/internal/cmd/pdatagen/internal/base_fields.go +++ b/pdata/internal/cmd/pdatagen/internal/base_fields.go @@ -15,6 +15,7 @@ package internal // import "go.opentelemetry.io/collector/pdata/internal/cmd/pdatagen/internal" import ( + "bytes" "os" "strings" ) @@ -178,13 +179,13 @@ func (ms ${structName}) Set${fieldName}(v ${returnType}) { }` type baseField interface { - generateAccessors(ms baseStruct, sb *strings.Builder) + generateAccessors(ms baseStruct, sb *bytes.Buffer) - generateAccessorsTest(ms baseStruct, sb *strings.Builder) + generateAccessorsTest(ms baseStruct, sb *bytes.Buffer) - generateSetWithTestValue(sb *strings.Builder) + generateSetWithTestValue(sb *bytes.Buffer) - generateCopyToValue(ms baseStruct, sb *strings.Builder) + generateCopyToValue(ms baseStruct, sb *bytes.Buffer) } type sliceField struct { @@ -192,7 +193,7 @@ type sliceField struct { returnSlice baseSlice } -func (sf *sliceField) generateAccessors(ms baseStruct, sb *strings.Builder) { +func (sf *sliceField) generateAccessors(ms baseStruct, sb *bytes.Buffer) { sb.WriteString(os.Expand(accessorSliceTemplate, func(name string) string { switch name { case "structName": @@ -212,7 +213,7 @@ func (sf *sliceField) generateAccessors(ms baseStruct, sb *strings.Builder) { })) } -func (sf *sliceField) generateAccessorsTest(ms baseStruct, sb *strings.Builder) { +func (sf *sliceField) generateAccessorsTest(ms baseStruct, sb *bytes.Buffer) { sb.WriteString(os.Expand(accessorsSliceTestTemplate, func(name string) string { switch name { case "structName": @@ -232,11 +233,11 @@ func (sf *sliceField) generateAccessorsTest(ms baseStruct, sb *strings.Builder) })) } -func (sf *sliceField) generateSetWithTestValue(sb *strings.Builder) { +func (sf *sliceField) generateSetWithTestValue(sb *bytes.Buffer) { sb.WriteString("\tFillTest" + sf.returnSlice.getName() + "(New" + sf.returnSlice.getName() + "(&tv.orig." + sf.fieldName + "))") } -func (sf *sliceField) generateCopyToValue(_ baseStruct, sb *strings.Builder) { +func (sf *sliceField) generateCopyToValue(_ baseStruct, sb *bytes.Buffer) { sb.WriteString("\tms." + sf.fieldName + "().CopyTo(dest." + sf.fieldName + "())") } @@ -247,7 +248,7 @@ type messageValueField struct { returnMessage baseStruct } -func (mf *messageValueField) generateAccessors(ms baseStruct, sb *strings.Builder) { +func (mf *messageValueField) generateAccessors(ms baseStruct, sb *bytes.Buffer) { sb.WriteString(os.Expand(accessorsMessageValueTemplate, func(name string) string { switch name { case "structName": @@ -269,7 +270,7 @@ func (mf *messageValueField) generateAccessors(ms baseStruct, sb *strings.Builde })) } -func (mf *messageValueField) generateAccessorsTest(ms baseStruct, sb *strings.Builder) { +func (mf *messageValueField) generateAccessorsTest(ms baseStruct, sb *bytes.Buffer) { sb.WriteString(os.Expand(accessorsMessageValueTestTemplate, func(name string) string { switch name { case "structName": @@ -289,11 +290,11 @@ func (mf *messageValueField) generateAccessorsTest(ms baseStruct, sb *strings.Bu })) } -func (mf *messageValueField) generateSetWithTestValue(sb *strings.Builder) { +func (mf *messageValueField) generateSetWithTestValue(sb *bytes.Buffer) { sb.WriteString("\tFillTest" + mf.returnMessage.getName() + "(New" + mf.returnMessage.getName() + "(&tv.orig." + mf.fieldName + "))") } -func (mf *messageValueField) generateCopyToValue(_ baseStruct, sb *strings.Builder) { +func (mf *messageValueField) generateCopyToValue(_ baseStruct, sb *bytes.Buffer) { sb.WriteString("\tms." + mf.fieldName + "().CopyTo(dest." + mf.fieldName + "())") } @@ -306,7 +307,7 @@ type primitiveField struct { testVal string } -func (pf *primitiveField) generateAccessors(ms baseStruct, sb *strings.Builder) { +func (pf *primitiveField) generateAccessors(ms baseStruct, sb *bytes.Buffer) { sb.WriteString(os.Expand(accessorsPrimitiveTemplate, func(name string) string { switch name { case "structName": @@ -325,7 +326,7 @@ func (pf *primitiveField) generateAccessors(ms baseStruct, sb *strings.Builder) })) } -func (pf *primitiveField) generateAccessorsTest(ms baseStruct, sb *strings.Builder) { +func (pf *primitiveField) generateAccessorsTest(ms baseStruct, sb *bytes.Buffer) { sb.WriteString(os.Expand(accessorsPrimitiveTestTemplate, func(name string) string { switch name { case "structName": @@ -344,11 +345,11 @@ func (pf *primitiveField) generateAccessorsTest(ms baseStruct, sb *strings.Build })) } -func (pf *primitiveField) generateSetWithTestValue(sb *strings.Builder) { +func (pf *primitiveField) generateSetWithTestValue(sb *bytes.Buffer) { sb.WriteString("\ttv.orig." + pf.fieldName + " = " + pf.testVal) } -func (pf *primitiveField) generateCopyToValue(_ baseStruct, sb *strings.Builder) { +func (pf *primitiveField) generateCopyToValue(_ baseStruct, sb *bytes.Buffer) { sb.WriteString("\tdest.Set" + pf.fieldName + "(ms." + pf.fieldName + "())") } @@ -369,7 +370,7 @@ type primitiveTypedField struct { returnType *primitiveType } -func (ptf *primitiveTypedField) generateAccessors(ms baseStruct, sb *strings.Builder) { +func (ptf *primitiveTypedField) generateAccessors(ms baseStruct, sb *bytes.Buffer) { sb.WriteString(os.Expand(accessorsPrimitiveTypedTemplate, func(name string) string { switch name { case "structName": @@ -398,7 +399,7 @@ func (ptf *primitiveTypedField) generateAccessors(ms baseStruct, sb *strings.Bui })) } -func (ptf *primitiveTypedField) generateAccessorsTest(ms baseStruct, sb *strings.Builder) { +func (ptf *primitiveTypedField) generateAccessorsTest(ms baseStruct, sb *bytes.Buffer) { sb.WriteString(os.Expand(accessorsPrimitiveTypedTestTemplate, func(name string) string { switch name { case "structName": @@ -422,7 +423,7 @@ func (ptf *primitiveTypedField) generateAccessorsTest(ms baseStruct, sb *strings })) } -func (ptf *primitiveTypedField) generateSetWithTestValue(sb *strings.Builder) { +func (ptf *primitiveTypedField) generateSetWithTestValue(sb *bytes.Buffer) { originFieldName := ptf.fieldName if ptf.originFieldName != "" { originFieldName = ptf.originFieldName @@ -430,7 +431,7 @@ func (ptf *primitiveTypedField) generateSetWithTestValue(sb *strings.Builder) { sb.WriteString("\ttv.orig." + originFieldName + " = " + ptf.returnType.testVal) } -func (ptf *primitiveTypedField) generateCopyToValue(_ baseStruct, sb *strings.Builder) { +func (ptf *primitiveTypedField) generateCopyToValue(_ baseStruct, sb *bytes.Buffer) { sb.WriteString("\tdest.Set" + ptf.fieldName + "(ms." + ptf.fieldName + "())") } @@ -446,7 +447,7 @@ type primitiveSliceField struct { testVal string } -func (psf *primitiveSliceField) generateAccessors(ms baseStruct, sb *strings.Builder) { +func (psf *primitiveSliceField) generateAccessors(ms baseStruct, sb *bytes.Buffer) { sb.WriteString(os.Expand(accessorsPrimitiveSliceTemplate, func(name string) string { switch name { case "structName": @@ -468,7 +469,7 @@ func (psf *primitiveSliceField) generateAccessors(ms baseStruct, sb *strings.Bui })) } -func (psf *primitiveSliceField) generateAccessorsTest(ms baseStruct, sb *strings.Builder) { +func (psf *primitiveSliceField) generateAccessorsTest(ms baseStruct, sb *bytes.Buffer) { sb.WriteString(os.Expand(accessorsPrimitiveSliceTestTemplate, func(name string) string { switch name { case "structName": @@ -492,11 +493,11 @@ func (psf *primitiveSliceField) generateAccessorsTest(ms baseStruct, sb *strings })) } -func (psf *primitiveSliceField) generateSetWithTestValue(sb *strings.Builder) { +func (psf *primitiveSliceField) generateSetWithTestValue(sb *bytes.Buffer) { sb.WriteString("\ttv.orig." + psf.fieldName + " = " + psf.testVal) } -func (psf *primitiveSliceField) generateCopyToValue(ms baseStruct, sb *strings.Builder) { +func (psf *primitiveSliceField) generateCopyToValue(ms baseStruct, sb *bytes.Buffer) { sb.WriteString("\tms." + psf.fieldName + "().CopyTo(dest." + psf.fieldName + "())") } @@ -511,7 +512,7 @@ type oneOfField struct { omitOriginFieldNameInNames bool } -func (of *oneOfField) generateAccessors(ms baseStruct, sb *strings.Builder) { +func (of *oneOfField) generateAccessors(ms baseStruct, sb *bytes.Buffer) { of.generateTypeAccessors(ms, sb) sb.WriteString("\n") for _, v := range of.values { @@ -520,7 +521,7 @@ func (of *oneOfField) generateAccessors(ms baseStruct, sb *strings.Builder) { } } -func (of *oneOfField) generateTypeAccessors(ms baseStruct, sb *strings.Builder) { +func (of *oneOfField) generateTypeAccessors(ms baseStruct, sb *bytes.Buffer) { sb.WriteString(os.Expand(oneOfTypeAccessorHeaderTemplate, func(name string) string { switch name { case "lowerOriginFieldName": @@ -554,7 +555,7 @@ func (of *oneOfField) typeFuncName() string { return of.originFieldName + typeSuffix } -func (of *oneOfField) generateAccessorsTest(ms baseStruct, sb *strings.Builder) { +func (of *oneOfField) generateAccessorsTest(ms baseStruct, sb *bytes.Buffer) { sb.WriteString(os.Expand(oneOfTypeAccessorHeaderTestTemplate, func(name string) string { switch name { case "structName": @@ -574,11 +575,11 @@ func (of *oneOfField) generateAccessorsTest(ms baseStruct, sb *strings.Builder) } } -func (of *oneOfField) generateSetWithTestValue(sb *strings.Builder) { +func (of *oneOfField) generateSetWithTestValue(sb *bytes.Buffer) { of.values[of.testValueIdx].generateSetWithTestValue(of, sb) } -func (of *oneOfField) generateCopyToValue(_ baseStruct, sb *strings.Builder) { +func (of *oneOfField) generateCopyToValue(_ baseStruct, sb *bytes.Buffer) { sb.WriteString("\tswitch ms." + of.typeFuncName() + "() {\n") for _, v := range of.values { v.generateCopyToValue(of, sb) @@ -589,11 +590,11 @@ func (of *oneOfField) generateCopyToValue(_ baseStruct, sb *strings.Builder) { var _ baseField = (*oneOfField)(nil) type oneOfValue interface { - generateAccessors(ms baseStruct, of *oneOfField, sb *strings.Builder) - generateTests(ms baseStruct, of *oneOfField, sb *strings.Builder) - generateSetWithTestValue(of *oneOfField, sb *strings.Builder) - generateCopyToValue(of *oneOfField, sb *strings.Builder) - generateTypeSwitchCase(of *oneOfField, sb *strings.Builder) + generateAccessors(ms baseStruct, of *oneOfField, sb *bytes.Buffer) + generateTests(ms baseStruct, of *oneOfField, sb *bytes.Buffer) + generateSetWithTestValue(of *oneOfField, sb *bytes.Buffer) + generateCopyToValue(of *oneOfField, sb *bytes.Buffer) + generateTypeSwitchCase(of *oneOfField, sb *bytes.Buffer) } type oneOfPrimitiveValue struct { @@ -604,7 +605,7 @@ type oneOfPrimitiveValue struct { originFieldName string } -func (opv *oneOfPrimitiveValue) generateAccessors(ms baseStruct, of *oneOfField, sb *strings.Builder) { +func (opv *oneOfPrimitiveValue) generateAccessors(ms baseStruct, of *oneOfField, sb *bytes.Buffer) { sb.WriteString(os.Expand(accessorsOneOfPrimitiveTemplate, func(name string) string { switch name { case "structName": @@ -628,7 +629,7 @@ func (opv *oneOfPrimitiveValue) generateAccessors(ms baseStruct, of *oneOfField, sb.WriteString("\n") } -func (opv *oneOfPrimitiveValue) generateTests(ms baseStruct, of *oneOfField, sb *strings.Builder) { +func (opv *oneOfPrimitiveValue) generateTests(ms baseStruct, of *oneOfField, sb *bytes.Buffer) { sb.WriteString(os.Expand(accessorsOneOfPrimitiveTestTemplate, func(name string) string { switch name { case "structName": @@ -659,16 +660,16 @@ func (opv *oneOfPrimitiveValue) accessorFieldName(of *oneOfField) string { return opv.fieldName + of.originFieldName } -func (opv *oneOfPrimitiveValue) generateSetWithTestValue(of *oneOfField, sb *strings.Builder) { +func (opv *oneOfPrimitiveValue) generateSetWithTestValue(of *oneOfField, sb *bytes.Buffer) { sb.WriteString("\ttv.orig." + of.originFieldName + " = &" + of.originTypePrefix + opv.originFieldName + "{" + opv.originFieldName + ":" + opv.testVal + "}") } -func (opv *oneOfPrimitiveValue) generateCopyToValue(of *oneOfField, sb *strings.Builder) { +func (opv *oneOfPrimitiveValue) generateCopyToValue(of *oneOfField, sb *bytes.Buffer) { sb.WriteString("\tcase " + of.typeName + opv.fieldName + ":\n") sb.WriteString("\tdest.Set" + opv.accessorFieldName(of) + "(ms." + opv.accessorFieldName(of) + "())\n") } -func (opv *oneOfPrimitiveValue) generateTypeSwitchCase(of *oneOfField, sb *strings.Builder) { +func (opv *oneOfPrimitiveValue) generateTypeSwitchCase(of *oneOfField, sb *bytes.Buffer) { sb.WriteString("\tcase *" + of.originTypePrefix + opv.originFieldName + ":\n") sb.WriteString("\t\treturn " + of.typeName + opv.fieldName + "\n") } @@ -681,7 +682,7 @@ type oneOfMessageValue struct { returnMessage *messageValueStruct } -func (omv *oneOfMessageValue) generateAccessors(ms baseStruct, of *oneOfField, sb *strings.Builder) { +func (omv *oneOfMessageValue) generateAccessors(ms baseStruct, of *oneOfField, sb *bytes.Buffer) { sb.WriteString(os.Expand(accessorsOneOfMessageTemplate, func(name string) string { switch name { case "fieldName": @@ -709,7 +710,7 @@ func (omv *oneOfMessageValue) generateAccessors(ms baseStruct, of *oneOfField, s sb.WriteString("\n") } -func (omv *oneOfMessageValue) generateTests(ms baseStruct, of *oneOfField, sb *strings.Builder) { +func (omv *oneOfMessageValue) generateTests(ms baseStruct, of *oneOfField, sb *bytes.Buffer) { sb.WriteString(os.Expand(accessorsOneOfMessageTestTemplate, func(name string) string { switch name { case "structName": @@ -729,12 +730,12 @@ func (omv *oneOfMessageValue) generateTests(ms baseStruct, of *oneOfField, sb *s sb.WriteString("\n") } -func (omv *oneOfMessageValue) generateSetWithTestValue(of *oneOfField, sb *strings.Builder) { +func (omv *oneOfMessageValue) generateSetWithTestValue(of *oneOfField, sb *bytes.Buffer) { 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) { +func (omv *oneOfMessageValue) generateCopyToValue(of *oneOfField, sb *bytes.Buffer) { sb.WriteString(os.Expand(copyToValueOneOfMessageTemplate, func(name string) string { switch name { case "fieldName": @@ -750,7 +751,7 @@ func (omv *oneOfMessageValue) generateCopyToValue(of *oneOfField, sb *strings.Bu sb.WriteString("\n") } -func (omv *oneOfMessageValue) generateTypeSwitchCase(of *oneOfField, sb *strings.Builder) { +func (omv *oneOfMessageValue) generateTypeSwitchCase(of *oneOfField, sb *bytes.Buffer) { sb.WriteString("\tcase *" + of.originTypePrefix + omv.fieldName + ":\n") sb.WriteString("\t\treturn " + of.typeName + omv.fieldName + "\n") } @@ -765,7 +766,7 @@ type optionalPrimitiveValue struct { originTypePrefix string } -func (opv *optionalPrimitiveValue) generateAccessors(ms baseStruct, sb *strings.Builder) { +func (opv *optionalPrimitiveValue) generateAccessors(ms baseStruct, sb *bytes.Buffer) { sb.WriteString(os.Expand(accessorsOptionalPrimitiveValueTemplate, func(name string) string { switch name { case "structName": @@ -785,7 +786,7 @@ func (opv *optionalPrimitiveValue) generateAccessors(ms baseStruct, sb *strings. sb.WriteString("\n") } -func (opv *optionalPrimitiveValue) generateAccessorsTest(ms baseStruct, sb *strings.Builder) { +func (opv *optionalPrimitiveValue) generateAccessorsTest(ms baseStruct, sb *bytes.Buffer) { sb.WriteString(os.Expand(accessorsPrimitiveTestTemplate, func(name string) string { switch name { case "structName": @@ -805,11 +806,11 @@ func (opv *optionalPrimitiveValue) generateAccessorsTest(ms baseStruct, sb *stri sb.WriteString("\n") } -func (opv *optionalPrimitiveValue) generateSetWithTestValue(sb *strings.Builder) { +func (opv *optionalPrimitiveValue) generateSetWithTestValue(sb *bytes.Buffer) { sb.WriteString("\ttv.orig." + opv.fieldName + "_ = &" + opv.originTypePrefix + opv.fieldName + "{" + opv.fieldName + ":" + opv.testVal + "}") } -func (opv *optionalPrimitiveValue) generateCopyToValue(_ baseStruct, sb *strings.Builder) { +func (opv *optionalPrimitiveValue) generateCopyToValue(_ baseStruct, sb *bytes.Buffer) { sb.WriteString("if ms.Has" + opv.fieldName + "(){\n") sb.WriteString("\tdest.Set" + opv.fieldName + "(ms." + opv.fieldName + "())\n") sb.WriteString("}\n") diff --git a/pdata/internal/cmd/pdatagen/internal/base_slices.go b/pdata/internal/cmd/pdatagen/internal/base_slices.go index 5fc741a81a26..ba360f9d2d6b 100644 --- a/pdata/internal/cmd/pdatagen/internal/base_slices.go +++ b/pdata/internal/cmd/pdatagen/internal/base_slices.go @@ -15,8 +15,8 @@ package internal // import "go.opentelemetry.io/collector/pdata/internal/cmd/pdatagen/internal" import ( + "bytes" "os" - "strings" ) const commonSliceTemplate = ` @@ -489,17 +489,17 @@ func (ss *sliceOfPtrs) getPackageName() string { return ss.packageName } -func (ss *sliceOfPtrs) generateStruct(sb *strings.Builder) { +func (ss *sliceOfPtrs) generateStruct(sb *bytes.Buffer) { sb.WriteString(os.Expand(slicePtrTemplate, ss.templateFields())) sb.WriteString(os.Expand(commonSliceTemplate, ss.templateFields())) } -func (ss *sliceOfPtrs) generateTests(sb *strings.Builder) { +func (ss *sliceOfPtrs) generateTests(sb *bytes.Buffer) { sb.WriteString(os.Expand(slicePtrTestTemplate, ss.templateFields())) sb.WriteString(os.Expand(commonSliceTestTemplate, ss.templateFields())) } -func (ss *sliceOfPtrs) generateTestValueHelpers(sb *strings.Builder) { +func (ss *sliceOfPtrs) generateTestValueHelpers(sb *bytes.Buffer) { sb.WriteString(os.Expand(slicePtrGenerateTest, ss.templateFields())) } @@ -518,7 +518,7 @@ func (ss *sliceOfPtrs) templateFields() func(name string) string { } } -func (ss *sliceOfPtrs) generateInternal(sb *strings.Builder) { +func (ss *sliceOfPtrs) generateInternal(sb *bytes.Buffer) { sb.WriteString(os.Expand(slicePtrInternalTemplate, func(name string) string { switch name { case "structName": @@ -549,17 +549,17 @@ func (ss *sliceOfValues) getPackageName() string { return ss.packageName } -func (ss *sliceOfValues) generateStruct(sb *strings.Builder) { +func (ss *sliceOfValues) generateStruct(sb *bytes.Buffer) { sb.WriteString(os.Expand(sliceValueTemplate, ss.templateFields())) sb.WriteString(os.Expand(commonSliceTemplate, ss.templateFields())) } -func (ss *sliceOfValues) generateTests(sb *strings.Builder) { +func (ss *sliceOfValues) generateTests(sb *bytes.Buffer) { sb.WriteString(os.Expand(sliceValueTestTemplate, ss.templateFields())) sb.WriteString(os.Expand(commonSliceTestTemplate, ss.templateFields())) } -func (ss *sliceOfValues) generateTestValueHelpers(sb *strings.Builder) { +func (ss *sliceOfValues) generateTestValueHelpers(sb *bytes.Buffer) { sb.WriteString(os.Expand(sliceValueGenerateTest, ss.templateFields())) } @@ -578,7 +578,7 @@ func (ss *sliceOfValues) templateFields() func(name string) string { } } -func (ss *sliceOfValues) generateInternal(sb *strings.Builder) { +func (ss *sliceOfValues) generateInternal(sb *bytes.Buffer) { sb.WriteString(os.Expand(sliceValueInternalTemplate, func(name string) string { switch name { case "structName": diff --git a/pdata/internal/cmd/pdatagen/internal/base_structs.go b/pdata/internal/cmd/pdatagen/internal/base_structs.go index db1091cab778..5400c034cb9d 100644 --- a/pdata/internal/cmd/pdatagen/internal/base_structs.go +++ b/pdata/internal/cmd/pdatagen/internal/base_structs.go @@ -15,8 +15,8 @@ package internal // import "go.opentelemetry.io/collector/pdata/internal/cmd/pdatagen/internal" import ( + "bytes" "os" - "strings" ) const messageValueTemplate = `${description} @@ -106,13 +106,13 @@ type baseStruct interface { getPackageName() string - generateStruct(sb *strings.Builder) + generateStruct(sb *bytes.Buffer) - generateTests(sb *strings.Builder) + generateTests(sb *bytes.Buffer) - generateTestValueHelpers(sb *strings.Builder) + generateTestValueHelpers(sb *bytes.Buffer) - generateInternal(sb *strings.Builder) + generateInternal(sb *bytes.Buffer) } type messageValueStruct struct { @@ -131,7 +131,7 @@ func (ms *messageValueStruct) getPackageName() string { return ms.packageName } -func (ms *messageValueStruct) generateStruct(sb *strings.Builder) { +func (ms *messageValueStruct) generateStruct(sb *bytes.Buffer) { sb.WriteString(os.Expand(messageValueTemplate, func(name string) string { switch name { case "structName": @@ -169,7 +169,7 @@ func (ms *messageValueStruct) generateStruct(sb *strings.Builder) { })) } -func (ms *messageValueStruct) generateTests(sb *strings.Builder) { +func (ms *messageValueStruct) generateTests(sb *bytes.Buffer) { sb.WriteString(os.Expand(messageValueTestTemplate, func(name string) string { switch name { case "structName": @@ -186,7 +186,7 @@ func (ms *messageValueStruct) generateTests(sb *strings.Builder) { } } -func (ms *messageValueStruct) generateTestValueHelpers(sb *strings.Builder) { +func (ms *messageValueStruct) generateTestValueHelpers(sb *bytes.Buffer) { sb.WriteString(os.Expand(messageValueGenerateTestTemplate, func(name string) string { switch name { case "structName": @@ -217,7 +217,7 @@ func (ms *messageValueStruct) generateTestValueHelpers(sb *strings.Builder) { })) } -func (ms *messageValueStruct) generateInternal(sb *strings.Builder) { +func (ms *messageValueStruct) generateInternal(sb *bytes.Buffer) { sb.WriteString(os.Expand(messageValueAliasTemplate, func(name string) string { switch name { case "structName": diff --git a/pdata/internal/cmd/pdatagen/internal/files.go b/pdata/internal/cmd/pdatagen/internal/files.go index a66f2f719106..3b7c83d578e8 100644 --- a/pdata/internal/cmd/pdatagen/internal/files.go +++ b/pdata/internal/cmd/pdatagen/internal/files.go @@ -15,9 +15,9 @@ package internal // import "go.opentelemetry.io/collector/pdata/internal/cmd/pdatagen/internal" import ( + "bytes" "os" "path/filepath" - "strings" ) const header = `// Copyright The OpenTelemetry Authors @@ -59,12 +59,7 @@ type File struct { // GenerateFile generates the configured data structures for this File. func (f *File) GenerateFile() error { - fp, err := os.Create(filepath.Join(".", "pdata", f.PackageName, generateFileName(f.Name))) - if err != nil { - return err - } - - var sb strings.Builder + var sb bytes.Buffer generateHeader(&sb, f.PackageName) // Add imports @@ -85,21 +80,14 @@ func (f *File) GenerateFile() error { } sb.WriteString(newLine) - _, err = fp.WriteString(sb.String()) - if err != nil { - return err - } - return fp.Close() + // ignore gosec complain about permissions being `0644`. + //nolint:gosec + return os.WriteFile(filepath.Join("pdata", f.PackageName, generateFileName(f.Name)), sb.Bytes(), 0644) } // GenerateTestFile generates tests for the configured data structures for this File. func (f *File) GenerateTestFile() error { - fp, err := os.Create(filepath.Join(".", "pdata", f.PackageName, generateTestFileName(f.Name))) - if err != nil { - return err - } - - var sb strings.Builder + var sb bytes.Buffer generateHeader(&sb, f.PackageName) // Add imports @@ -119,21 +107,14 @@ func (f *File) GenerateTestFile() error { s.generateTests(&sb) } - _, err = fp.WriteString(sb.String()) - if err != nil { - return err - } - return fp.Close() + // ignore gosec complain about permissions being `0644`. + //nolint:gosec + return os.WriteFile(filepath.Join("pdata", f.PackageName, generateTestFileName(f.Name)), sb.Bytes(), 0644) } // GenerateInternalFile generates the internal pdata structures for this File. func (f *File) GenerateInternalFile() error { - fp, err := os.Create(filepath.Join(".", "pdata", "internal", generateInternalFileName(f.Name))) - if err != nil { - return err - } - - var sb strings.Builder + var sb bytes.Buffer generateHeader(&sb, "internal") // Add imports @@ -160,11 +141,9 @@ func (f *File) GenerateInternalFile() error { } sb.WriteString(newLine) - _, err = fp.WriteString(sb.String()) - if err != nil { - return err - } - return fp.Close() + // ignore gosec complain about permissions being `0644`. + //nolint:gosec + return os.WriteFile(filepath.Join("pdata", "internal", generateInternalFileName(f.Name)), sb.Bytes(), 0644) } func generateFileName(fileName string) string { @@ -179,7 +158,7 @@ func generateTestFileName(fileName string) string { return "generated_" + fileName + "_test.go" } -func generateHeader(sb *strings.Builder, packageName string) { +func generateHeader(sb *bytes.Buffer, packageName string) { sb.WriteString(header) sb.WriteString(newLine + newLine) sb.WriteString("package " + packageName) diff --git a/pdata/internal/cmd/pdatagen/internal/primitive_slice_structs.go b/pdata/internal/cmd/pdatagen/internal/primitive_slice_structs.go index a410faae1316..15827d2839cb 100644 --- a/pdata/internal/cmd/pdatagen/internal/primitive_slice_structs.go +++ b/pdata/internal/cmd/pdatagen/internal/primitive_slice_structs.go @@ -15,6 +15,7 @@ package internal // import "go.opentelemetry.io/collector/pdata/internal/cmd/pdatagen/internal" import ( + "bytes" "os" "strings" ) @@ -178,7 +179,7 @@ func (iss *primitiveSliceStruct) getPackageName() string { return iss.packageName } -func (iss *primitiveSliceStruct) generateStruct(sb *strings.Builder) { +func (iss *primitiveSliceStruct) generateStruct(sb *bytes.Buffer) { sb.WriteString(os.Expand(primitiveSliceTemplate, func(name string) string { switch name { case "structName": @@ -193,7 +194,7 @@ func (iss *primitiveSliceStruct) generateStruct(sb *strings.Builder) { })) } -func (iss *primitiveSliceStruct) generateTests(sb *strings.Builder) { +func (iss *primitiveSliceStruct) generateTests(sb *bytes.Buffer) { sb.WriteString(os.Expand(immutableSliceTestTemplate, func(name string) string { switch name { case "structName": @@ -206,9 +207,9 @@ func (iss *primitiveSliceStruct) generateTests(sb *strings.Builder) { })) } -func (iss *primitiveSliceStruct) generateTestValueHelpers(*strings.Builder) {} +func (iss *primitiveSliceStruct) generateTestValueHelpers(*bytes.Buffer) {} -func (iss *primitiveSliceStruct) generateInternal(sb *strings.Builder) { +func (iss *primitiveSliceStruct) generateInternal(sb *bytes.Buffer) { sb.WriteString(os.Expand(primitiveSliceInternalTemplate, func(name string) string { switch name { case "structName":