refactor: ♻️ change resolvspec types to build in sqltypes
This commit is contained in:
@@ -6,6 +6,8 @@ Generates Go source files with GORM model definitions from database schema infor
|
||||
|
||||
The GORM Writer converts RelSpec's internal database model representation into Go source code with GORM struct definitions, complete with proper tags, relationships, and methods.
|
||||
|
||||
With `--types sqltypes`, nullable fields use the [`pkg/sqltypes`](../../sqltypes/README.md) package.
|
||||
|
||||
## Features
|
||||
|
||||
- Generates GORM-compatible Go structs
|
||||
@@ -48,19 +50,19 @@ func main() {
|
||||
### CLI Examples
|
||||
|
||||
```bash
|
||||
# Generate GORM models from a DBML schema (default: resolvespec types)
|
||||
# Generate GORM models from a DBML schema (default: baselib pointer types)
|
||||
relspec convert --from dbml --from-path schema.dbml \
|
||||
--to gorm --to-path models.go --package models
|
||||
|
||||
# Use standard library database/sql nullable types instead of resolvespec
|
||||
# Use standard library database/sql nullable types instead
|
||||
relspec convert --from dbml --from-path schema.dbml \
|
||||
--to gorm --to-path models.go --package models \
|
||||
--types stdlib
|
||||
|
||||
# Explicitly select resolvespec types (same as omitting --types)
|
||||
# Select sqltypes package types (git.warky.dev/wdevs/relspecgo/pkg/sqltypes)
|
||||
relspec convert --from pgsql --from-conn "postgres://localhost/mydb" \
|
||||
--to gorm --to-path models.go --package models \
|
||||
--types resolvespec
|
||||
--types sqltypes
|
||||
|
||||
# Multi-file output (one file per table)
|
||||
relspec convert --from json --from-path schema.json \
|
||||
@@ -89,13 +91,13 @@ Files are named: `sql_{schema}_{table}.go`
|
||||
|
||||
## Generated Code Examples
|
||||
|
||||
### Default — resolvespec types (`--types resolvespec`)
|
||||
### sqltypes package types (`--types sqltypes`)
|
||||
|
||||
```go
|
||||
package models
|
||||
|
||||
import (
|
||||
sql_types "github.com/bitechdev/ResolveSpec/pkg/spectypes"
|
||||
sql_types "git.warky.dev/wdevs/relspecgo/pkg/sqltypes"
|
||||
)
|
||||
|
||||
type ModelUser struct {
|
||||
@@ -141,11 +143,11 @@ func (ModelUser) TableName() string {
|
||||
Controls which Go package is used for nullable column types. Set via the `--types` CLI flag or `WriterOptions.NullableTypes`:
|
||||
|
||||
```go
|
||||
// Use resolvespec types (default — omit NullableTypes or set to "resolvespec")
|
||||
// Use sqltypes package types
|
||||
options := &writers.WriterOptions{
|
||||
OutputPath: "models.go",
|
||||
PackageName: "models",
|
||||
NullableTypes: writers.NullableTypeResolveSpec,
|
||||
NullableTypes: writers.NullableTypeSqlTypes,
|
||||
}
|
||||
|
||||
// Use standard library database/sql types
|
||||
@@ -176,7 +178,7 @@ options := &writers.WriterOptions{
|
||||
|
||||
The nullable type package is selected with `--types` (or `WriterOptions.NullableTypes`).
|
||||
|
||||
| SQL Type | NOT NULL — both | Nullable — resolvespec | Nullable — stdlib |
|
||||
| SQL Type | NOT NULL — both | Nullable — sqltypes | Nullable — stdlib |
|
||||
|---|---|---|---|
|
||||
| `bigint` | `int64` | `SqlInt64` | `sql.NullInt64` |
|
||||
| `integer` | `int32` | `SqlInt32` | `sql.NullInt32` |
|
||||
|
||||
@@ -12,12 +12,12 @@ import (
|
||||
// TypeMapper handles type conversions between SQL and Go types
|
||||
type TypeMapper struct {
|
||||
sqlTypesAlias string
|
||||
typeStyle string // writers.NullableTypeResolveSpec | writers.NullableTypeStdlib
|
||||
typeStyle string // writers.NullableTypeSqlTypes | writers.NullableTypeStdlib | writers.NullableTypeBaselib
|
||||
}
|
||||
|
||||
// NewTypeMapper creates a new TypeMapper.
|
||||
// typeStyle should be writers.NullableTypeResolveSpec or writers.NullableTypeStdlib;
|
||||
// an empty string defaults to resolvespec.
|
||||
// typeStyle should be writers.NullableTypeSqlTypes, writers.NullableTypeStdlib, or
|
||||
// writers.NullableTypeBaselib; an empty string defaults to baselib.
|
||||
func NewTypeMapper(typeStyle string) *TypeMapper {
|
||||
if typeStyle == "" {
|
||||
typeStyle = writers.NullableTypeBaselib
|
||||
@@ -50,7 +50,7 @@ func (tm *TypeMapper) SQLTypeToGoType(sqlType string, notNull bool) string {
|
||||
return tm.baselibNullableGoType(baseType)
|
||||
}
|
||||
|
||||
// resolvespec (default)
|
||||
// sqltypes
|
||||
if notNull {
|
||||
return tm.baseGoType(baseType)
|
||||
}
|
||||
@@ -505,9 +505,9 @@ func (tm *TypeMapper) NeedsFmtImport(generateGetIDStr bool) bool {
|
||||
return generateGetIDStr
|
||||
}
|
||||
|
||||
// GetSQLTypesImport returns the import path for the spectypes package.
|
||||
// GetSQLTypesImport returns the import path for the sqltypes package.
|
||||
func (tm *TypeMapper) GetSQLTypesImport() string {
|
||||
return "git.warky.dev/wdevs/relspecgo/pkg/spectypes"
|
||||
return "git.warky.dev/wdevs/relspecgo/pkg/sqltypes"
|
||||
}
|
||||
|
||||
// GetNullableTypeImportLine returns the full Go import line for the nullable type
|
||||
|
||||
@@ -77,7 +77,7 @@ func (w *Writer) writeSingleFile(db *models.Database) error {
|
||||
packageName := w.getPackageName()
|
||||
templateData := NewTemplateData(packageName, w.config)
|
||||
|
||||
// Add nullable types import (resolvespec or stdlib depending on options)
|
||||
// Add nullable types import (sqltypes or stdlib depending on options)
|
||||
templateData.AddImport(w.typeMapper.GetNullableTypeImportLine())
|
||||
|
||||
// Collect all models
|
||||
@@ -171,7 +171,7 @@ func (w *Writer) writeMultiFile(db *models.Database) error {
|
||||
// Create template data for this single table
|
||||
templateData := NewTemplateData(packageName, w.config)
|
||||
|
||||
// Add nullable types import (resolvespec or stdlib depending on options)
|
||||
// Add nullable types import (sqltypes or stdlib depending on options)
|
||||
templateData.AddImport(w.typeMapper.GetNullableTypeImportLine())
|
||||
|
||||
// Create model data
|
||||
|
||||
Reference in New Issue
Block a user