style(report, writers, graphql, prisma, typeorm): replace sb.WriteString with fmt.Fprintf for consistency

This commit is contained in:
2026-02-28 17:08:12 +02:00
parent ff1180524a
commit a471a4533b
5 changed files with 43 additions and 43 deletions

View File

@@ -60,19 +60,19 @@ func (f *MarkdownFormatter) Format(report *InspectorReport) (string, error) {
// Summary // Summary
sb.WriteString(f.formatHeader("Summary")) sb.WriteString(f.formatHeader("Summary"))
sb.WriteString("\n") sb.WriteString("\n")
sb.WriteString(fmt.Sprintf("- Rules Checked: %d\n", report.Summary.RulesChecked)) fmt.Fprintf(&sb, "- Rules Checked: %d\n", report.Summary.RulesChecked)
// Color-code error and warning counts // Color-code error and warning counts
if report.Summary.ErrorCount > 0 { if report.Summary.ErrorCount > 0 {
sb.WriteString(f.colorize(fmt.Sprintf("- Errors: %d\n", report.Summary.ErrorCount), colorRed)) sb.WriteString(f.colorize(fmt.Sprintf("- Errors: %d\n", report.Summary.ErrorCount), colorRed))
} else { } else {
sb.WriteString(fmt.Sprintf("- Errors: %d\n", report.Summary.ErrorCount)) fmt.Fprintf(&sb, "- Errors: %d\n", report.Summary.ErrorCount)
} }
if report.Summary.WarningCount > 0 { if report.Summary.WarningCount > 0 {
sb.WriteString(f.colorize(fmt.Sprintf("- Warnings: %d\n", report.Summary.WarningCount), colorYellow)) sb.WriteString(f.colorize(fmt.Sprintf("- Warnings: %d\n", report.Summary.WarningCount), colorYellow))
} else { } else {
sb.WriteString(fmt.Sprintf("- Warnings: %d\n", report.Summary.WarningCount)) fmt.Fprintf(&sb, "- Warnings: %d\n", report.Summary.WarningCount)
} }
if report.Summary.PassedCount > 0 { if report.Summary.PassedCount > 0 {

View File

@@ -62,10 +62,10 @@ func (w *Writer) databaseToDBML(d *models.Database) string {
var sb strings.Builder var sb strings.Builder
if d.Description != "" { if d.Description != "" {
sb.WriteString(fmt.Sprintf("// %s\n", d.Description)) fmt.Fprintf(&sb, "// %s\n", d.Description)
} }
if d.Comment != "" { if d.Comment != "" {
sb.WriteString(fmt.Sprintf("// %s\n", d.Comment)) fmt.Fprintf(&sb, "// %s\n", d.Comment)
} }
if d.Description != "" || d.Comment != "" { if d.Description != "" || d.Comment != "" {
sb.WriteString("\n") sb.WriteString("\n")
@@ -94,7 +94,7 @@ func (w *Writer) schemaToDBML(schema *models.Schema) string {
var sb strings.Builder var sb strings.Builder
if schema.Description != "" { if schema.Description != "" {
sb.WriteString(fmt.Sprintf("// Schema: %s - %s\n", schema.Name, schema.Description)) fmt.Fprintf(&sb, "// Schema: %s - %s\n", schema.Name, schema.Description)
} }
for _, table := range schema.Tables { for _, table := range schema.Tables {
@@ -110,10 +110,10 @@ func (w *Writer) tableToDBML(t *models.Table) string {
var sb strings.Builder var sb strings.Builder
tableName := fmt.Sprintf("%s.%s", t.Schema, t.Name) tableName := fmt.Sprintf("%s.%s", t.Schema, t.Name)
sb.WriteString(fmt.Sprintf("Table %s {\n", tableName)) fmt.Fprintf(&sb, "Table %s {\n", tableName)
for _, column := range t.Columns { for _, column := range t.Columns {
sb.WriteString(fmt.Sprintf(" %s %s", column.Name, column.Type)) fmt.Fprintf(&sb, " %s %s", column.Name, column.Type)
var attrs []string var attrs []string
if column.IsPrimaryKey { if column.IsPrimaryKey {
@@ -138,11 +138,11 @@ func (w *Writer) tableToDBML(t *models.Table) string {
} }
if len(attrs) > 0 { if len(attrs) > 0 {
sb.WriteString(fmt.Sprintf(" [%s]", strings.Join(attrs, ", "))) fmt.Fprintf(&sb, " [%s]", strings.Join(attrs, ", "))
} }
if column.Comment != "" { if column.Comment != "" {
sb.WriteString(fmt.Sprintf(" // %s", column.Comment)) fmt.Fprintf(&sb, " // %s", column.Comment)
} }
sb.WriteString("\n") sb.WriteString("\n")
} }
@@ -161,9 +161,9 @@ func (w *Writer) tableToDBML(t *models.Table) string {
indexAttrs = append(indexAttrs, fmt.Sprintf("type: %s", index.Type)) indexAttrs = append(indexAttrs, fmt.Sprintf("type: %s", index.Type))
} }
sb.WriteString(fmt.Sprintf(" (%s)", strings.Join(index.Columns, ", "))) fmt.Fprintf(&sb, " (%s)", strings.Join(index.Columns, ", "))
if len(indexAttrs) > 0 { if len(indexAttrs) > 0 {
sb.WriteString(fmt.Sprintf(" [%s]", strings.Join(indexAttrs, ", "))) fmt.Fprintf(&sb, " [%s]", strings.Join(indexAttrs, ", "))
} }
sb.WriteString("\n") sb.WriteString("\n")
} }
@@ -172,7 +172,7 @@ func (w *Writer) tableToDBML(t *models.Table) string {
note := strings.TrimSpace(t.Description + " " + t.Comment) note := strings.TrimSpace(t.Description + " " + t.Comment)
if note != "" { if note != "" {
sb.WriteString(fmt.Sprintf("\n Note: '%s'\n", note)) fmt.Fprintf(&sb, "\n Note: '%s'\n", note)
} }
sb.WriteString("}\n") sb.WriteString("}\n")

View File

@@ -52,7 +52,7 @@ func (w *Writer) databaseToGraphQL(db *models.Database) string {
if w.shouldIncludeComments() { if w.shouldIncludeComments() {
sb.WriteString("# Generated GraphQL Schema\n") sb.WriteString("# Generated GraphQL Schema\n")
if db.Name != "" { if db.Name != "" {
sb.WriteString(fmt.Sprintf("# Database: %s\n", db.Name)) fmt.Fprintf(&sb, "# Database: %s\n", db.Name)
} }
sb.WriteString("\n") sb.WriteString("\n")
} }
@@ -62,7 +62,7 @@ func (w *Writer) databaseToGraphQL(db *models.Database) string {
scalars := w.collectCustomScalars(db) scalars := w.collectCustomScalars(db)
if len(scalars) > 0 { if len(scalars) > 0 {
for _, scalar := range scalars { for _, scalar := range scalars {
sb.WriteString(fmt.Sprintf("scalar %s\n", scalar)) fmt.Fprintf(&sb, "scalar %s\n", scalar)
} }
sb.WriteString("\n") sb.WriteString("\n")
} }
@@ -176,9 +176,9 @@ func (w *Writer) isJoinTable(table *models.Table) bool {
func (w *Writer) enumToGraphQL(enum *models.Enum) string { func (w *Writer) enumToGraphQL(enum *models.Enum) string {
var sb strings.Builder var sb strings.Builder
sb.WriteString(fmt.Sprintf("enum %s {\n", enum.Name)) fmt.Fprintf(&sb, "enum %s {\n", enum.Name)
for _, value := range enum.Values { for _, value := range enum.Values {
sb.WriteString(fmt.Sprintf(" %s\n", value)) fmt.Fprintf(&sb, " %s\n", value)
} }
sb.WriteString("}\n") sb.WriteString("}\n")
@@ -197,10 +197,10 @@ func (w *Writer) tableToGraphQL(table *models.Table, db *models.Database, schema
if desc == "" { if desc == "" {
desc = table.Comment desc = table.Comment
} }
sb.WriteString(fmt.Sprintf("# %s\n", desc)) fmt.Fprintf(&sb, "# %s\n", desc)
} }
sb.WriteString(fmt.Sprintf("type %s {\n", typeName)) fmt.Fprintf(&sb, "type %s {\n", typeName)
// Collect and categorize fields // Collect and categorize fields
var idFields, scalarFields, relationFields []string var idFields, scalarFields, relationFields []string

View File

@@ -125,9 +125,9 @@ func (w *Writer) generateGenerator() string {
func (w *Writer) enumToPrisma(enum *models.Enum) string { func (w *Writer) enumToPrisma(enum *models.Enum) string {
var sb strings.Builder var sb strings.Builder
sb.WriteString(fmt.Sprintf("enum %s {\n", enum.Name)) fmt.Fprintf(&sb, "enum %s {\n", enum.Name)
for _, value := range enum.Values { for _, value := range enum.Values {
sb.WriteString(fmt.Sprintf(" %s\n", value)) fmt.Fprintf(&sb, " %s\n", value)
} }
sb.WriteString("}\n") sb.WriteString("}\n")
@@ -179,7 +179,7 @@ func (w *Writer) identifyJoinTables(schema *models.Schema) map[string]bool {
func (w *Writer) tableToPrisma(table *models.Table, schema *models.Schema, joinTables map[string]bool) string { func (w *Writer) tableToPrisma(table *models.Table, schema *models.Schema, joinTables map[string]bool) string {
var sb strings.Builder var sb strings.Builder
sb.WriteString(fmt.Sprintf("model %s {\n", table.Name)) fmt.Fprintf(&sb, "model %s {\n", table.Name)
// Collect columns to write // Collect columns to write
columns := make([]*models.Column, 0, len(table.Columns)) columns := make([]*models.Column, 0, len(table.Columns))
@@ -219,11 +219,11 @@ func (w *Writer) columnToField(col *models.Column, table *models.Table, schema *
var sb strings.Builder var sb strings.Builder
// Field name // Field name
sb.WriteString(fmt.Sprintf(" %s", col.Name)) fmt.Fprintf(&sb, " %s", col.Name)
// Field type // Field type
prismaType := w.sqlTypeToPrisma(col.Type, schema) prismaType := w.sqlTypeToPrisma(col.Type, schema)
sb.WriteString(fmt.Sprintf(" %s", prismaType)) fmt.Fprintf(&sb, " %s", prismaType)
// Optional modifier // Optional modifier
if !col.NotNull && !col.IsPrimaryKey { if !col.NotNull && !col.IsPrimaryKey {
@@ -413,7 +413,7 @@ func (w *Writer) generateRelationFields(table *models.Table, schema *models.Sche
relationName = relationName[:len(relationName)-1] relationName = relationName[:len(relationName)-1]
} }
sb.WriteString(fmt.Sprintf(" %s %s", strings.ToLower(relationName), relationType)) fmt.Fprintf(&sb, " %s %s", strings.ToLower(relationName), relationType)
if isOptional { if isOptional {
sb.WriteString("?") sb.WriteString("?")
@@ -479,8 +479,8 @@ func (w *Writer) generateInverseRelations(table *models.Table, schema *models.Sc
if fk.ReferencedTable != table.Name { if fk.ReferencedTable != table.Name {
// This is the other side // This is the other side
otherSide := fk.ReferencedTable otherSide := fk.ReferencedTable
sb.WriteString(fmt.Sprintf(" %ss %s[]\n", fmt.Fprintf(&sb, " %ss %s[]\n",
strings.ToLower(otherSide), otherSide)) strings.ToLower(otherSide), otherSide)
break break
} }
} }
@@ -497,8 +497,8 @@ func (w *Writer) generateInverseRelations(table *models.Table, schema *models.Sc
pluralName += "s" pluralName += "s"
} }
sb.WriteString(fmt.Sprintf(" %s %s[]\n", fmt.Fprintf(&sb, " %s %s[]\n",
strings.ToLower(pluralName), otherTable.Name)) strings.ToLower(pluralName), otherTable.Name)
} }
} }
} }
@@ -530,20 +530,20 @@ func (w *Writer) generateBlockAttributes(table *models.Table) string {
if len(pkCols) > 1 { if len(pkCols) > 1 {
sort.Strings(pkCols) sort.Strings(pkCols)
sb.WriteString(fmt.Sprintf(" @@id([%s])\n", strings.Join(pkCols, ", "))) fmt.Fprintf(&sb, " @@id([%s])\n", strings.Join(pkCols, ", "))
} }
// @@unique for multi-column unique constraints // @@unique for multi-column unique constraints
for _, constraint := range table.Constraints { for _, constraint := range table.Constraints {
if constraint.Type == models.UniqueConstraint && len(constraint.Columns) > 1 { if constraint.Type == models.UniqueConstraint && len(constraint.Columns) > 1 {
sb.WriteString(fmt.Sprintf(" @@unique([%s])\n", strings.Join(constraint.Columns, ", "))) fmt.Fprintf(&sb, " @@unique([%s])\n", strings.Join(constraint.Columns, ", "))
} }
} }
// @@index for indexes // @@index for indexes
for _, index := range table.Indexes { for _, index := range table.Indexes {
if !index.Unique { // Unique indexes are handled by @@unique if !index.Unique { // Unique indexes are handled by @@unique
sb.WriteString(fmt.Sprintf(" @@index([%s])\n", strings.Join(index.Columns, ", "))) fmt.Fprintf(&sb, " @@index([%s])\n", strings.Join(index.Columns, ", "))
} }
} }

View File

@@ -207,7 +207,7 @@ func (w *Writer) tableToEntity(table *models.Table, schema *models.Schema, joinT
// Generate @Entity decorator with options // Generate @Entity decorator with options
entityOptions := w.buildEntityOptions(table) entityOptions := w.buildEntityOptions(table)
sb.WriteString(fmt.Sprintf("@Entity({\n%s\n})\n", entityOptions)) fmt.Fprintf(&sb, "@Entity({\n%s\n})\n", entityOptions)
// Get class name (from metadata if different from table name) // Get class name (from metadata if different from table name)
className := table.Name className := table.Name
@@ -219,7 +219,7 @@ func (w *Writer) tableToEntity(table *models.Table, schema *models.Schema, joinT
} }
} }
sb.WriteString(fmt.Sprintf("export class %s {\n", className)) fmt.Fprintf(&sb, "export class %s {\n", className)
// Collect and sort columns // Collect and sort columns
columns := make([]*models.Column, 0, len(table.Columns)) columns := make([]*models.Column, 0, len(table.Columns))
@@ -272,7 +272,7 @@ func (w *Writer) viewToEntity(view *models.View) string {
sb.WriteString("})\n") sb.WriteString("})\n")
// Generate class // Generate class
sb.WriteString(fmt.Sprintf("export class %s {\n", view.Name)) fmt.Fprintf(&sb, "export class %s {\n", view.Name)
// Generate field definitions (without decorators for view fields) // Generate field definitions (without decorators for view fields)
columns := make([]*models.Column, 0, len(view.Columns)) columns := make([]*models.Column, 0, len(view.Columns))
@@ -285,7 +285,7 @@ func (w *Writer) viewToEntity(view *models.View) string {
for _, col := range columns { for _, col := range columns {
tsType := w.sqlTypeToTypeScript(col.Type) tsType := w.sqlTypeToTypeScript(col.Type)
sb.WriteString(fmt.Sprintf(" %s: %s;\n", col.Name, tsType)) fmt.Fprintf(&sb, " %s: %s;\n", col.Name, tsType)
} }
sb.WriteString("}\n") sb.WriteString("}\n")
@@ -314,7 +314,7 @@ func (w *Writer) columnToField(col *models.Column, table *models.Table) string {
// Regular @Column decorator // Regular @Column decorator
options := w.buildColumnOptions(col, table) options := w.buildColumnOptions(col, table)
if options != "" { if options != "" {
sb.WriteString(fmt.Sprintf(" @Column({ %s })\n", options)) fmt.Fprintf(&sb, " @Column({ %s })\n", options)
} else { } else {
sb.WriteString(" @Column()\n") sb.WriteString(" @Column()\n")
} }
@@ -327,7 +327,7 @@ func (w *Writer) columnToField(col *models.Column, table *models.Table) string {
nullable = " | null" nullable = " | null"
} }
sb.WriteString(fmt.Sprintf(" %s: %s%s;", col.Name, tsType, nullable)) fmt.Fprintf(&sb, " %s: %s%s;", col.Name, tsType, nullable)
return sb.String() return sb.String()
} }
@@ -464,17 +464,17 @@ func (w *Writer) generateRelationFields(table *models.Table, schema *models.Sche
inverseField := w.findInverseFieldName(table.Name, relatedTable, schema) inverseField := w.findInverseFieldName(table.Name, relatedTable, schema)
if inverseField != "" { if inverseField != "" {
sb.WriteString(fmt.Sprintf(" @ManyToOne(() => %s, %s => %s.%s)\n", fmt.Fprintf(&sb, " @ManyToOne(() => %s, %s => %s.%s)\n",
relatedTable, strings.ToLower(relatedTable), strings.ToLower(relatedTable), inverseField)) relatedTable, strings.ToLower(relatedTable), strings.ToLower(relatedTable), inverseField)
} else { } else {
if isNullable { if isNullable {
sb.WriteString(fmt.Sprintf(" @ManyToOne(() => %s, { nullable: true })\n", relatedTable)) fmt.Fprintf(&sb, " @ManyToOne(() => %s, { nullable: true })\n", relatedTable)
} else { } else {
sb.WriteString(fmt.Sprintf(" @ManyToOne(() => %s)\n", relatedTable)) fmt.Fprintf(&sb, " @ManyToOne(() => %s)\n", relatedTable)
} }
} }
sb.WriteString(fmt.Sprintf(" %s: %s%s;\n", fieldName, relatedTable, nullable)) fmt.Fprintf(&sb, " %s: %s%s;\n", fieldName, relatedTable, nullable)
sb.WriteString("\n") sb.WriteString("\n")
} }