From 79effe6921c1b3b4ff2a6f8d3ea76c2846032ea1 Mon Sep 17 00:00:00 2001 From: Hein Date: Fri, 19 Dec 2025 21:47:11 +0200 Subject: [PATCH] Fixed bun/gorm writer logic for multi files. --- pkg/writers/bun/writer.go | 42 ++++++++++++++++++++++++++++++++------ pkg/writers/gorm/writer.go | 42 ++++++++++++++++++++++++++++++++------ 2 files changed, 72 insertions(+), 12 deletions(-) diff --git a/pkg/writers/bun/writer.go b/pkg/writers/bun/writer.go index 4155c63..2aa0baa 100644 --- a/pkg/writers/bun/writer.go +++ b/pkg/writers/bun/writer.go @@ -41,12 +41,7 @@ func NewWriter(options *writers.WriterOptions) *Writer { // WriteDatabase writes a complete database as Bun models func (w *Writer) WriteDatabase(db *models.Database) error { // Check if multi-file mode is enabled - multiFile := false - if w.options.Metadata != nil { - if mf, ok := w.options.Metadata["multi_file"].(bool); ok { - multiFile = mf - } - } + multiFile := w.shouldUseMultiFile() if multiFile { return w.writeMultiFile(db) @@ -346,6 +341,41 @@ func (w *Writer) writeOutput(content string) error { return nil } +// shouldUseMultiFile determines whether to use multi-file mode based on metadata or output path +func (w *Writer) shouldUseMultiFile() bool { + // Check if multi_file is explicitly set in metadata + if w.options.Metadata != nil { + if mf, ok := w.options.Metadata["multi_file"].(bool); ok { + return mf + } + } + + // Auto-detect based on output path + if w.options.OutputPath == "" { + // No output path means stdout (single file) + return false + } + + // Check if path ends with .go (explicit file) + if strings.HasSuffix(w.options.OutputPath, ".go") { + return false + } + + // Check if path ends with directory separator + if strings.HasSuffix(w.options.OutputPath, "/") || strings.HasSuffix(w.options.OutputPath, "\\") { + return true + } + + // Check if path exists and is a directory + info, err := os.Stat(w.options.OutputPath) + if err == nil && info.IsDir() { + return true + } + + // Default to single file for ambiguous cases + return false +} + // createDatabaseRef creates a shallow copy of database without schemas to avoid circular references func (w *Writer) createDatabaseRef(db *models.Database) *models.Database { return &models.Database{ diff --git a/pkg/writers/gorm/writer.go b/pkg/writers/gorm/writer.go index e4319a1..7d0a86a 100644 --- a/pkg/writers/gorm/writer.go +++ b/pkg/writers/gorm/writer.go @@ -41,12 +41,7 @@ func NewWriter(options *writers.WriterOptions) *Writer { // WriteDatabase writes a complete database as GORM models func (w *Writer) WriteDatabase(db *models.Database) error { // Check if multi-file mode is enabled - multiFile := false - if w.options.Metadata != nil { - if mf, ok := w.options.Metadata["multi_file"].(bool); ok { - multiFile = mf - } - } + multiFile := w.shouldUseMultiFile() if multiFile { return w.writeMultiFile(db) @@ -340,6 +335,41 @@ func (w *Writer) writeOutput(content string) error { return nil } +// shouldUseMultiFile determines whether to use multi-file mode based on metadata or output path +func (w *Writer) shouldUseMultiFile() bool { + // Check if multi_file is explicitly set in metadata + if w.options.Metadata != nil { + if mf, ok := w.options.Metadata["multi_file"].(bool); ok { + return mf + } + } + + // Auto-detect based on output path + if w.options.OutputPath == "" { + // No output path means stdout (single file) + return false + } + + // Check if path ends with .go (explicit file) + if strings.HasSuffix(w.options.OutputPath, ".go") { + return false + } + + // Check if path ends with directory separator + if strings.HasSuffix(w.options.OutputPath, "/") || strings.HasSuffix(w.options.OutputPath, "\\") { + return true + } + + // Check if path exists and is a directory + info, err := os.Stat(w.options.OutputPath) + if err == nil && info.IsDir() { + return true + } + + // Default to single file for ambiguous cases + return false +} + // createDatabaseRef creates a shallow copy of database without schemas to avoid circular references func (w *Writer) createDatabaseRef(db *models.Database) *models.Database { return &models.Database{