Files
relspecgo/pkg/writers/json/writer.go
Hein 5d60bc3b2c
Some checks are pending
CI / Test (1.23) (push) Waiting to run
CI / Test (1.24) (push) Waiting to run
CI / Test (1.25) (push) Waiting to run
CI / Lint (push) Waiting to run
CI / Build (push) Waiting to run
Bugs Fixed
1. pkg/models/models.go:184 - Fixed typo in ForeignKeyConstraint constant from "foreign_Key" to "foreign_key"
  2. pkg/readers/drawdb/reader.go:62-68 - Fixed ReadSchema() to properly detect schema name from tables instead of hardcoding "default"
  3. pkg/writers/dbml/writer.go:128 - Changed primary key attribute from "primary key" to DBML standard "pk"
  4. pkg/writers/dbml/writer.go:208-221 - Fixed foreign key reference format to use "table.column" syntax for single columns instead of "table.(column)"

  Test Results

  All reader and writer tests are now passing:

  Readers:
  - DBML: 74.4% coverage (2 tests skipped due to missing parser features for Ref statements)
  - DCTX: 77.6% coverage
  - DrawDB: 83.6% coverage
  - JSON: 82.1% coverage
  - YAML: 82.1% coverage

  Writers:
  - Bun: 68.5% coverage
  - DBML: 91.5% coverage
  - DCTX: 100.0% coverage
  - DrawDB: 83.8% coverage
  - GORM: 69.2% coverage
  - JSON: 82.4% coverage
  - YAML: 82.4% coverage
2025-12-16 21:43:45 +02:00

65 lines
1.5 KiB
Go

package json
import (
"encoding/json"
"fmt"
"os"
"git.warky.dev/wdevs/relspecgo/pkg/models"
"git.warky.dev/wdevs/relspecgo/pkg/writers"
)
// Writer implements the writers.Writer interface for JSON format
type Writer struct {
options *writers.WriterOptions
}
// NewWriter creates a new JSON writer with the given options
func NewWriter(options *writers.WriterOptions) *Writer {
return &Writer{
options: options,
}
}
// WriteDatabase writes a complete database as JSON
func (w *Writer) WriteDatabase(db *models.Database) error {
// Pretty print JSON with indentation
data, err := json.MarshalIndent(db, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal database to JSON: %w", err)
}
return w.writeOutput(data)
}
// WriteSchema writes a schema as JSON
func (w *Writer) WriteSchema(schema *models.Schema) error {
data, err := json.MarshalIndent(schema, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal schema to JSON: %w", err)
}
return w.writeOutput(data)
}
// WriteTable writes a single table as JSON
func (w *Writer) WriteTable(table *models.Table) error {
data, err := json.MarshalIndent(table, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal table to JSON: %w", err)
}
return w.writeOutput(data)
}
// writeOutput writes the content to file or stdout
func (w *Writer) writeOutput(data []byte) error {
if w.options.OutputPath != "" {
return os.WriteFile(w.options.OutputPath, data, 0644)
}
// Print to stdout
fmt.Println(string(data))
return nil
}