Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added CLI Flags for Package name customization for Model, Table, View and Enum #424

Merged
merged 8 commits into from
Nov 26, 2024
Merged
33 changes: 23 additions & 10 deletions cmd/jet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ var (
ignoreViews string
ignoreEnums string

destDir string
destDir string
modelPkg string
tablePkg string
viewPkg string
enumPkg string
)

func init() {
Expand All @@ -66,11 +70,15 @@ func init() {
flag.StringVar(&schemaName, "schema", "public", `Database schema name. (default "public")(PostgreSQL only)`)
flag.StringVar(&params, "params", "", "Additional connection string parameters(optional). Used only if dsn is not set.")
flag.StringVar(&sslmode, "sslmode", "disable", `Whether or not to use SSL. Used only if dsn is not set. (optional)(default "disable")(PostgreSQL only)`)
flag.StringVar(&ignoreTables, "ignore-tables", "", `Comma-separated list of tables to ignore`)
flag.StringVar(&ignoreViews, "ignore-views", "", `Comma-separated list of views to ignore`)
flag.StringVar(&ignoreEnums, "ignore-enums", "", `Comma-separated list of enums to ignore`)

flag.StringVar(&destDir, "path", "", "Destination dir for files generated.")
flag.StringVar(&ignoreTables, "ignore-tables", "", `Comma-separated list of tables to ignore.`)
flag.StringVar(&ignoreViews, "ignore-views", "", `Comma-separated list of views to ignore.`)
flag.StringVar(&ignoreEnums, "ignore-enums", "", `Comma-separated list of enums to ignore.`)

flag.StringVar(&destDir, "path", "", "Destination directory for files generated.")
flag.StringVar(&modelPkg, "rel-model-path", "model", "Relative path for the Model files package from the destination directory.")
flag.StringVar(&tablePkg, "rel-table-path", "table", "Relative path for the Table files package from the destination directory.")
flag.StringVar(&viewPkg, "rel-view-path", "view", "Relative path for the View files package from the destination directory.")
flag.StringVar(&enumPkg, "rel-enum-path", "enum", "Relative path for the Enum files package from the destination directory.")
}

func main() {
Expand Down Expand Up @@ -170,6 +178,7 @@ func usage() {
"source", "dsn", "host", "port", "user", "password", "dbname", "schema", "params", "sslmode",
"path",
"ignore-tables", "ignore-views", "ignore-enums",
"rel-model-path", "rel-table-path", "rel-view-path", "rel-enum-path",
}

for _, name := range order {
Expand All @@ -186,6 +195,7 @@ func usage() {
$ jet -source=postgres -dsn="user=jet password=jet host=localhost port=5432 dbname=jetdb" -schema=dvds -path=./gen
$ jet -source=mysql -host=localhost -port=3306 -user=jet -password=jet -dbname=jetdb -path=./gen
$ jet -source=sqlite -dsn="file://path/to/sqlite/database/file" -path=./gen
$ jet -source=sqlite -dsn="file://path/to/sqlite/database/file" -path=./gen -rel-model-path=./entity
`)
}

Expand Down Expand Up @@ -246,7 +256,7 @@ func genTemplate(dialect jet.Dialect, ignoreTables []string, ignoreViews []strin
return template.Default(dialect).
UseSchema(func(schemaMetaData metadata.Schema) template.Schema {
return template.DefaultSchema(schemaMetaData).
UseModel(template.DefaultModel().
UseModel(template.DefaultModel().UsePath(modelPkg).
UseTable(func(table metadata.Table) template.TableModel {
if shouldSkipTable(table) {
return template.TableModel{Skip: true}
Expand All @@ -271,19 +281,22 @@ func genTemplate(dialect jet.Dialect, ignoreTables []string, ignoreViews []strin
if shouldSkipTable(table) {
return template.TableSQLBuilder{Skip: true}
}
return template.DefaultTableSQLBuilder(table)

return template.DefaultTableSQLBuilder(table).UsePath(tablePkg)
}).
UseView(func(table metadata.Table) template.ViewSQLBuilder {
if shouldSkipView(table) {
return template.ViewSQLBuilder{Skip: true}
}
return template.DefaultViewSQLBuilder(table)

return template.DefaultViewSQLBuilder(table).UsePath(viewPkg)
}).
UseEnum(func(enum metadata.Enum) template.EnumSQLBuilder {
if shouldSkipEnum(enum) {
return template.EnumSQLBuilder{Skip: true}
}
return template.DefaultEnumSQLBuilder(enum)

return template.DefaultEnumSQLBuilder(enum).UsePath(enumPkg)
}),
)
})
Expand Down
4 changes: 2 additions & 2 deletions generator/postgres/postgres_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"database/sql"
"fmt"
"net/url"
"path"
"path/filepath"
"strconv"

"github.com/go-jet/jet/v2/generator/metadata"
Expand Down Expand Up @@ -66,7 +66,7 @@ func GenerateDSN(dsn, schema, destDir string, templates ...template.Template) er
return fmt.Errorf("failed to get '%s' schema metadata: %w", schema, err)
}

dirPath := path.Join(destDir, cfg.Database)
dirPath := filepath.Join(destDir, cfg.Database)

err = template.ProcessSchema(dirPath, schemaMetadata, generatorTemplate)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions generator/template/model_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/go-jet/jet/v2/internal/utils/dbidentifier"
"github.com/google/uuid"
"github.com/jackc/pgtype"
"path"
"path/filepath"
"reflect"
"strings"
"time"
Expand All @@ -23,7 +23,7 @@ type Model struct {

// PackageName returns package name of model types
func (m Model) PackageName() string {
return path.Base(m.Path)
return filepath.Base(m.Path)
}

// UsePath returns new Model template with replaced file path
Expand Down
14 changes: 7 additions & 7 deletions generator/template/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"errors"
"fmt"
"github.com/go-jet/jet/v2/internal/utils/filesys"
"path"
"path/filepath"
"strings"
"text/template"

Expand All @@ -20,7 +20,7 @@ func ProcessSchema(dirPath string, schemaMetaData metadata.Schema, generatorTemp
}

schemaTemplate := generatorTemplate.Schema(schemaMetaData)
schemaPath := path.Join(dirPath, schemaTemplate.Path)
schemaPath := filepath.Join(dirPath, schemaTemplate.Path)

fmt.Println("Destination directory:", schemaPath)
fmt.Println("Cleaning up destination directory...")
Expand Down Expand Up @@ -50,7 +50,7 @@ func processModel(dirPath string, schemaMetaData metadata.Schema, schemaTemplate
return nil
}

modelDirPath := path.Join(dirPath, modelTemplate.Path)
modelDirPath := filepath.Join(dirPath, modelTemplate.Path)

err := filesys.EnsureDirPathExist(modelDirPath)
if err != nil {
Expand Down Expand Up @@ -83,7 +83,7 @@ func processSQLBuilder(dirPath string, dialect jet.Dialect, schemaMetaData metad
return nil
}

sqlBuilderPath := path.Join(dirPath, sqlBuilderTemplate.Path)
sqlBuilderPath := filepath.Join(dirPath, sqlBuilderTemplate.Path)

err := processTableSQLBuilder("table", sqlBuilderPath, dialect, schemaMetaData, schemaMetaData.TablesMetaData, sqlBuilderTemplate)
if err != nil {
Expand Down Expand Up @@ -117,7 +117,7 @@ func processEnumSQLBuilder(dirPath string, dialect jet.Dialect, enumsMetaData []
continue
}

enumSQLBuilderPath := path.Join(dirPath, enumTemplate.Path)
enumSQLBuilderPath := filepath.Join(dirPath, enumTemplate.Path)

err := filesys.EnsureDirPathExist(enumSQLBuilderPath)
if err != nil {
Expand Down Expand Up @@ -182,7 +182,7 @@ func processTableSQLBuilder(fileTypes, dirPath string,
continue
}

tableSQLBuilderPath := path.Join(dirPath, tableSQLBuilder.Path)
tableSQLBuilderPath := filepath.Join(dirPath, tableSQLBuilder.Path)

err := filesys.EnsureDirPathExist(tableSQLBuilderPath)
if err != nil {
Expand Down Expand Up @@ -255,7 +255,7 @@ func generateUseSchemaFunc(dirPath, fileTypes string, builders []TableSQLBuilder
return fmt.Errorf("failed to generate use schema template: %w", err)
}

basePath := path.Join(dirPath, builders[0].Path)
basePath := filepath.Join(dirPath, builders[0].Path)
fileName := fileTypes + "_use_schema"

err = filesys.FormatAndSaveGoFile(basePath, fileName, text)
Expand Down
6 changes: 3 additions & 3 deletions generator/template/sql_builder_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"github.com/go-jet/jet/v2/generator/metadata"
"github.com/go-jet/jet/v2/internal/utils/dbidentifier"
"path"
"path/filepath"
"slices"
"strings"
"unicode"
Expand Down Expand Up @@ -90,7 +90,7 @@ func DefaultViewSQLBuilder(viewMetaData metadata.Table) ViewSQLBuilder {

// PackageName returns package name of table sql builder types
func (tb TableSQLBuilder) PackageName() string {
return path.Base(tb.Path)
return filepath.Base(tb.Path)
}

// UsePath returns new TableSQLBuilder with new relative path set
Expand Down Expand Up @@ -228,7 +228,7 @@ func DefaultEnumSQLBuilder(enumMetaData metadata.Enum) EnumSQLBuilder {

// PackageName returns enum sql builder package name
func (e EnumSQLBuilder) PackageName() string {
return path.Base(e.Path)
return filepath.Base(e.Path)
}

// UsePath returns new EnumSQLBuilder with new path set
Expand Down
6 changes: 3 additions & 3 deletions tests/internal/utils/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package file
import (
"github.com/stretchr/testify/require"
"os"
"path"
"path/filepath"
"testing"
)

// Exists expects file to exist on path constructed from pathElems and returns content of the file
func Exists(t *testing.T, pathElems ...string) (fileContent string) {
modelFilePath := path.Join(pathElems...)
modelFilePath := filepath.Join(pathElems...)
file, err := os.ReadFile(modelFilePath) // #nosec G304
require.Nil(t, err)
require.NotEmpty(t, file)
Expand All @@ -18,7 +18,7 @@ func Exists(t *testing.T, pathElems ...string) (fileContent string) {

// NotExists expects file not to exist on path constructed from pathElems
func NotExists(t *testing.T, pathElems ...string) {
modelFilePath := path.Join(pathElems...)
modelFilePath := filepath.Join(pathElems...)
_, err := os.ReadFile(modelFilePath) // #nosec G304
require.True(t, os.IsNotExist(err))
}
14 changes: 7 additions & 7 deletions tests/mysql/generator_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ import (
"github.com/go-jet/jet/v2/tests/dbconfig"
file2 "github.com/go-jet/jet/v2/tests/internal/utils/file"
"github.com/stretchr/testify/require"
"path"
"path/filepath"
"testing"
)

const tempTestDir = "./.tempTestDir"

var defaultModelPath = path.Join(tempTestDir, "dvds/model")
var defaultActorModelFilePath = path.Join(tempTestDir, "dvds/model", "actor.go")
var defaultTableSQLBuilderFilePath = path.Join(tempTestDir, "dvds/table")
var defaultViewSQLBuilderFilePath = path.Join(tempTestDir, "dvds/view")
var defaultEnumSQLBuilderFilePath = path.Join(tempTestDir, "dvds/enum")
var defaultActorSQLBuilderFilePath = path.Join(tempTestDir, "dvds/table", "actor.go")
var defaultModelPath = filepath.Join(tempTestDir, "dvds/model")
var defaultActorModelFilePath = filepath.Join(tempTestDir, "dvds/model", "actor.go")
var defaultTableSQLBuilderFilePath = filepath.Join(tempTestDir, "dvds/table")
var defaultViewSQLBuilderFilePath = filepath.Join(tempTestDir, "dvds/view")
var defaultEnumSQLBuilderFilePath = filepath.Join(tempTestDir, "dvds/enum")
var defaultActorSQLBuilderFilePath = filepath.Join(tempTestDir, "dvds/table", "actor.go")

func dbConnection(dbName string) mysql2.DBConnection {
if sourceIsMariaDB() {
Expand Down
16 changes: 8 additions & 8 deletions tests/postgres/generator_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package postgres
import (
"database/sql"
"fmt"
"path"
"path/filepath"
"testing"

"github.com/go-jet/jet/v2/generator/metadata"
Expand All @@ -20,13 +20,13 @@ import (

const tempTestDir = "./.tempTestDir"

var defaultModelPath = path.Join(tempTestDir, "jetdb/dvds/model")
var defaultSqlBuilderPath = path.Join(tempTestDir, "jetdb/dvds/table")
var defaultActorModelFilePath = path.Join(tempTestDir, "jetdb/dvds/model", "actor.go")
var defaultTableSQLBuilderFilePath = path.Join(tempTestDir, "jetdb/dvds/table")
var defaultViewSQLBuilderFilePath = path.Join(tempTestDir, "jetdb/dvds/view")
var defaultEnumSQLBuilderFilePath = path.Join(tempTestDir, "jetdb/dvds/enum")
var defaultActorSQLBuilderFilePath = path.Join(tempTestDir, "jetdb/dvds/table", "actor.go")
var defaultModelPath = filepath.Join(tempTestDir, "jetdb/dvds/model")
var defaultSqlBuilderPath = filepath.Join(tempTestDir, "jetdb/dvds/table")
var defaultActorModelFilePath = filepath.Join(tempTestDir, "jetdb/dvds/model", "actor.go")
var defaultTableSQLBuilderFilePath = filepath.Join(tempTestDir, "jetdb/dvds/table")
var defaultViewSQLBuilderFilePath = filepath.Join(tempTestDir, "jetdb/dvds/view")
var defaultEnumSQLBuilderFilePath = filepath.Join(tempTestDir, "jetdb/dvds/enum")
var defaultActorSQLBuilderFilePath = filepath.Join(tempTestDir, "jetdb/dvds/table", "actor.go")

var dbConnection = postgres.DBConnection{
Host: dbconfig.PgHost,
Expand Down
Loading
Loading