From 94412b797cee45d48641facc9f9d40a4b2cc8150 Mon Sep 17 00:00:00 2001 From: Jamius Siam Date: Thu, 21 Nov 2024 00:48:12 +0600 Subject: [PATCH 1/8] Added package name CLI flags for Model, Table, View and Enum --- cmd/jet/main.go | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/cmd/jet/main.go b/cmd/jet/main.go index b56ef31a..e213aefb 100644 --- a/cmd/jet/main.go +++ b/cmd/jet/main.go @@ -42,7 +42,11 @@ var ( ignoreViews string ignoreEnums string - destDir string + destDir string + modelPkg string + tablePkg string + viewPkg string + enumPkg string ) func init() { @@ -71,6 +75,10 @@ func init() { flag.StringVar(&ignoreEnums, "ignore-enums", "", `Comma-separated list of enums to ignore`) flag.StringVar(&destDir, "path", "", "Destination dir for files generated.") + flag.StringVar(&modelPkg, "model-pkg", "", "Package name for the model files generated") + flag.StringVar(&tablePkg, "table-pkg", "", "Package name for the table files generated") + flag.StringVar(&viewPkg, "view-pkg", "", "Package name for the view files generated") + flag.StringVar(&enumPkg, "enum-pkg", "", "Package name for the enum files generated") } func main() { @@ -170,6 +178,7 @@ func usage() { "source", "dsn", "host", "port", "user", "password", "dbname", "schema", "params", "sslmode", "path", "ignore-tables", "ignore-views", "ignore-enums", + "model-pkg", "table-pkg", "view-pkg", "enum-pkg", } for _, name := range order { @@ -243,10 +252,16 @@ func genTemplate(dialect jet.Dialect, ignoreTables []string, ignoreViews []strin return strslice.Contains(ignoreEnums, strings.ToLower(enum.Name)) } + model := template.DefaultModel() + + if modelPkg != "" { + model = model.UsePath("/" + modelPkg) + } + return template.Default(dialect). UseSchema(func(schemaMetaData metadata.Schema) template.Schema { return template.DefaultSchema(schemaMetaData). - UseModel(template.DefaultModel(). + UseModel(model. UseTable(func(table metadata.Table) template.TableModel { if shouldSkipTable(table) { return template.TableModel{Skip: true} @@ -271,18 +286,33 @@ func genTemplate(dialect jet.Dialect, ignoreTables []string, ignoreViews []strin if shouldSkipTable(table) { return template.TableSQLBuilder{Skip: true} } + + if tablePkg != "" { + return template.DefaultTableSQLBuilder(table).UsePath("/" + tablePkg) + } + return template.DefaultTableSQLBuilder(table) }). UseView(func(table metadata.Table) template.ViewSQLBuilder { if shouldSkipView(table) { return template.ViewSQLBuilder{Skip: true} } + + if viewPkg != "" { + return template.DefaultViewSQLBuilder(table).UsePath("/" + viewPkg) + } + return template.DefaultViewSQLBuilder(table) }). UseEnum(func(enum metadata.Enum) template.EnumSQLBuilder { if shouldSkipEnum(enum) { return template.EnumSQLBuilder{Skip: true} } + + if enumPkg != "" { + return template.DefaultEnumSQLBuilder(enum).UsePath("/" + enumPkg) + } + return template.DefaultEnumSQLBuilder(enum) }), ) From 47fd02fb8d7ff7037fa3ee1d2c01fa80c2b8c0f1 Mon Sep 17 00:00:00 2001 From: Jamius Siam Date: Thu, 21 Nov 2024 18:12:54 +0600 Subject: [PATCH 2/8] Added test for the CLI flags for Model, Table, View and Enum --- tests/postgres/generator_test.go | 337 +++++++++++++++++++++++++++++++ 1 file changed, 337 insertions(+) diff --git a/tests/postgres/generator_test.go b/tests/postgres/generator_test.go index 87a7eeec..a03864bc 100644 --- a/tests/postgres/generator_test.go +++ b/tests/postgres/generator_test.go @@ -108,6 +108,34 @@ func TestCmdGenerator(t *testing.T) { require.NoError(t, err) } +func TestCmdGeneratorWithPkgNames(t *testing.T) { + err := os.RemoveAll(genTestDir2) + require.NoError(t, err) + + cmd := exec.Command("jet", "-source=PostgreSQL", "-dbname=jetdb", "-host=localhost", + "-port="+strconv.Itoa(dbconfig.PgPort), + "-user=jet", + "-password=jet", + "-schema=dvds", + "-path="+genTestDir2, + "-model-pkg=newmodel", + "-table-pkg=newtable", + "-view-pkg=newview", + "-enum-pkg=newenum", + ) + + cmd.Stderr = os.Stderr + cmd.Stdout = os.Stdout + + err = cmd.Run() + require.NoError(t, err) + + assertGeneratedFilesWithPkgNames(t) + + err = os.RemoveAll(genTestDir2) + require.NoError(t, err) +} + func TestGeneratorIgnoreTables(t *testing.T) { tests := []struct { name string @@ -311,6 +339,38 @@ func assertGeneratedFiles(t *testing.T) { testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/model/actor.go", actorModelFile) } +func assertGeneratedFilesWithPkgNames(t *testing.T) { + // Table SQL Builder files + testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/newtable", + "actor.go", "address.go", "category.go", "city.go", "country.go", + "customer.go", "film.go", "film_actor.go", "film_category.go", "inventory.go", "language.go", + "payment.go", "rental.go", "staff.go", "store.go", "table_use_schema.go") + + testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/newtable/actor.go", actorSQLBuilderFileWithPkgName) + testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/newtable/table_use_schema.go", tableUseSchemaFileWithPkgName) + + // View SQL Builder files + testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/newview", + "actor_info.go", "film_list.go", "nicer_but_slower_film_list.go", + "sales_by_film_category.go", "customer_list.go", "sales_by_store.go", "staff_list.go", "view_use_schema.go") + + testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/newview/actor_info.go", actorInfoSQLBuilderFileWithPkgName) + testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/newview/view_use_schema.go", viewUseSchemaFileWithPkgName) + + // Enums SQL Builder files + testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/newenum", "mpaa_rating.go") + testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/newenum/mpaa_rating.go", mpaaRatingEnumFileWithPkgName) + + // Model files + testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/newmodel", "actor.go", "address.go", "category.go", "city.go", "country.go", + "customer.go", "film.go", "film_actor.go", "film_category.go", "inventory.go", "language.go", + "payment.go", "rental.go", "staff.go", "store.go", "mpaa_rating.go", + "actor_info.go", "film_list.go", "nicer_but_slower_film_list.go", "sales_by_film_category.go", + "customer_list.go", "sales_by_store.go", "staff_list.go") + + testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/newmodel/actor.go", actorModelFileWithPkgName) +} + var mpaaRatingEnumFile = ` // // Code generated by go-jet DO NOT EDIT. @@ -338,6 +398,33 @@ var MpaaRating = &struct { } ` +var mpaaRatingEnumFileWithPkgName = ` +// +// Code generated by go-jet DO NOT EDIT. +// +// WARNING: Changes to this file may cause incorrect behavior +// and will be lost if the code is regenerated +// + +package newenum + +import "github.com/go-jet/jet/v2/postgres" + +var MpaaRating = &struct { + G postgres.StringExpression + Pg postgres.StringExpression + Pg13 postgres.StringExpression + R postgres.StringExpression + Nc17 postgres.StringExpression +}{ + G: postgres.NewEnumValue("G"), + Pg: postgres.NewEnumValue("PG"), + Pg13: postgres.NewEnumValue("PG-13"), + R: postgres.NewEnumValue("R"), + Nc17: postgres.NewEnumValue("NC-17"), +} +` + var actorSQLBuilderFile = ` // // Code generated by go-jet DO NOT EDIT. @@ -425,6 +512,93 @@ func newActorTableImpl(schemaName, tableName, alias string) actorTable { } ` +var actorSQLBuilderFileWithPkgName = ` +// +// Code generated by go-jet DO NOT EDIT. +// +// WARNING: Changes to this file may cause incorrect behavior +// and will be lost if the code is regenerated +// + +package newtable + +import ( + "github.com/go-jet/jet/v2/postgres" +) + +var Actor = newActorTable("dvds", "actor", "") + +type actorTable struct { + postgres.Table + + // Columns + ActorID postgres.ColumnInteger + FirstName postgres.ColumnString + LastName postgres.ColumnString + LastUpdate postgres.ColumnTimestamp + + AllColumns postgres.ColumnList + MutableColumns postgres.ColumnList +} + +type ActorTable struct { + actorTable + + EXCLUDED actorTable +} + +// AS creates new ActorTable with assigned alias +func (a ActorTable) AS(alias string) *ActorTable { + return newActorTable(a.SchemaName(), a.TableName(), alias) +} + +// Schema creates new ActorTable with assigned schema name +func (a ActorTable) FromSchema(schemaName string) *ActorTable { + return newActorTable(schemaName, a.TableName(), a.Alias()) +} + +// WithPrefix creates new ActorTable with assigned table prefix +func (a ActorTable) WithPrefix(prefix string) *ActorTable { + return newActorTable(a.SchemaName(), prefix+a.TableName(), a.TableName()) +} + +// WithSuffix creates new ActorTable with assigned table suffix +func (a ActorTable) WithSuffix(suffix string) *ActorTable { + return newActorTable(a.SchemaName(), a.TableName()+suffix, a.TableName()) +} + +func newActorTable(schemaName, tableName, alias string) *ActorTable { + return &ActorTable{ + actorTable: newActorTableImpl(schemaName, tableName, alias), + EXCLUDED: newActorTableImpl("", "excluded", ""), + } +} + +func newActorTableImpl(schemaName, tableName, alias string) actorTable { + var ( + ActorIDColumn = postgres.IntegerColumn("actor_id") + FirstNameColumn = postgres.StringColumn("first_name") + LastNameColumn = postgres.StringColumn("last_name") + LastUpdateColumn = postgres.TimestampColumn("last_update") + allColumns = postgres.ColumnList{ActorIDColumn, FirstNameColumn, LastNameColumn, LastUpdateColumn} + mutableColumns = postgres.ColumnList{FirstNameColumn, LastNameColumn, LastUpdateColumn} + ) + + return actorTable{ + Table: postgres.NewTable(schemaName, tableName, alias, allColumns...), + + //Columns + ActorID: ActorIDColumn, + FirstName: FirstNameColumn, + LastName: LastNameColumn, + LastUpdate: LastUpdateColumn, + + AllColumns: allColumns, + MutableColumns: mutableColumns, + } +} +` + var tableUseSchemaFile = ` // // Code generated by go-jet DO NOT EDIT. @@ -456,6 +630,37 @@ func UseSchema(schema string) { } ` +var tableUseSchemaFileWithPkgName = ` +// +// Code generated by go-jet DO NOT EDIT. +// +// WARNING: Changes to this file may cause incorrect behavior +// and will be lost if the code is regenerated +// + +package newtable + +// UseSchema sets a new schema name for all generated table SQL builder types. It is recommended to invoke +// this method only once at the beginning of the program. +func UseSchema(schema string) { + Actor = Actor.FromSchema(schema) + Address = Address.FromSchema(schema) + Category = Category.FromSchema(schema) + City = City.FromSchema(schema) + Country = Country.FromSchema(schema) + Customer = Customer.FromSchema(schema) + Film = Film.FromSchema(schema) + FilmActor = FilmActor.FromSchema(schema) + FilmCategory = FilmCategory.FromSchema(schema) + Inventory = Inventory.FromSchema(schema) + Language = Language.FromSchema(schema) + Payment = Payment.FromSchema(schema) + Rental = Rental.FromSchema(schema) + Staff = Staff.FromSchema(schema) + Store = Store.FromSchema(schema) +} +` + var actorModelFile = ` // // Code generated by go-jet DO NOT EDIT. @@ -478,6 +683,28 @@ type Actor struct { } ` +var actorModelFileWithPkgName = ` +// +// Code generated by go-jet DO NOT EDIT. +// +// WARNING: Changes to this file may cause incorrect behavior +// and will be lost if the code is regenerated +// + +package newmodel + +import ( + "time" +) + +type Actor struct { + ActorID int32 ` + "`sql:\"primary_key\"`" + ` + FirstName string + LastName string + LastUpdate time.Time +} +` + var actorInfoSQLBuilderFile = ` // // Code generated by go-jet DO NOT EDIT. @@ -565,6 +792,93 @@ func newActorInfoTableImpl(schemaName, tableName, alias string) actorInfoTable { } ` +var actorInfoSQLBuilderFileWithPkgName = ` +// +// Code generated by go-jet DO NOT EDIT. +// +// WARNING: Changes to this file may cause incorrect behavior +// and will be lost if the code is regenerated +// + +package newview + +import ( + "github.com/go-jet/jet/v2/postgres" +) + +var ActorInfo = newActorInfoTable("dvds", "actor_info", "") + +type actorInfoTable struct { + postgres.Table + + // Columns + ActorID postgres.ColumnInteger + FirstName postgres.ColumnString + LastName postgres.ColumnString + FilmInfo postgres.ColumnString + + AllColumns postgres.ColumnList + MutableColumns postgres.ColumnList +} + +type ActorInfoTable struct { + actorInfoTable + + EXCLUDED actorInfoTable +} + +// AS creates new ActorInfoTable with assigned alias +func (a ActorInfoTable) AS(alias string) *ActorInfoTable { + return newActorInfoTable(a.SchemaName(), a.TableName(), alias) +} + +// Schema creates new ActorInfoTable with assigned schema name +func (a ActorInfoTable) FromSchema(schemaName string) *ActorInfoTable { + return newActorInfoTable(schemaName, a.TableName(), a.Alias()) +} + +// WithPrefix creates new ActorInfoTable with assigned table prefix +func (a ActorInfoTable) WithPrefix(prefix string) *ActorInfoTable { + return newActorInfoTable(a.SchemaName(), prefix+a.TableName(), a.TableName()) +} + +// WithSuffix creates new ActorInfoTable with assigned table suffix +func (a ActorInfoTable) WithSuffix(suffix string) *ActorInfoTable { + return newActorInfoTable(a.SchemaName(), a.TableName()+suffix, a.TableName()) +} + +func newActorInfoTable(schemaName, tableName, alias string) *ActorInfoTable { + return &ActorInfoTable{ + actorInfoTable: newActorInfoTableImpl(schemaName, tableName, alias), + EXCLUDED: newActorInfoTableImpl("", "excluded", ""), + } +} + +func newActorInfoTableImpl(schemaName, tableName, alias string) actorInfoTable { + var ( + ActorIDColumn = postgres.IntegerColumn("actor_id") + FirstNameColumn = postgres.StringColumn("first_name") + LastNameColumn = postgres.StringColumn("last_name") + FilmInfoColumn = postgres.StringColumn("film_info") + allColumns = postgres.ColumnList{ActorIDColumn, FirstNameColumn, LastNameColumn, FilmInfoColumn} + mutableColumns = postgres.ColumnList{ActorIDColumn, FirstNameColumn, LastNameColumn, FilmInfoColumn} + ) + + return actorInfoTable{ + Table: postgres.NewTable(schemaName, tableName, alias, allColumns...), + + //Columns + ActorID: ActorIDColumn, + FirstName: FirstNameColumn, + LastName: LastNameColumn, + FilmInfo: FilmInfoColumn, + + AllColumns: allColumns, + MutableColumns: mutableColumns, + } +} +` + var viewUseSchemaFile = ` // // Code generated by go-jet DO NOT EDIT. @@ -588,6 +902,29 @@ func UseSchema(schema string) { } ` +var viewUseSchemaFileWithPkgName = ` +// +// Code generated by go-jet DO NOT EDIT. +// +// WARNING: Changes to this file may cause incorrect behavior +// and will be lost if the code is regenerated +// + +package newview + +// UseSchema sets a new schema name for all generated view SQL builder types. It is recommended to invoke +// this method only once at the beginning of the program. +func UseSchema(schema string) { + ActorInfo = ActorInfo.FromSchema(schema) + CustomerList = CustomerList.FromSchema(schema) + FilmList = FilmList.FromSchema(schema) + NicerButSlowerFilmList = NicerButSlowerFilmList.FromSchema(schema) + SalesByFilmCategory = SalesByFilmCategory.FromSchema(schema) + SalesByStore = SalesByStore.FromSchema(schema) + StaffList = StaffList.FromSchema(schema) +} +` + func TestGeneratedAllTypesSQLBuilderFiles(t *testing.T) { skipForCockroachDB(t) // because of rowid column From 5add82eacb3832df5b643add58fac4e3a51cfd40 Mon Sep 17 00:00:00 2001 From: Jamius Siam Date: Fri, 22 Nov 2024 21:37:55 +0600 Subject: [PATCH 3/8] Refactored CLI arguments --- cmd/jet/main.go | 45 ++++++++++++++------------------------------- 1 file changed, 14 insertions(+), 31 deletions(-) diff --git a/cmd/jet/main.go b/cmd/jet/main.go index e213aefb..aecbfaa1 100644 --- a/cmd/jet/main.go +++ b/cmd/jet/main.go @@ -70,15 +70,15 @@ func init() { flag.StringVar(&schemaName, "schema", "public", `Database schema name. (default "public")(PostgreSQL only)`) flag.StringVar(¶ms, "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(&modelPkg, "model-pkg", "", "Package name for the model files generated") - flag.StringVar(&tablePkg, "table-pkg", "", "Package name for the table files generated") - flag.StringVar(&viewPkg, "view-pkg", "", "Package name for the view files generated") - flag.StringVar(&enumPkg, "enum-pkg", "", "Package name for the enum 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, "model-pkg", "model", "Relative path for the Model files package from the destination directory.") + flag.StringVar(&tablePkg, "table-pkg", "table", "Relative path for the Table files package from the destination directory.") + flag.StringVar(&viewPkg, "view-pkg", "view", "Relative path for the View files package from the destination directory.") + flag.StringVar(&enumPkg, "enum-pkg", "enum", "Relative path for the Enum files package from the destination directory.") } func main() { @@ -195,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 -model-pkg=./entity `) } @@ -252,16 +253,10 @@ func genTemplate(dialect jet.Dialect, ignoreTables []string, ignoreViews []strin return strslice.Contains(ignoreEnums, strings.ToLower(enum.Name)) } - model := template.DefaultModel() - - if modelPkg != "" { - model = model.UsePath("/" + modelPkg) - } - return template.Default(dialect). UseSchema(func(schemaMetaData metadata.Schema) template.Schema { return template.DefaultSchema(schemaMetaData). - UseModel(model. + UseModel(template.DefaultModel().UsePath(modelPkg). UseTable(func(table metadata.Table) template.TableModel { if shouldSkipTable(table) { return template.TableModel{Skip: true} @@ -287,33 +282,21 @@ func genTemplate(dialect jet.Dialect, ignoreTables []string, ignoreViews []strin return template.TableSQLBuilder{Skip: true} } - if tablePkg != "" { - return template.DefaultTableSQLBuilder(table).UsePath("/" + tablePkg) - } - - 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} } - if viewPkg != "" { - return template.DefaultViewSQLBuilder(table).UsePath("/" + viewPkg) - } - - 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} } - if enumPkg != "" { - return template.DefaultEnumSQLBuilder(enum).UsePath("/" + enumPkg) - } - - return template.DefaultEnumSQLBuilder(enum) + return template.DefaultEnumSQLBuilder(enum).UsePath(enumPkg) }), ) }) From b08dd48c715cc8c2c7da9ed11d8233971036b868 Mon Sep 17 00:00:00 2001 From: Jamius Siam Date: Sat, 23 Nov 2024 00:02:35 +0600 Subject: [PATCH 4/8] Updated tests for package customization CLI args --- tests/postgres/generator_test.go | 412 +++++++++---------------------- 1 file changed, 115 insertions(+), 297 deletions(-) diff --git a/tests/postgres/generator_test.go b/tests/postgres/generator_test.go index a03864bc..48efc1dd 100644 --- a/tests/postgres/generator_test.go +++ b/tests/postgres/generator_test.go @@ -6,6 +6,7 @@ import ( "os/exec" "path/filepath" "reflect" + "regexp" "strconv" "testing" @@ -112,17 +113,22 @@ func TestCmdGeneratorWithPkgNames(t *testing.T) { err := os.RemoveAll(genTestDir2) require.NoError(t, err) + // Testing with custom package paths + modelPath := "./newmodel" + tablePath := "./newtable" + viewPath := "./newview" + enumPath := "./newenum" + cmd := exec.Command("jet", "-source=PostgreSQL", "-dbname=jetdb", "-host=localhost", "-port="+strconv.Itoa(dbconfig.PgPort), "-user=jet", "-password=jet", "-schema=dvds", "-path="+genTestDir2, - "-model-pkg=newmodel", - "-table-pkg=newtable", - "-view-pkg=newview", - "-enum-pkg=newenum", - ) + "-model-pkg="+modelPath, + "-table-pkg="+tablePath, + "-view-pkg="+viewPath, + "-enum-pkg="+enumPath) cmd.Stderr = os.Stderr cmd.Stdout = os.Stdout @@ -130,10 +136,47 @@ func TestCmdGeneratorWithPkgNames(t *testing.T) { err = cmd.Run() require.NoError(t, err) - assertGeneratedFilesWithPkgNames(t) + assertGeneratedFilesWithPkgNames( + t, + modelPath, + tablePath, + viewPath, + enumPath, + ) err = os.RemoveAll(genTestDir2) require.NoError(t, err) + + // Testing with nested paths + modelPath = "./db/newmodel" + tablePath = "./db/newtable" + viewPath = "./db/newview" + enumPath = "./db/newenum" + + cmd = exec.Command("jet", "-source=PostgreSQL", "-dbname=jetdb", "-host=localhost", + "-port="+strconv.Itoa(dbconfig.PgPort), + "-user=jet", + "-password=jet", + "-schema=dvds", + "-path="+genTestDir2, + "-model-pkg="+modelPath, + "-table-pkg="+tablePath, + "-view-pkg="+viewPath, + "-enum-pkg="+enumPath) + + cmd.Stderr = os.Stderr + cmd.Stdout = os.Stdout + + err = cmd.Run() + require.NoError(t, err) + + assertGeneratedFilesWithPkgNames( + t, + modelPath, + tablePath, + viewPath, + enumPath, + ) } func TestGeneratorIgnoreTables(t *testing.T) { @@ -339,66 +382,91 @@ func assertGeneratedFiles(t *testing.T) { testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/model/actor.go", actorModelFile) } -func assertGeneratedFilesWithPkgNames(t *testing.T) { +func assertGeneratedFilesWithPkgNames(t *testing.T, modelPkgPath, tablePkgPath, viewPkgPath, enumPkgPath string) { + // We can get the package names from the base of the package paths for + // replacing package names in the default file content strings + modelPkg := filepath.Base(modelPkgPath) + tablePkg := filepath.Base(tablePkgPath) + viewPkg := filepath.Base(viewPkgPath) + enumPkg := filepath.Base(enumPkgPath) + // Table SQL Builder files - testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/newtable", + testutils.AssertFileNamesEqual( + t, + filepath.Join("./.gentestdata2/jetdb/dvds/", tablePkgPath), "actor.go", "address.go", "category.go", "city.go", "country.go", "customer.go", "film.go", "film_actor.go", "film_category.go", "inventory.go", "language.go", - "payment.go", "rental.go", "staff.go", "store.go", "table_use_schema.go") + "payment.go", "rental.go", "staff.go", "store.go", "table_use_schema.go", + ) - testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/newtable/actor.go", actorSQLBuilderFileWithPkgName) - testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/newtable/table_use_schema.go", tableUseSchemaFileWithPkgName) + testutils.AssertFileContent( + t, + filepath.Join("./.gentestdata2/jetdb/dvds/", tablePkgPath, "actor.go"), + getFileContentWithNewPkg(tablePkg, actorSQLBuilderFile), + ) + + testutils.AssertFileContent( + t, + filepath.Join("./.gentestdata2/jetdb/dvds/", tablePkgPath, "table_use_schema.go"), + getFileContentWithNewPkg(tablePkg, tableUseSchemaFile), + ) // View SQL Builder files - testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/newview", + testutils.AssertFileNamesEqual( + t, + filepath.Join("./.gentestdata2/jetdb/dvds/", viewPkgPath), "actor_info.go", "film_list.go", "nicer_but_slower_film_list.go", - "sales_by_film_category.go", "customer_list.go", "sales_by_store.go", "staff_list.go", "view_use_schema.go") + "sales_by_film_category.go", "customer_list.go", "sales_by_store.go", "staff_list.go", "view_use_schema.go", + ) - testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/newview/actor_info.go", actorInfoSQLBuilderFileWithPkgName) - testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/newview/view_use_schema.go", viewUseSchemaFileWithPkgName) + testutils.AssertFileContent(t, + filepath.Join("./.gentestdata2/jetdb/dvds/", viewPkgPath, "actor_info.go"), + getFileContentWithNewPkg(viewPkg, actorInfoSQLBuilderFile), + ) + + testutils.AssertFileContent( + t, + filepath.Join("./.gentestdata2/jetdb/dvds/", viewPkgPath, "view_use_schema.go"), + getFileContentWithNewPkg(viewPkg, viewUseSchemaFile), + ) // Enums SQL Builder files - testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/newenum", "mpaa_rating.go") - testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/newenum/mpaa_rating.go", mpaaRatingEnumFileWithPkgName) + testutils.AssertFileNamesEqual( + t, + filepath.Join("./.gentestdata2/jetdb/dvds/", enumPkgPath), + "mpaa_rating.go", + ) + + testutils.AssertFileContent( + t, + filepath.Join("./.gentestdata2/jetdb/dvds/", enumPkgPath, "mpaa_rating.go"), + getFileContentWithNewPkg(enumPkg, mpaaRatingEnumFile), + ) // Model files - testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/newmodel", "actor.go", "address.go", "category.go", "city.go", "country.go", + testutils.AssertFileNamesEqual( + t, + filepath.Join("./.gentestdata2/jetdb/dvds/", modelPkgPath), + "actor.go", "address.go", "category.go", "city.go", "country.go", "customer.go", "film.go", "film_actor.go", "film_category.go", "inventory.go", "language.go", "payment.go", "rental.go", "staff.go", "store.go", "mpaa_rating.go", "actor_info.go", "film_list.go", "nicer_but_slower_film_list.go", "sales_by_film_category.go", - "customer_list.go", "sales_by_store.go", "staff_list.go") + "customer_list.go", "sales_by_store.go", "staff_list.go", + ) - testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/newmodel/actor.go", actorModelFileWithPkgName) + testutils.AssertFileContent( + t, + filepath.Join("./.gentestdata2/jetdb/dvds/", modelPkgPath, "actor.go"), + getFileContentWithNewPkg(modelPkg, actorModelFile), + ) } -var mpaaRatingEnumFile = ` -// -// Code generated by go-jet DO NOT EDIT. -// -// WARNING: Changes to this file may cause incorrect behavior -// and will be lost if the code is regenerated -// - -package enum - -import "github.com/go-jet/jet/v2/postgres" - -var MpaaRating = &struct { - G postgres.StringExpression - Pg postgres.StringExpression - Pg13 postgres.StringExpression - R postgres.StringExpression - Nc17 postgres.StringExpression -}{ - G: postgres.NewEnumValue("G"), - Pg: postgres.NewEnumValue("PG"), - Pg13: postgres.NewEnumValue("PG-13"), - R: postgres.NewEnumValue("R"), - Nc17: postgres.NewEnumValue("NC-17"), +func getFileContentWithNewPkg(pkgName, fileContent string) string { + regex := regexp.MustCompile(`package \w+`) + return regex.ReplaceAllString(fileContent, "package "+pkgName) } -` -var mpaaRatingEnumFileWithPkgName = ` +var mpaaRatingEnumFile = ` // // Code generated by go-jet DO NOT EDIT. // @@ -406,7 +474,7 @@ var mpaaRatingEnumFileWithPkgName = ` // and will be lost if the code is regenerated // -package newenum +package enum import "github.com/go-jet/jet/v2/postgres" @@ -512,93 +580,6 @@ func newActorTableImpl(schemaName, tableName, alias string) actorTable { } ` -var actorSQLBuilderFileWithPkgName = ` -// -// Code generated by go-jet DO NOT EDIT. -// -// WARNING: Changes to this file may cause incorrect behavior -// and will be lost if the code is regenerated -// - -package newtable - -import ( - "github.com/go-jet/jet/v2/postgres" -) - -var Actor = newActorTable("dvds", "actor", "") - -type actorTable struct { - postgres.Table - - // Columns - ActorID postgres.ColumnInteger - FirstName postgres.ColumnString - LastName postgres.ColumnString - LastUpdate postgres.ColumnTimestamp - - AllColumns postgres.ColumnList - MutableColumns postgres.ColumnList -} - -type ActorTable struct { - actorTable - - EXCLUDED actorTable -} - -// AS creates new ActorTable with assigned alias -func (a ActorTable) AS(alias string) *ActorTable { - return newActorTable(a.SchemaName(), a.TableName(), alias) -} - -// Schema creates new ActorTable with assigned schema name -func (a ActorTable) FromSchema(schemaName string) *ActorTable { - return newActorTable(schemaName, a.TableName(), a.Alias()) -} - -// WithPrefix creates new ActorTable with assigned table prefix -func (a ActorTable) WithPrefix(prefix string) *ActorTable { - return newActorTable(a.SchemaName(), prefix+a.TableName(), a.TableName()) -} - -// WithSuffix creates new ActorTable with assigned table suffix -func (a ActorTable) WithSuffix(suffix string) *ActorTable { - return newActorTable(a.SchemaName(), a.TableName()+suffix, a.TableName()) -} - -func newActorTable(schemaName, tableName, alias string) *ActorTable { - return &ActorTable{ - actorTable: newActorTableImpl(schemaName, tableName, alias), - EXCLUDED: newActorTableImpl("", "excluded", ""), - } -} - -func newActorTableImpl(schemaName, tableName, alias string) actorTable { - var ( - ActorIDColumn = postgres.IntegerColumn("actor_id") - FirstNameColumn = postgres.StringColumn("first_name") - LastNameColumn = postgres.StringColumn("last_name") - LastUpdateColumn = postgres.TimestampColumn("last_update") - allColumns = postgres.ColumnList{ActorIDColumn, FirstNameColumn, LastNameColumn, LastUpdateColumn} - mutableColumns = postgres.ColumnList{FirstNameColumn, LastNameColumn, LastUpdateColumn} - ) - - return actorTable{ - Table: postgres.NewTable(schemaName, tableName, alias, allColumns...), - - //Columns - ActorID: ActorIDColumn, - FirstName: FirstNameColumn, - LastName: LastNameColumn, - LastUpdate: LastUpdateColumn, - - AllColumns: allColumns, - MutableColumns: mutableColumns, - } -} -` - var tableUseSchemaFile = ` // // Code generated by go-jet DO NOT EDIT. @@ -630,37 +611,6 @@ func UseSchema(schema string) { } ` -var tableUseSchemaFileWithPkgName = ` -// -// Code generated by go-jet DO NOT EDIT. -// -// WARNING: Changes to this file may cause incorrect behavior -// and will be lost if the code is regenerated -// - -package newtable - -// UseSchema sets a new schema name for all generated table SQL builder types. It is recommended to invoke -// this method only once at the beginning of the program. -func UseSchema(schema string) { - Actor = Actor.FromSchema(schema) - Address = Address.FromSchema(schema) - Category = Category.FromSchema(schema) - City = City.FromSchema(schema) - Country = Country.FromSchema(schema) - Customer = Customer.FromSchema(schema) - Film = Film.FromSchema(schema) - FilmActor = FilmActor.FromSchema(schema) - FilmCategory = FilmCategory.FromSchema(schema) - Inventory = Inventory.FromSchema(schema) - Language = Language.FromSchema(schema) - Payment = Payment.FromSchema(schema) - Rental = Rental.FromSchema(schema) - Staff = Staff.FromSchema(schema) - Store = Store.FromSchema(schema) -} -` - var actorModelFile = ` // // Code generated by go-jet DO NOT EDIT. @@ -683,28 +633,6 @@ type Actor struct { } ` -var actorModelFileWithPkgName = ` -// -// Code generated by go-jet DO NOT EDIT. -// -// WARNING: Changes to this file may cause incorrect behavior -// and will be lost if the code is regenerated -// - -package newmodel - -import ( - "time" -) - -type Actor struct { - ActorID int32 ` + "`sql:\"primary_key\"`" + ` - FirstName string - LastName string - LastUpdate time.Time -} -` - var actorInfoSQLBuilderFile = ` // // Code generated by go-jet DO NOT EDIT. @@ -792,93 +720,6 @@ func newActorInfoTableImpl(schemaName, tableName, alias string) actorInfoTable { } ` -var actorInfoSQLBuilderFileWithPkgName = ` -// -// Code generated by go-jet DO NOT EDIT. -// -// WARNING: Changes to this file may cause incorrect behavior -// and will be lost if the code is regenerated -// - -package newview - -import ( - "github.com/go-jet/jet/v2/postgres" -) - -var ActorInfo = newActorInfoTable("dvds", "actor_info", "") - -type actorInfoTable struct { - postgres.Table - - // Columns - ActorID postgres.ColumnInteger - FirstName postgres.ColumnString - LastName postgres.ColumnString - FilmInfo postgres.ColumnString - - AllColumns postgres.ColumnList - MutableColumns postgres.ColumnList -} - -type ActorInfoTable struct { - actorInfoTable - - EXCLUDED actorInfoTable -} - -// AS creates new ActorInfoTable with assigned alias -func (a ActorInfoTable) AS(alias string) *ActorInfoTable { - return newActorInfoTable(a.SchemaName(), a.TableName(), alias) -} - -// Schema creates new ActorInfoTable with assigned schema name -func (a ActorInfoTable) FromSchema(schemaName string) *ActorInfoTable { - return newActorInfoTable(schemaName, a.TableName(), a.Alias()) -} - -// WithPrefix creates new ActorInfoTable with assigned table prefix -func (a ActorInfoTable) WithPrefix(prefix string) *ActorInfoTable { - return newActorInfoTable(a.SchemaName(), prefix+a.TableName(), a.TableName()) -} - -// WithSuffix creates new ActorInfoTable with assigned table suffix -func (a ActorInfoTable) WithSuffix(suffix string) *ActorInfoTable { - return newActorInfoTable(a.SchemaName(), a.TableName()+suffix, a.TableName()) -} - -func newActorInfoTable(schemaName, tableName, alias string) *ActorInfoTable { - return &ActorInfoTable{ - actorInfoTable: newActorInfoTableImpl(schemaName, tableName, alias), - EXCLUDED: newActorInfoTableImpl("", "excluded", ""), - } -} - -func newActorInfoTableImpl(schemaName, tableName, alias string) actorInfoTable { - var ( - ActorIDColumn = postgres.IntegerColumn("actor_id") - FirstNameColumn = postgres.StringColumn("first_name") - LastNameColumn = postgres.StringColumn("last_name") - FilmInfoColumn = postgres.StringColumn("film_info") - allColumns = postgres.ColumnList{ActorIDColumn, FirstNameColumn, LastNameColumn, FilmInfoColumn} - mutableColumns = postgres.ColumnList{ActorIDColumn, FirstNameColumn, LastNameColumn, FilmInfoColumn} - ) - - return actorInfoTable{ - Table: postgres.NewTable(schemaName, tableName, alias, allColumns...), - - //Columns - ActorID: ActorIDColumn, - FirstName: FirstNameColumn, - LastName: LastNameColumn, - FilmInfo: FilmInfoColumn, - - AllColumns: allColumns, - MutableColumns: mutableColumns, - } -} -` - var viewUseSchemaFile = ` // // Code generated by go-jet DO NOT EDIT. @@ -902,29 +743,6 @@ func UseSchema(schema string) { } ` -var viewUseSchemaFileWithPkgName = ` -// -// Code generated by go-jet DO NOT EDIT. -// -// WARNING: Changes to this file may cause incorrect behavior -// and will be lost if the code is regenerated -// - -package newview - -// UseSchema sets a new schema name for all generated view SQL builder types. It is recommended to invoke -// this method only once at the beginning of the program. -func UseSchema(schema string) { - ActorInfo = ActorInfo.FromSchema(schema) - CustomerList = CustomerList.FromSchema(schema) - FilmList = FilmList.FromSchema(schema) - NicerButSlowerFilmList = NicerButSlowerFilmList.FromSchema(schema) - SalesByFilmCategory = SalesByFilmCategory.FromSchema(schema) - SalesByStore = SalesByStore.FromSchema(schema) - StaffList = StaffList.FromSchema(schema) -} -` - func TestGeneratedAllTypesSQLBuilderFiles(t *testing.T) { skipForCockroachDB(t) // because of rowid column From d6f1f28db896e1da9b8a95de44692656645e9fb6 Mon Sep 17 00:00:00 2001 From: Jamius Siam Date: Sun, 24 Nov 2024 21:41:52 +0600 Subject: [PATCH 5/8] Fixed cross-platform path compatibility --- cmd/jet/main.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/cmd/jet/main.go b/cmd/jet/main.go index aecbfaa1..fe3161aa 100644 --- a/cmd/jet/main.go +++ b/cmd/jet/main.go @@ -8,6 +8,7 @@ import ( "github.com/go-jet/jet/v2/internal/utils/errfmt" "github.com/go-jet/jet/v2/internal/utils/strslice" "os" + "path/filepath" "strings" "github.com/go-jet/jet/v2/generator/metadata" @@ -79,12 +80,19 @@ func init() { flag.StringVar(&tablePkg, "table-pkg", "table", "Relative path for the Table files package from the destination directory.") flag.StringVar(&viewPkg, "view-pkg", "view", "Relative path for the View files package from the destination directory.") flag.StringVar(&enumPkg, "enum-pkg", "enum", "Relative path for the Enum files package from the destination directory.") -} -func main() { flag.Usage = usage flag.Parse() + // Convert the OS-specific path separator to slashes for cross-platform compatibility. + destDir = filepath.ToSlash(destDir) + modelPkg = filepath.ToSlash(modelPkg) + tablePkg = filepath.ToSlash(tablePkg) + viewPkg = filepath.ToSlash(viewPkg) + enumPkg = filepath.ToSlash(enumPkg) +} + +func main() { if dsn == "" && (source == "" || host == "" || port == 0 || user == "" || dbName == "") { printErrorAndExit("ERROR: required flag(s) missing") } From cab3cc63bf592b9bdb03766f70ca2e8257e93fd6 Mon Sep 17 00:00:00 2001 From: Jamius Siam Date: Tue, 26 Nov 2024 15:40:34 +0600 Subject: [PATCH 6/8] Replace path package with filepath --- cmd/jet/main.go | 12 ++---------- generator/postgres/postgres_generator.go | 4 ++-- generator/template/model_template.go | 4 ++-- generator/template/process.go | 14 +++++++------- generator/template/sql_builder_template.go | 6 +++--- tests/internal/utils/file/file.go | 6 +++--- tests/mysql/generator_template_test.go | 14 +++++++------- tests/postgres/generator_template_test.go | 16 ++++++++-------- 8 files changed, 34 insertions(+), 42 deletions(-) diff --git a/cmd/jet/main.go b/cmd/jet/main.go index fe3161aa..aecbfaa1 100644 --- a/cmd/jet/main.go +++ b/cmd/jet/main.go @@ -8,7 +8,6 @@ import ( "github.com/go-jet/jet/v2/internal/utils/errfmt" "github.com/go-jet/jet/v2/internal/utils/strslice" "os" - "path/filepath" "strings" "github.com/go-jet/jet/v2/generator/metadata" @@ -80,19 +79,12 @@ func init() { flag.StringVar(&tablePkg, "table-pkg", "table", "Relative path for the Table files package from the destination directory.") flag.StringVar(&viewPkg, "view-pkg", "view", "Relative path for the View files package from the destination directory.") flag.StringVar(&enumPkg, "enum-pkg", "enum", "Relative path for the Enum files package from the destination directory.") +} +func main() { flag.Usage = usage flag.Parse() - // Convert the OS-specific path separator to slashes for cross-platform compatibility. - destDir = filepath.ToSlash(destDir) - modelPkg = filepath.ToSlash(modelPkg) - tablePkg = filepath.ToSlash(tablePkg) - viewPkg = filepath.ToSlash(viewPkg) - enumPkg = filepath.ToSlash(enumPkg) -} - -func main() { if dsn == "" && (source == "" || host == "" || port == 0 || user == "" || dbName == "") { printErrorAndExit("ERROR: required flag(s) missing") } diff --git a/generator/postgres/postgres_generator.go b/generator/postgres/postgres_generator.go index ce6acd16..0b503ef8 100644 --- a/generator/postgres/postgres_generator.go +++ b/generator/postgres/postgres_generator.go @@ -4,7 +4,7 @@ import ( "database/sql" "fmt" "net/url" - "path" + "path/filepath" "strconv" "github.com/go-jet/jet/v2/generator/metadata" @@ -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 { diff --git a/generator/template/model_template.go b/generator/template/model_template.go index f89ebd1b..990b4096 100644 --- a/generator/template/model_template.go +++ b/generator/template/model_template.go @@ -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" @@ -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 diff --git a/generator/template/process.go b/generator/template/process.go index 5abef879..371c89b6 100644 --- a/generator/template/process.go +++ b/generator/template/process.go @@ -5,7 +5,7 @@ import ( "errors" "fmt" "github.com/go-jet/jet/v2/internal/utils/filesys" - "path" + "path/filepath" "strings" "text/template" @@ -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...") @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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) diff --git a/generator/template/sql_builder_template.go b/generator/template/sql_builder_template.go index 16f88b84..3b158b85 100644 --- a/generator/template/sql_builder_template.go +++ b/generator/template/sql_builder_template.go @@ -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" @@ -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 @@ -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 diff --git a/tests/internal/utils/file/file.go b/tests/internal/utils/file/file.go index 73095ea3..98438b13 100644 --- a/tests/internal/utils/file/file.go +++ b/tests/internal/utils/file/file.go @@ -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) @@ -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)) } diff --git a/tests/mysql/generator_template_test.go b/tests/mysql/generator_template_test.go index a257f31c..8c539e26 100644 --- a/tests/mysql/generator_template_test.go +++ b/tests/mysql/generator_template_test.go @@ -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() { diff --git a/tests/postgres/generator_template_test.go b/tests/postgres/generator_template_test.go index 65603cd7..9d323bd9 100644 --- a/tests/postgres/generator_template_test.go +++ b/tests/postgres/generator_template_test.go @@ -3,7 +3,7 @@ package postgres import ( "database/sql" "fmt" - "path" + "path/filepath" "testing" "github.com/go-jet/jet/v2/generator/metadata" @@ -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, From d22f3cff3d101956d838ef7c1cb8b292b04df447 Mon Sep 17 00:00:00 2001 From: Jamius Siam Date: Tue, 26 Nov 2024 15:48:36 +0600 Subject: [PATCH 7/8] Fixed flag names --- cmd/jet/main.go | 8 ++++---- tests/postgres/generator_test.go | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/cmd/jet/main.go b/cmd/jet/main.go index aecbfaa1..67d739ff 100644 --- a/cmd/jet/main.go +++ b/cmd/jet/main.go @@ -75,10 +75,10 @@ func init() { flag.StringVar(&ignoreEnums, "ignore-enums", "", `Comma-separated list of enums to ignore.`) flag.StringVar(&destDir, "path", "", "Destination directory for files generated.") - flag.StringVar(&modelPkg, "model-pkg", "model", "Relative path for the Model files package from the destination directory.") - flag.StringVar(&tablePkg, "table-pkg", "table", "Relative path for the Table files package from the destination directory.") - flag.StringVar(&viewPkg, "view-pkg", "view", "Relative path for the View files package from the destination directory.") - flag.StringVar(&enumPkg, "enum-pkg", "enum", "Relative path for the Enum files package from the destination directory.") + 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() { diff --git a/tests/postgres/generator_test.go b/tests/postgres/generator_test.go index 48efc1dd..93fb9c3a 100644 --- a/tests/postgres/generator_test.go +++ b/tests/postgres/generator_test.go @@ -125,10 +125,10 @@ func TestCmdGeneratorWithPkgNames(t *testing.T) { "-password=jet", "-schema=dvds", "-path="+genTestDir2, - "-model-pkg="+modelPath, - "-table-pkg="+tablePath, - "-view-pkg="+viewPath, - "-enum-pkg="+enumPath) + "-rel-model-path="+modelPath, + "-rel-table-path="+tablePath, + "-rel-view-path="+viewPath, + "-rel-enum-path="+enumPath) cmd.Stderr = os.Stderr cmd.Stdout = os.Stdout @@ -159,10 +159,10 @@ func TestCmdGeneratorWithPkgNames(t *testing.T) { "-password=jet", "-schema=dvds", "-path="+genTestDir2, - "-model-pkg="+modelPath, - "-table-pkg="+tablePath, - "-view-pkg="+viewPath, - "-enum-pkg="+enumPath) + "-rel-model-path="+modelPath, + "-rel-table-path="+tablePath, + "-rel-view-path="+viewPath, + "-rel-enum-path="+enumPath) cmd.Stderr = os.Stderr cmd.Stdout = os.Stdout From 71ae4ed2e054e441a5100036d2fa75d458cbbd23 Mon Sep 17 00:00:00 2001 From: Jamius Siam Date: Tue, 26 Nov 2024 16:36:33 +0600 Subject: [PATCH 8/8] Fixed flag usage info --- cmd/jet/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/jet/main.go b/cmd/jet/main.go index 67d739ff..243a40de 100644 --- a/cmd/jet/main.go +++ b/cmd/jet/main.go @@ -178,7 +178,7 @@ func usage() { "source", "dsn", "host", "port", "user", "password", "dbname", "schema", "params", "sslmode", "path", "ignore-tables", "ignore-views", "ignore-enums", - "model-pkg", "table-pkg", "view-pkg", "enum-pkg", + "rel-model-path", "rel-table-path", "rel-view-path", "rel-enum-path", } for _, name := range order { @@ -195,7 +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 -model-pkg=./entity + $ jet -source=sqlite -dsn="file://path/to/sqlite/database/file" -path=./gen -rel-model-path=./entity `) }