diff --git a/pkg/readers/dbml/reader.go b/pkg/readers/dbml/reader.go index e0c115d..298a1ec 100644 --- a/pkg/readers/dbml/reader.go +++ b/pkg/readers/dbml/reader.go @@ -434,6 +434,9 @@ func (r *Reader) parseDBML(content string) (*models.Database, error) { var currentSchema string var inIndexes bool var inTable bool + var inTableNote bool + var tableNoteLines []string + tableNoteStartLine := 0 var columnSeq uint var lastIndex *models.Index // most recent index in the current Indexes block lineNo := 0 @@ -443,7 +446,22 @@ func (r *Reader) parseDBML(content string) (*models.Database, error) { for scanner.Scan() { lineNo++ - line := strings.TrimSpace(scanner.Text()) + rawLine := scanner.Text() + line := strings.TrimSpace(rawLine) + + // A table note can use DBML's triple-quoted form. Its contents must be + // consumed before normal parsing, otherwise each prose line is mistaken + // for a column declaration. + if inTableNote { + if line == "'''" { + currentTable.Description = strings.TrimSpace(strings.Join(tableNoteLines, "\n")) + inTableNote = false + tableNoteLines = nil + continue + } + tableNoteLines = append(tableNoteLines, strings.TrimSpace(rawLine)) + continue + } // Skip empty lines and comments if line == "" || strings.HasPrefix(line, "//") { @@ -542,6 +560,12 @@ func (r *Reader) parseDBML(content string) (*models.Database, error) { // Parse table note if inTable && currentTable != nil && strings.HasPrefix(line, "Note:") { note := strings.TrimPrefix(line, "Note:") + if strings.TrimSpace(note) == "'''" { + inTableNote = true + tableNoteLines = nil + tableNoteStartLine = lineNo + continue + } note = strings.Trim(note, " '\"") currentTable.Description = note continue @@ -580,6 +604,13 @@ func (r *Reader) parseDBML(content string) (*models.Database, error) { } } + if err := scanner.Err(); err != nil { + return nil, fmt.Errorf("failed to scan DBML: %w", err) + } + if inTableNote { + return nil, fmt.Errorf("dbml: line %d: unterminated triple-quoted table note", tableNoteStartLine) + } + // Assign pending constraints to their respective tables for _, constraint := range pendingConstraints { // Find the table this constraint belongs to diff --git a/pkg/readers/dbml/reader_test.go b/pkg/readers/dbml/reader_test.go index 6f359ee..8a26181 100644 --- a/pkg/readers/dbml/reader_test.go +++ b/pkg/readers/dbml/reader_test.go @@ -1033,3 +1033,32 @@ func TestReader_ColumnPKOrderPreserved(t *testing.T) { t.Errorf("expected snapshot_id (declared first) to have a lower Sequence than artifact_id, got %d >= %d", snapshotCol.Sequence, artifactCol.Sequence) } } + +func TestReader_MultilineTableNote(t *testing.T) { + dbmlContent := "Table \"info\".\"city\" {\n" + + " \"id_city\" serial [pk, not null, increment]\n" + + " \"name\" text [not null]\n\n" + + " Note: '''\n" + + " Cities and municipalities worldwide.\n\n" + + " SPATIAL:\n" + + " Proximity queries use a GiST index.\n" + + " '''\n" + + "}\n" + path := filepath.Join(t.TempDir(), "city.dbml") + if err := os.WriteFile(path, []byte(dbmlContent), 0o644); err != nil { + t.Fatalf("failed to write fixture: %v", err) + } + + db, err := NewReader(&readers.ReaderOptions{FilePath: path}).ReadDatabase() + if err != nil { + t.Fatalf("ReadDatabase() error = %v", err) + } + + table := db.Schemas[0].Tables[0] + if got, want := table.Description, "Cities and municipalities worldwide.\n\nSPATIAL:\nProximity queries use a GiST index."; got != want { + t.Errorf("table description = %q, want %q", got, want) + } + if got, want := len(table.Columns), 2; got != want { + t.Errorf("column count = %d, want %d; note body must not be parsed as columns", got, want) + } +} diff --git a/pkg/writers/bun/template_data.go b/pkg/writers/bun/template_data.go index 4abacd1..b5deca5 100644 --- a/pkg/writers/bun/template_data.go +++ b/pkg/writers/bun/template_data.go @@ -279,13 +279,16 @@ func (md *ModelData) AddRelationshipField(field *FieldData) { // formatComment combines description and comment into a single comment string func formatComment(description, comment string) string { + var result string if description != "" && comment != "" { - return description + " - " + comment + result = description + " - " + comment + } else if description != "" { + result = description + } else { + result = comment } - if description != "" { - return description - } - return comment + // Generated Go comments are emitted on a single source line. + return strings.Join(strings.Fields(result), " ") } func isStringLikePrimaryKeyType(goType string) bool { diff --git a/pkg/writers/bun/writer_test.go b/pkg/writers/bun/writer_test.go index dc89167..583b8bf 100644 --- a/pkg/writers/bun/writer_test.go +++ b/pkg/writers/bun/writer_test.go @@ -1,6 +1,8 @@ package bun import ( + "go/parser" + "go/token" "os" "path/filepath" "strings" @@ -95,6 +97,29 @@ func TestWriter_WriteTable(t *testing.T) { } } +func TestWriter_WriteTable_MultilineDescriptionProducesValidGo(t *testing.T) { + table := models.InitTable("city", "info") + table.Description = "Cities and municipalities worldwide.\n\nSPATIAL:\nProximity queries use a GiST index." + table.Columns["id_city"] = &models.Column{Name: "id_city", Type: "integer", IsPrimaryKey: true, NotNull: true} + + outputPath := filepath.Join(t.TempDir(), "city.go") + writer := NewWriter(&writers.WriterOptions{OutputPath: outputPath, PackageName: "models"}) + if err := writer.WriteTable(table); err != nil { + t.Fatalf("WriteTable() error = %v", err) + } + + generated, err := os.ReadFile(outputPath) + if err != nil { + t.Fatalf("failed to read generated code: %v", err) + } + if _, err := parser.ParseFile(token.NewFileSet(), outputPath, generated, parser.AllErrors); err != nil { + t.Fatalf("generated code is invalid Go: %v\n%s", err, generated) + } + if !strings.Contains(string(generated), "// Cities and municipalities worldwide. SPATIAL: Proximity queries use a GiST index.") { + t.Errorf("multiline description was not rendered as a single Go comment:\n%s", generated) + } +} + func TestWriter_WriteDatabase_MultiFile(t *testing.T) { // Create a database with two tables db := models.InitDatabase("testdb") diff --git a/pkg/writers/dbml/writer.go b/pkg/writers/dbml/writer.go index a73c7c1..b9e7035 100644 --- a/pkg/writers/dbml/writer.go +++ b/pkg/writers/dbml/writer.go @@ -196,7 +196,11 @@ func (w *Writer) tableToDBML(t *models.Table) string { note := strings.TrimSpace(t.Description + " " + t.Comment) if note != "" { - fmt.Fprintf(&sb, "\n Note: '%s'\n", note) + if strings.Contains(note, "\n") { + fmt.Fprintf(&sb, "\n Note: '''\n%s\n '''\n", note) + } else { + fmt.Fprintf(&sb, "\n Note: '%s'\n", note) + } } sb.WriteString("}\n") diff --git a/pkg/writers/dbml/writer_test.go b/pkg/writers/dbml/writer_test.go index 6c0d70b..6303b37 100644 --- a/pkg/writers/dbml/writer_test.go +++ b/pkg/writers/dbml/writer_test.go @@ -59,6 +59,23 @@ func TestWriter_WriteTable(t *testing.T) { assert.Contains(t, output, "Note: 'User accounts table'") } +func TestWriter_WriteTable_MultilineNote(t *testing.T) { + table := models.InitTable("cities", "info") + table.Description = "Cities and municipalities worldwide.\n\nSPATIAL:\nProximity queries use a GiST index." + + outputPath := filepath.Join(t.TempDir(), "cities.dbml") + writer := NewWriter(&writers.WriterOptions{OutputPath: outputPath}) + if err := writer.WriteTable(table); err != nil { + t.Fatalf("WriteTable() error = %v", err) + } + + output, err := os.ReadFile(outputPath) + if err != nil { + t.Fatalf("failed to read generated DBML: %v", err) + } + assert.Contains(t, string(output), "Note: '''\nCities and municipalities worldwide.\n\nSPATIAL:\nProximity queries use a GiST index.\n '''") +} + func TestWriter_WriteDatabase_WithRelationships(t *testing.T) { db := models.InitDatabase("test_db") schema := models.InitSchema("public") diff --git a/pkg/writers/drizzle/template_data.go b/pkg/writers/drizzle/template_data.go index 0a4584a..060e4cf 100644 --- a/pkg/writers/drizzle/template_data.go +++ b/pkg/writers/drizzle/template_data.go @@ -2,6 +2,7 @@ package drizzle import ( "sort" + "strings" "git.warky.dev/wdevs/relspecgo/pkg/models" ) @@ -199,13 +200,16 @@ func NewIndexData(index *models.Index, tableVar string, tm *TypeMapper) *IndexDa // formatComment combines description and comment into a single comment string func formatComment(description, comment string) string { + var result string if description != "" && comment != "" { - return description + " - " + comment + result = description + " - " + comment + } else if description != "" { + result = description + } else { + result = comment } - if description != "" { - return description - } - return comment + // Generated TypeScript comments are emitted on a single source line. + return strings.Join(strings.Fields(result), " ") } // joinStrings joins a slice of strings with a separator diff --git a/pkg/writers/gorm/template_data.go b/pkg/writers/gorm/template_data.go index fa543ec..6f6b9ef 100644 --- a/pkg/writers/gorm/template_data.go +++ b/pkg/writers/gorm/template_data.go @@ -192,13 +192,17 @@ func (md *ModelData) AddRelationshipField(field *FieldData) { // formatComment combines description and comment into a single comment string func formatComment(description, comment string) string { + var result string if description != "" && comment != "" { - return description + " - " + comment + result = description + " - " + comment + } else if description != "" { + result = description + } else { + result = comment } - if description != "" { - return description - } - return comment + // Generated Go comments are emitted on a single source line. Collapse + // multiline DBML notes so the remaining lines cannot become invalid Go. + return strings.Join(strings.Fields(result), " ") } func isStringLikePrimaryKeyType(goType string) bool { diff --git a/pkg/writers/gorm/writer_test.go b/pkg/writers/gorm/writer_test.go index 552d208..8122cf3 100644 --- a/pkg/writers/gorm/writer_test.go +++ b/pkg/writers/gorm/writer_test.go @@ -1,6 +1,8 @@ package gorm import ( + "go/parser" + "go/token" "os" "path/filepath" "strings" @@ -87,6 +89,29 @@ func TestWriter_WriteTable(t *testing.T) { } } +func TestWriter_WriteTable_MultilineDescriptionProducesValidGo(t *testing.T) { + table := models.InitTable("cities", "info") + table.Description = "Cities and municipalities worldwide.\n\nSPATIAL:\nProximity queries use a GiST index." + table.Columns["id"] = &models.Column{Name: "id", Type: "integer", IsPrimaryKey: true, NotNull: true} + + outputPath := filepath.Join(t.TempDir(), "cities.go") + writer := NewWriter(&writers.WriterOptions{OutputPath: outputPath, PackageName: "models"}) + if err := writer.WriteTable(table); err != nil { + t.Fatalf("WriteTable() error = %v", err) + } + + generated, err := os.ReadFile(outputPath) + if err != nil { + t.Fatalf("failed to read generated code: %v", err) + } + if _, err := parser.ParseFile(token.NewFileSet(), outputPath, generated, parser.AllErrors); err != nil { + t.Fatalf("generated code is invalid Go: %v\n%s", err, generated) + } + if !strings.Contains(string(generated), "// Cities and municipalities worldwide. SPATIAL: Proximity queries use a GiST index.") { + t.Errorf("multiline description was not rendered as a single Go comment:\n%s", generated) + } +} + func TestWriter_WriteDatabase_MultiFile(t *testing.T) { // Create a database with two tables db := models.InitDatabase("testdb")