feat(dbml): support multiline table notes in DBML
* Add parsing for triple-quoted table notes in DBML * Update writer to format multiline notes correctly * Enhance tests for multiline table notes handling
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user