Fixed bun/gorm writer logic for multi files.
This commit is contained in:
@@ -41,12 +41,7 @@ func NewWriter(options *writers.WriterOptions) *Writer {
|
|||||||
// WriteDatabase writes a complete database as Bun models
|
// WriteDatabase writes a complete database as Bun models
|
||||||
func (w *Writer) WriteDatabase(db *models.Database) error {
|
func (w *Writer) WriteDatabase(db *models.Database) error {
|
||||||
// Check if multi-file mode is enabled
|
// Check if multi-file mode is enabled
|
||||||
multiFile := false
|
multiFile := w.shouldUseMultiFile()
|
||||||
if w.options.Metadata != nil {
|
|
||||||
if mf, ok := w.options.Metadata["multi_file"].(bool); ok {
|
|
||||||
multiFile = mf
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if multiFile {
|
if multiFile {
|
||||||
return w.writeMultiFile(db)
|
return w.writeMultiFile(db)
|
||||||
@@ -346,6 +341,41 @@ func (w *Writer) writeOutput(content string) error {
|
|||||||
return nil
|
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
|
// createDatabaseRef creates a shallow copy of database without schemas to avoid circular references
|
||||||
func (w *Writer) createDatabaseRef(db *models.Database) *models.Database {
|
func (w *Writer) createDatabaseRef(db *models.Database) *models.Database {
|
||||||
return &models.Database{
|
return &models.Database{
|
||||||
|
|||||||
@@ -41,12 +41,7 @@ func NewWriter(options *writers.WriterOptions) *Writer {
|
|||||||
// WriteDatabase writes a complete database as GORM models
|
// WriteDatabase writes a complete database as GORM models
|
||||||
func (w *Writer) WriteDatabase(db *models.Database) error {
|
func (w *Writer) WriteDatabase(db *models.Database) error {
|
||||||
// Check if multi-file mode is enabled
|
// Check if multi-file mode is enabled
|
||||||
multiFile := false
|
multiFile := w.shouldUseMultiFile()
|
||||||
if w.options.Metadata != nil {
|
|
||||||
if mf, ok := w.options.Metadata["multi_file"].(bool); ok {
|
|
||||||
multiFile = mf
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if multiFile {
|
if multiFile {
|
||||||
return w.writeMultiFile(db)
|
return w.writeMultiFile(db)
|
||||||
@@ -340,6 +335,41 @@ func (w *Writer) writeOutput(content string) error {
|
|||||||
return nil
|
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
|
// createDatabaseRef creates a shallow copy of database without schemas to avoid circular references
|
||||||
func (w *Writer) createDatabaseRef(db *models.Database) *models.Database {
|
func (w *Writer) createDatabaseRef(db *models.Database) *models.Database {
|
||||||
return &models.Database{
|
return &models.Database{
|
||||||
|
|||||||
Reference in New Issue
Block a user