From bfb02acb43e472835b3838ed188a76e35836e9cc Mon Sep 17 00:00:00 2001 From: SanjaiyKumar Date: Wed, 18 Jun 2025 00:13:18 +0530 Subject: [PATCH 1/5] Added new flags '-allow-tables', '-allow-views', and '-allow-enums' for whitelisting the tables, views, and enums while generate command. --- cmd/jet/main.go | 86 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 61 insertions(+), 25 deletions(-) diff --git a/cmd/jet/main.go b/cmd/jet/main.go index 8512e43..384653c 100644 --- a/cmd/jet/main.go +++ b/cmd/jet/main.go @@ -50,8 +50,17 @@ var ( tablePkg string viewPkg string enumPkg string + + allowTables string + allowViews string + allowEnums string ) +type templateFilter struct { + names []string + allow bool +} + func init() { flag.StringVar(&source, "source", "", "Database system name (postgres, mysql, cockroachdb, mariadb or sqlite)") @@ -84,6 +93,10 @@ func init() { 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.") + + flag.StringVar(&allowTables, "allow-tables", "", `Comma-separated list of tables to allow.`) + flag.StringVar(&allowViews, "allow-views", "", `Comma-separated list of views to allow. Will override ignore-views flag.`) + flag.StringVar(&allowEnums, "allow-enums", "", `Comma-separated list of enums to allow.`) } func main() { @@ -95,15 +108,15 @@ func main() { } source := getSource() - ignoreTablesList := parseList(ignoreTables) - ignoreViewsList := parseList(ignoreViews) - ignoreEnumsList := parseList(ignoreEnums) + tablesFilter := createTemplateFilter(ignoreTables, allowTables) + viewsFilter := createTemplateFilter(ignoreViews, allowViews) + enumsFilter := createTemplateFilter(ignoreEnums, allowEnums) var err error switch source { case "postgresql", "postgres", "cockroachdb", "cockroach": - generatorTemplate := genTemplate(postgres2.Dialect, ignoreTablesList, ignoreViewsList, ignoreEnumsList) + generatorTemplate := genTemplate(postgres2.Dialect, tablesFilter, viewsFilter, enumsFilter) if dsn != "" { err = postgresgen.GenerateDSN(dsn, schemaName, destDir, generatorTemplate) @@ -129,7 +142,7 @@ func main() { ) case "mysql", "mysqlx", "mariadb": - generatorTemplate := genTemplate(mysql.Dialect, ignoreTablesList, ignoreViewsList, ignoreEnumsList) + generatorTemplate := genTemplate(mysql.Dialect, tablesFilter, viewsFilter, enumsFilter) if dsn != "" { err = mysqlgen.GenerateDSN(dsn, destDir, generatorTemplate) @@ -158,7 +171,7 @@ func main() { err = sqlitegen.GenerateDSN( dsn, destDir, - genTemplate(sqlite.Dialect, ignoreTablesList, ignoreViewsList, ignoreEnumsList), + genTemplate(sqlite.Dialect, tablesFilter, viewsFilter, enumsFilter), ) case "": @@ -184,7 +197,8 @@ func usage() { "path", "ignore-tables", "ignore-views", "ignore-enums", "skip-model", "skip-sql-builder", - "rel-model-path", "rel-table-path", "rel-view-path", "rel-enum-path", + "rel-model-path", "rel-table-path", "rel-view-path", "rel-enum-path", "allow-tables", "allow-views", + "allow-enums", } for _, name := range order { @@ -245,38 +259,27 @@ func parseList(list string) []string { return ret } -func genTemplate(dialect jet.Dialect, ignoreTables []string, ignoreViews []string, ignoreEnums []string) template.Template { - shouldSkipTable := func(table metadata.Table) bool { - return strslice.Contains(ignoreTables, strings.ToLower(table.Name)) - } - - shouldSkipView := func(view metadata.Table) bool { - return strslice.Contains(ignoreViews, strings.ToLower(view.Name)) - } - - shouldSkipEnum := func(enum metadata.Enum) bool { - return strslice.Contains(ignoreEnums, strings.ToLower(enum.Name)) - } +func genTemplate(dialect jet.Dialect, tablesFilter, viewsFilter, enumsFilter templateFilter) template.Template { return template.Default(dialect). UseSchema(func(schemaMetaData metadata.Schema) template.Schema { return template.DefaultSchema(schemaMetaData). UseModel(template.DefaultModel().ShouldSkip(skipModel).UsePath(modelPkg). UseTable(func(table metadata.Table) template.TableModel { - if shouldSkipTable(table) { + if shouldSkipTable(table, tablesFilter) { return template.TableModel{Skip: true} } return template.DefaultTableModel(table) }). UseView(func(view metadata.Table) template.ViewModel { - if shouldSkipView(view) { + if shouldSkipTable(view, viewsFilter) { return template.ViewModel{Skip: true} } return template.DefaultViewModel(view) }). UseEnum(func(enum metadata.Enum) template.EnumModel { - if shouldSkipEnum(enum) { + if shouldSkipEnum(enum, enumsFilter) { return template.EnumModel{Skip: true} } return template.DefaultEnumModel(enum) @@ -284,21 +287,21 @@ func genTemplate(dialect jet.Dialect, ignoreTables []string, ignoreViews []strin ). UseSQLBuilder(template.DefaultSQLBuilder().ShouldSkip(skipSQLBuilder). UseTable(func(table metadata.Table) template.TableSQLBuilder { - if shouldSkipTable(table) { + if shouldSkipTable(table, tablesFilter) { return template.TableSQLBuilder{Skip: true} } return template.DefaultTableSQLBuilder(table).UsePath(tablePkg) }). UseView(func(table metadata.Table) template.ViewSQLBuilder { - if shouldSkipView(table) { + if shouldSkipTable(table, viewsFilter) { return template.ViewSQLBuilder{Skip: true} } return template.DefaultViewSQLBuilder(table).UsePath(viewPkg) }). UseEnum(func(enum metadata.Enum) template.EnumSQLBuilder { - if shouldSkipEnum(enum) { + if shouldSkipEnum(enum, enumsFilter) { return template.EnumSQLBuilder{Skip: true} } @@ -307,3 +310,36 @@ func genTemplate(dialect jet.Dialect, ignoreTables []string, ignoreViews []strin ) }) } + +func createTemplateFilter(ignoreList, allowList string) templateFilter { + ignoreListParsed := parseList(ignoreList) + allowListParsed := parseList(allowList) + + if len(allowListParsed) > 0 { + return templateFilter{ + names: allowListParsed, + allow: true, + } + } + + return templateFilter{ + names: ignoreListParsed, + allow: false, + } +} + +func shouldSkipTable(table metadata.Table, config templateFilter) bool { + if config.allow { + return !strslice.Contains(config.names, strings.ToLower(table.Name)) + } + + return strslice.Contains(config.names, strings.ToLower(table.Name)) +} + +func shouldSkipEnum(enum metadata.Enum, config templateFilter) bool { + if config.allow { + return !strslice.Contains(config.names, strings.ToLower(enum.Name)) + } + + return strslice.Contains(config.names, strings.ToLower(enum.Name)) +} From c35892320d2be5300f8cf11a44f67fb2c66429d4 Mon Sep 17 00:00:00 2001 From: SanjaiyKumar Date: Wed, 18 Jun 2025 16:02:36 +0530 Subject: [PATCH 2/5] Added unit test cases for the new flags '-allow-tables', '-allow-views', and '-allow-enums'. --- cmd/jet/main.go | 25 +++--- tests/mysql/generator_test.go | 120 +++++++++++++++++++++++++++ tests/postgres/generator_test.go | 138 +++++++++++++++++++++++++++++++ tests/sqlite/generator_test.go | 50 +++++++++++ 4 files changed, 319 insertions(+), 14 deletions(-) diff --git a/cmd/jet/main.go b/cmd/jet/main.go index 384653c..49a347e 100644 --- a/cmd/jet/main.go +++ b/cmd/jet/main.go @@ -312,34 +312,31 @@ func genTemplate(dialect jet.Dialect, tablesFilter, viewsFilter, enumsFilter tem } func createTemplateFilter(ignoreList, allowList string) templateFilter { - ignoreListParsed := parseList(ignoreList) - allowListParsed := parseList(allowList) - - if len(allowListParsed) > 0 { + if allowList != "" { return templateFilter{ - names: allowListParsed, + names: parseList(allowList), allow: true, } } return templateFilter{ - names: ignoreListParsed, + names: parseList(ignoreList), allow: false, } } -func shouldSkipTable(table metadata.Table, config templateFilter) bool { - if config.allow { - return !strslice.Contains(config.names, strings.ToLower(table.Name)) +func shouldSkipTable(table metadata.Table, filter templateFilter) bool { + if filter.allow { + return !strslice.Contains(filter.names, strings.ToLower(table.Name)) } - return strslice.Contains(config.names, strings.ToLower(table.Name)) + return strslice.Contains(filter.names, strings.ToLower(table.Name)) } -func shouldSkipEnum(enum metadata.Enum, config templateFilter) bool { - if config.allow { - return !strslice.Contains(config.names, strings.ToLower(enum.Name)) +func shouldSkipEnum(enum metadata.Enum, filter templateFilter) bool { + if filter.allow { + return !strslice.Contains(filter.names, strings.ToLower(enum.Name)) } - return strslice.Contains(config.names, strings.ToLower(enum.Name)) + return strslice.Contains(filter.names, strings.ToLower(enum.Name)) } diff --git a/tests/mysql/generator_test.go b/tests/mysql/generator_test.go index 763a310..01293fa 100644 --- a/tests/mysql/generator_test.go +++ b/tests/mysql/generator_test.go @@ -1002,3 +1002,123 @@ func newAllTypesTableImpl(schemaName, tableName, alias string) allTypesTable { } } ` + +func TestAllowTablesViewsEnums(t *testing.T) { + tests := []struct { + name string + args []string + }{ + { + name: "with dsn", + args: []string{ + "-dsn=mysql://" + dbconfig.MySQLConnectionString(sourceIsMariaDB(), "dvds"), + "-allow-tables=actor,ADDRESS,Category, city ,country,staff,store,rental", + "-allow-views=actor_info,CUSTomER_LIST, film_list", + "-allow-enums=film_list_rating,film_rating", + "-path=" + genTestDir3, + }, + }, + { + name: "without dsn", + args: []string{ + "-source=MySQL", + "-dbname=dvds", + "-host=" + dbconfig.MySqLHost, + "-port=" + strconv.Itoa(dbconfig.MySQLPort), + "-user=" + dbconfig.MySQLUser, + "-password=" + dbconfig.MySQLPassword, + "-allow-tables=actor,ADDRESS,Category, city ,country,staff,store,rental", + "-allow-views=actor_info,CUSTomER_LIST, film_list", + "-allow-enums=film_list_rating,film_rating", + "-path=" + genTestDir3, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cmd := exec.Command("jet", tt.args...) + + cmd.Stderr = os.Stderr + cmd.Stdout = os.Stdout + + err := cmd.Run() + require.NoError(t, err) + + testutils.AssertFileNamesEqual(t, genTestDir3+"/dvds/table", "category.go", "actor.go", "address.go", + "city.go", "country.go", "staff.go", "store.go", "rental.go", "table_use_schema.go") + + testutils.AssertFileNamesEqual(t, genTestDir3+"/dvds/view", "actor_info.go", "customer_list.go", + "film_list.go", "view_use_schema.go") + + testutils.AssertFileNamesEqual(t, genTestDir3+"/dvds/enum", "film_list_rating.go", "film_rating.go") + + testutils.AssertFileNamesEqual(t, genTestDir3+"/dvds/model", "category.go", "actor.go", "address.go", + "city.go", "country.go", "staff.go", "store.go", "rental.go", "actor_info.go", + "customer_list.go", "film_list.go", "film_list_rating.go", "film_rating.go") + }) + } +} + +func TestAllowAndIgnoreTablesViewsEnums(t *testing.T) { + tests := []struct { + name string + args []string + }{ + { + name: "with dsn", + args: []string{ + "-dsn=mysql://" + dbconfig.MySQLConnectionString(sourceIsMariaDB(), "dvds"), + "-allow-tables=actor,ADDRESS,Category, city ,country,staff,store,rental", + "-allow-views=actor_info,CUSTomER_LIST, film_list", + "-allow-enums=film_list_rating,film_rating", + "-ignore-tables=actor,ADDRESS,store,rental", + "-ignore-views=film_list", + "-ignore-enums=film_rating", + "-path=" + genTestDir3, + }, + }, + { + name: "without dsn", + args: []string{ + "-source=MySQL", + "-dbname=dvds", + "-host=" + dbconfig.MySqLHost, + "-port=" + strconv.Itoa(dbconfig.MySQLPort), + "-user=" + dbconfig.MySQLUser, + "-password=" + dbconfig.MySQLPassword, + "-allow-tables=actor,ADDRESS,Category, city ,country,staff,store,rental", + "-allow-views=actor_info,CUSTomER_LIST, film_list", + "-allow-enums=film_list_rating,film_rating", + "-ignore-tables=actor,ADDRESS,store,rental", + "-ignore-views=film_list", + "-ignore-enums=film_rating", + "-path=" + genTestDir3, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cmd := exec.Command("jet", tt.args...) + + cmd.Stderr = os.Stderr + cmd.Stdout = os.Stdout + + err := cmd.Run() + require.NoError(t, err) + + testutils.AssertFileNamesEqual(t, genTestDir3+"/dvds/table", "category.go", "actor.go", "address.go", + "city.go", "country.go", "staff.go", "store.go", "rental.go", "table_use_schema.go") + + testutils.AssertFileNamesEqual(t, genTestDir3+"/dvds/view", "actor_info.go", "customer_list.go", + "film_list.go", "view_use_schema.go") + + testutils.AssertFileNamesEqual(t, genTestDir3+"/dvds/enum", "film_list_rating.go", "film_rating.go") + + testutils.AssertFileNamesEqual(t, genTestDir3+"/dvds/model", "category.go", "actor.go", "address.go", + "city.go", "country.go", "staff.go", "store.go", "rental.go", "actor_info.go", + "customer_list.go", "film_list.go", "film_list_rating.go", "film_rating.go") + }) + } +} diff --git a/tests/postgres/generator_test.go b/tests/postgres/generator_test.go index 06478a6..ada4580 100644 --- a/tests/postgres/generator_test.go +++ b/tests/postgres/generator_test.go @@ -1384,3 +1384,141 @@ func newLinkTableImpl(schemaName, tableName, alias string) linkTable { } } ` + +func TestAllowTablesViewsEnums(t *testing.T) { + tests := []struct { + name string + args []string + }{ + { + name: "with dsn", + args: []string{ + "-dsn=" + defaultDSN(), + "-schema=dvds", + "-allow-tables=actor,ADDRESS,country, Film , cITY,", + "-allow-views=Actor_info, FILM_LIST ,staff_list", + "-allow-enums=mpaa_rating", + "-path=" + genTestDir2, + }, + }, + { + name: "without dsn", + args: []string{ + "-source=PostgreSQL", + "-host=localhost", + "-port=" + strconv.Itoa(dbconfig.PgPort), + "-user=jet", + "-password=jet", + "-dbname=jetdb", + "-schema=dvds", + "-allow-tables=actor,ADDRESS,country, Film , cITY,", + "-allow-views=Actor_info, FILM_LIST ,staff_list", + "-allow-enums=mpaa_rating", + "-path=" + genTestDir2, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := os.RemoveAll(genTestDir2) + require.NoError(t, err) + + cmd := exec.Command("jet", tt.args...) + + fmt.Println(cmd.Args) + cmd.Stderr = os.Stderr + cmd.Stdout = os.Stdout + + err = cmd.Run() + require.NoError(t, err) + + // Table SQL Builder files + testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/table", "actor.go", "address.go", + "country.go", "film.go", "city.go", "table_use_schema.go") + + // View SQL Builder files + testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/view", "actor_info.go", "film_list.go", + "staff_list.go", "view_use_schema.go") + + // Enums SQL Builder files + file.Exists(t, "./.gentestdata2/jetdb/dvds/enum", "mpaa_rating.go") + + // Model files + testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/model", "actor.go", "address.go", + "country.go", "film.go", "city.go", "actor_info.go", "film_list.go", "staff_list.go", "mpaa_rating.go") + }) + } +} + +func TestAllowAndIgnoreTablesViewsEnums(t *testing.T) { + tests := []struct { + name string + args []string + }{ + { + name: "with dsn", + args: []string{ + "-dsn=" + defaultDSN(), + "-schema=dvds", + "-allow-tables=actor,ADDRESS,country, Film , cITY,", + "-allow-views=Actor_info, FILM_LIST ,staff_list", + "-allow-enums=mpaa_rating", + "-ignore-tables=ADDRESS,country, Film", + "-ignore-views=FILM_LIST", + "-ignore-enums=mpaa_rating", + "-path=" + genTestDir2, + }, + }, + { + name: "without dsn", + args: []string{ + "-source=PostgreSQL", + "-host=localhost", + "-port=" + strconv.Itoa(dbconfig.PgPort), + "-user=jet", + "-password=jet", + "-dbname=jetdb", + "-schema=dvds", + "-allow-tables=actor,ADDRESS,country, Film , cITY,", + "-allow-views=Actor_info, FILM_LIST ,staff_list", + "-allow-enums=mpaa_rating", + "-ignore-tables=ADDRESS,country, Film", + "-ignore-views=FILM_LIST", + "-ignore-enums=mpaa_rating", + "-path=" + genTestDir2, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := os.RemoveAll(genTestDir2) + require.NoError(t, err) + + cmd := exec.Command("jet", tt.args...) + + fmt.Println(cmd.Args) + cmd.Stderr = os.Stderr + cmd.Stdout = os.Stdout + + err = cmd.Run() + require.NoError(t, err) + + // Table SQL Builder files + testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/table", "actor.go", "address.go", + "country.go", "film.go", "city.go", "table_use_schema.go") + + // View SQL Builder files + testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/view", "actor_info.go", "film_list.go", + "staff_list.go", "view_use_schema.go") + + // Enums SQL Builder files + file.Exists(t, "./.gentestdata2/jetdb/dvds/enum", "mpaa_rating.go") + + // Model files + testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/model", "actor.go", "address.go", + "country.go", "film.go", "city.go", "actor_info.go", "film_list.go", "staff_list.go", "mpaa_rating.go") + }) + } +} diff --git a/tests/sqlite/generator_test.go b/tests/sqlite/generator_test.go index f261d22..4140d5c 100644 --- a/tests/sqlite/generator_test.go +++ b/tests/sqlite/generator_test.go @@ -427,3 +427,53 @@ type Address struct { LastUpdate time.Time } ` + +func TestAllowTablesEnums(t *testing.T) { + cmd := exec.Command("jet", + "-source=SQLite", + "-dsn=file://"+testDatabaseFilePath, + "-allow-tables=actor,Address,CATEGORY , city ,film,rental,store", + "-allow-views=customer_list, film_list,STAFF_LIst", + "-path="+genDestDir) + + cmd.Stderr = os.Stderr + cmd.Stdout = os.Stdout + + err := cmd.Run() + require.NoError(t, err) + + testutils.AssertFileNamesEqual(t, genDestDir+"/table", "actor.go", "address.go", "category.go", "city.go", + "film.go", "rental.go", "store.go", "table_use_schema.go") + + testutils.AssertFileNamesEqual(t, genDestDir+"/view", "customer_list.go", "film_list.go", "staff_list.go", + "view_use_schema.go") + + testutils.AssertFileNamesEqual(t, genDestDir+"/model", "actor.go", "address.go", "category.go", "city.go", + "film.go", "rental.go", "store.go", "customer_list.go", "film_list.go", "staff_list.go") +} + +func TestAllowAndIgnoreTablesEnums(t *testing.T) { + cmd := exec.Command("jet", + "-source=SQLite", + "-dsn=file://"+testDatabaseFilePath, + "-allow-tables=actor,Address,CATEGORY , city ,film,rental,store", + "-allow-views=customer_list, film_list,STAFF_LIst", + "-ignore-tables=actor,CATEGORY ,store", + "-ignore-views=customer_list", + "-path="+genDestDir) + + cmd.Stderr = os.Stderr + cmd.Stdout = os.Stdout + + err := cmd.Run() + require.NoError(t, err) + + testutils.AssertFileNamesEqual(t, genDestDir+"/table", "actor.go", "address.go", "category.go", "city.go", + "film.go", "rental.go", "store.go", "table_use_schema.go") + + testutils.AssertFileNamesEqual(t, genDestDir+"/view", "customer_list.go", "film_list.go", "staff_list.go", + "view_use_schema.go") + + testutils.AssertFileNamesEqual(t, genDestDir+"/model", "actor.go", "address.go", "category.go", "city.go", + "film.go", "rental.go", "store.go", "customer_list.go", "film_list.go", "staff_list.go") +} From dbfcec7fbe6625d3b35caa088482d124cc91f8a0 Mon Sep 17 00:00:00 2001 From: SanjaiyKumar Date: Wed, 18 Jun 2025 16:08:51 +0530 Subject: [PATCH 3/5] Renamed new flags '-allow-tables', '-allow-views', and '-allow-enums' description. --- cmd/jet/main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/jet/main.go b/cmd/jet/main.go index 49a347e..a30236a 100644 --- a/cmd/jet/main.go +++ b/cmd/jet/main.go @@ -94,9 +94,9 @@ func init() { 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.") - flag.StringVar(&allowTables, "allow-tables", "", `Comma-separated list of tables to allow.`) - flag.StringVar(&allowViews, "allow-views", "", `Comma-separated list of views to allow. Will override ignore-views flag.`) - flag.StringVar(&allowEnums, "allow-enums", "", `Comma-separated list of enums to allow.`) + flag.StringVar(&allowTables, "allow-tables", "", `Comma-separated list of tables to allow. Takes precedence over --ignore-tables flag.`) + flag.StringVar(&allowViews, "allow-views", "", `Comma-separated list of views to allow. Takes precedence over --ignore-views flag.`) + flag.StringVar(&allowEnums, "allow-enums", "", `Comma-separated list of enums to allow. Takes precedence over --ignore-enums flag.`) } func main() { From 85fdfb01b30357e43aff030ccec52f65264493da Mon Sep 17 00:00:00 2001 From: SanjaiyKumar Date: Tue, 24 Jun 2025 14:09:30 +0530 Subject: [PATCH 4/5] Renamed new flags name '-tables', '-views', and '-enums'. --- cmd/jet/main.go | 24 +++++++++------- tests/mysql/generator_test.go | 44 +++++++++++++--------------- tests/postgres/generator_test.go | 49 +++++++++++--------------------- tests/sqlite/generator_test.go | 27 +++++++----------- 4 files changed, 60 insertions(+), 84 deletions(-) diff --git a/cmd/jet/main.go b/cmd/jet/main.go index a30236a..e7d2c66 100644 --- a/cmd/jet/main.go +++ b/cmd/jet/main.go @@ -58,7 +58,7 @@ var ( type templateFilter struct { names []string - allow bool + allow bool } func init() { @@ -94,9 +94,9 @@ func init() { 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.") - flag.StringVar(&allowTables, "allow-tables", "", `Comma-separated list of tables to allow. Takes precedence over --ignore-tables flag.`) - flag.StringVar(&allowViews, "allow-views", "", `Comma-separated list of views to allow. Takes precedence over --ignore-views flag.`) - flag.StringVar(&allowEnums, "allow-enums", "", `Comma-separated list of enums to allow. Takes precedence over --ignore-enums flag.`) + flag.StringVar(&allowTables, "tables", "", `Comma-separated list of tables to allow.`) + flag.StringVar(&allowViews, "views", "", `Comma-separated list of views to allow.`) + flag.StringVar(&allowEnums, "enums", "", `Comma-separated list of enums to allow.`) } func main() { @@ -108,9 +108,9 @@ func main() { } source := getSource() - tablesFilter := createTemplateFilter(ignoreTables, allowTables) - viewsFilter := createTemplateFilter(ignoreViews, allowViews) - enumsFilter := createTemplateFilter(ignoreEnums, allowEnums) + tablesFilter := createTemplateFilter(ignoreTables, allowTables, "tables") + viewsFilter := createTemplateFilter(ignoreViews, allowViews, "views") + enumsFilter := createTemplateFilter(ignoreEnums, allowEnums, "enums") var err error @@ -197,8 +197,8 @@ func usage() { "path", "ignore-tables", "ignore-views", "ignore-enums", "skip-model", "skip-sql-builder", - "rel-model-path", "rel-table-path", "rel-view-path", "rel-enum-path", "allow-tables", "allow-views", - "allow-enums", + "rel-model-path", "rel-table-path", "rel-view-path", "rel-enum-path", "tables", "views", + "enums", } for _, name := range order { @@ -311,7 +311,11 @@ func genTemplate(dialect jet.Dialect, tablesFilter, viewsFilter, enumsFilter tem }) } -func createTemplateFilter(ignoreList, allowList string) templateFilter { +func createTemplateFilter(ignoreList, allowList, filterType string) templateFilter { + if ignoreList != "" && allowList != "" { + printErrorAndExit(fmt.Sprintf("ERROR: cannot use both -%s and -ignore-%s flags simultaneously. Please specify only one option.", filterType, filterType)) + } + if allowList != "" { return templateFilter{ names: parseList(allowList), diff --git a/tests/mysql/generator_test.go b/tests/mysql/generator_test.go index 01293fa..940b67b 100644 --- a/tests/mysql/generator_test.go +++ b/tests/mysql/generator_test.go @@ -1,6 +1,7 @@ package mysql import ( + "bytes" "os" "os/exec" "path/filepath" @@ -1012,9 +1013,9 @@ func TestAllowTablesViewsEnums(t *testing.T) { name: "with dsn", args: []string{ "-dsn=mysql://" + dbconfig.MySQLConnectionString(sourceIsMariaDB(), "dvds"), - "-allow-tables=actor,ADDRESS,Category, city ,country,staff,store,rental", - "-allow-views=actor_info,CUSTomER_LIST, film_list", - "-allow-enums=film_list_rating,film_rating", + "-tables=actor,ADDRESS,Category, city ,country,staff,store,rental", + "-views=actor_info,CUSTomER_LIST, film_list", + "-enums=film_list_rating,film_rating", "-path=" + genTestDir3, }, }, @@ -1027,9 +1028,9 @@ func TestAllowTablesViewsEnums(t *testing.T) { "-port=" + strconv.Itoa(dbconfig.MySQLPort), "-user=" + dbconfig.MySQLUser, "-password=" + dbconfig.MySQLPassword, - "-allow-tables=actor,ADDRESS,Category, city ,country,staff,store,rental", - "-allow-views=actor_info,CUSTomER_LIST, film_list", - "-allow-enums=film_list_rating,film_rating", + "-tables=actor,ADDRESS,Category, city ,country,staff,store,rental", + "-views=actor_info,CUSTomER_LIST, film_list", + "-enums=film_list_rating,film_rating", "-path=" + genTestDir3, }, }, @@ -1069,9 +1070,9 @@ func TestAllowAndIgnoreTablesViewsEnums(t *testing.T) { name: "with dsn", args: []string{ "-dsn=mysql://" + dbconfig.MySQLConnectionString(sourceIsMariaDB(), "dvds"), - "-allow-tables=actor,ADDRESS,Category, city ,country,staff,store,rental", - "-allow-views=actor_info,CUSTomER_LIST, film_list", - "-allow-enums=film_list_rating,film_rating", + "-tables=actor,ADDRESS,Category, city ,country,staff,store,rental", + "-views=actor_info,CUSTomER_LIST, film_list", + "-enums=film_list_rating,film_rating", "-ignore-tables=actor,ADDRESS,store,rental", "-ignore-views=film_list", "-ignore-enums=film_rating", @@ -1087,9 +1088,9 @@ func TestAllowAndIgnoreTablesViewsEnums(t *testing.T) { "-port=" + strconv.Itoa(dbconfig.MySQLPort), "-user=" + dbconfig.MySQLUser, "-password=" + dbconfig.MySQLPassword, - "-allow-tables=actor,ADDRESS,Category, city ,country,staff,store,rental", - "-allow-views=actor_info,CUSTomER_LIST, film_list", - "-allow-enums=film_list_rating,film_rating", + "-tables=actor,ADDRESS,Category, city ,country,staff,store,rental", + "-views=actor_info,CUSTomER_LIST, film_list", + "-enums=film_list_rating,film_rating", "-ignore-tables=actor,ADDRESS,store,rental", "-ignore-views=film_list", "-ignore-enums=film_rating", @@ -1102,23 +1103,16 @@ func TestAllowAndIgnoreTablesViewsEnums(t *testing.T) { t.Run(tt.name, func(t *testing.T) { cmd := exec.Command("jet", tt.args...) + var stdOut bytes.Buffer cmd.Stderr = os.Stderr - cmd.Stdout = os.Stdout + cmd.Stdout = &stdOut err := cmd.Run() - require.NoError(t, err) + require.Error(t, err) + require.Equal(t, "exit status 1", err.Error()) - testutils.AssertFileNamesEqual(t, genTestDir3+"/dvds/table", "category.go", "actor.go", "address.go", - "city.go", "country.go", "staff.go", "store.go", "rental.go", "table_use_schema.go") - - testutils.AssertFileNamesEqual(t, genTestDir3+"/dvds/view", "actor_info.go", "customer_list.go", - "film_list.go", "view_use_schema.go") - - testutils.AssertFileNamesEqual(t, genTestDir3+"/dvds/enum", "film_list_rating.go", "film_rating.go") - - testutils.AssertFileNamesEqual(t, genTestDir3+"/dvds/model", "category.go", "actor.go", "address.go", - "city.go", "country.go", "staff.go", "store.go", "rental.go", "actor_info.go", - "customer_list.go", "film_list.go", "film_list_rating.go", "film_rating.go") + stdOutput := stdOut.String() + require.Contains(t, stdOutput, "ERROR: cannot use both -tables and -ignore-tables flags simultaneously. Please specify only one option.") }) } } diff --git a/tests/postgres/generator_test.go b/tests/postgres/generator_test.go index ada4580..89aa9da 100644 --- a/tests/postgres/generator_test.go +++ b/tests/postgres/generator_test.go @@ -1,6 +1,7 @@ package postgres import ( + "bytes" "fmt" "os" "os/exec" @@ -1395,9 +1396,9 @@ func TestAllowTablesViewsEnums(t *testing.T) { args: []string{ "-dsn=" + defaultDSN(), "-schema=dvds", - "-allow-tables=actor,ADDRESS,country, Film , cITY,", - "-allow-views=Actor_info, FILM_LIST ,staff_list", - "-allow-enums=mpaa_rating", + "-tables=actor,ADDRESS,country, Film , cITY,", + "-views=Actor_info, FILM_LIST ,staff_list", + "-enums=mpaa_rating", "-path=" + genTestDir2, }, }, @@ -1411,9 +1412,9 @@ func TestAllowTablesViewsEnums(t *testing.T) { "-password=jet", "-dbname=jetdb", "-schema=dvds", - "-allow-tables=actor,ADDRESS,country, Film , cITY,", - "-allow-views=Actor_info, FILM_LIST ,staff_list", - "-allow-enums=mpaa_rating", + "-tables=actor,ADDRESS,country, Film , cITY,", + "-views=Actor_info, FILM_LIST ,staff_list", + "-enums=mpaa_rating", "-path=" + genTestDir2, }, }, @@ -1451,7 +1452,7 @@ func TestAllowTablesViewsEnums(t *testing.T) { } } -func TestAllowAndIgnoreTablesViewsEnums(t *testing.T) { +func TestAllowAndIgnoreEnums(t *testing.T) { tests := []struct { name string args []string @@ -1461,11 +1462,7 @@ func TestAllowAndIgnoreTablesViewsEnums(t *testing.T) { args: []string{ "-dsn=" + defaultDSN(), "-schema=dvds", - "-allow-tables=actor,ADDRESS,country, Film , cITY,", - "-allow-views=Actor_info, FILM_LIST ,staff_list", - "-allow-enums=mpaa_rating", - "-ignore-tables=ADDRESS,country, Film", - "-ignore-views=FILM_LIST", + "-enums=mpaa_rating", "-ignore-enums=mpaa_rating", "-path=" + genTestDir2, }, @@ -1480,11 +1477,7 @@ func TestAllowAndIgnoreTablesViewsEnums(t *testing.T) { "-password=jet", "-dbname=jetdb", "-schema=dvds", - "-allow-tables=actor,ADDRESS,country, Film , cITY,", - "-allow-views=Actor_info, FILM_LIST ,staff_list", - "-allow-enums=mpaa_rating", - "-ignore-tables=ADDRESS,country, Film", - "-ignore-views=FILM_LIST", + "-enums=mpaa_rating", "-ignore-enums=mpaa_rating", "-path=" + genTestDir2, }, @@ -1499,26 +1492,16 @@ func TestAllowAndIgnoreTablesViewsEnums(t *testing.T) { cmd := exec.Command("jet", tt.args...) fmt.Println(cmd.Args) + var stdOut bytes.Buffer cmd.Stderr = os.Stderr - cmd.Stdout = os.Stdout + cmd.Stdout = &stdOut err = cmd.Run() - require.NoError(t, err) + require.Error(t, err) + require.Equal(t, "exit status 1", err.Error()) - // Table SQL Builder files - testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/table", "actor.go", "address.go", - "country.go", "film.go", "city.go", "table_use_schema.go") - - // View SQL Builder files - testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/view", "actor_info.go", "film_list.go", - "staff_list.go", "view_use_schema.go") - - // Enums SQL Builder files - file.Exists(t, "./.gentestdata2/jetdb/dvds/enum", "mpaa_rating.go") - - // Model files - testutils.AssertFileNamesEqual(t, "./.gentestdata2/jetdb/dvds/model", "actor.go", "address.go", - "country.go", "film.go", "city.go", "actor_info.go", "film_list.go", "staff_list.go", "mpaa_rating.go") + stdOutput := stdOut.String() + require.Contains(t, stdOutput, "ERROR: cannot use both -enums and -ignore-enums flags simultaneously. Please specify only one option.") }) } } diff --git a/tests/sqlite/generator_test.go b/tests/sqlite/generator_test.go index 4140d5c..783552b 100644 --- a/tests/sqlite/generator_test.go +++ b/tests/sqlite/generator_test.go @@ -1,6 +1,7 @@ package sqlite import ( + "bytes" "os" "os/exec" "reflect" @@ -432,8 +433,8 @@ func TestAllowTablesEnums(t *testing.T) { cmd := exec.Command("jet", "-source=SQLite", "-dsn=file://"+testDatabaseFilePath, - "-allow-tables=actor,Address,CATEGORY , city ,film,rental,store", - "-allow-views=customer_list, film_list,STAFF_LIst", + "-tables=actor,Address,CATEGORY , city ,film,rental,store", + "-views=customer_list, film_list,STAFF_LIst", "-path="+genDestDir) cmd.Stderr = os.Stderr @@ -452,28 +453,22 @@ func TestAllowTablesEnums(t *testing.T) { "film.go", "rental.go", "store.go", "customer_list.go", "film_list.go", "staff_list.go") } -func TestAllowAndIgnoreTablesEnums(t *testing.T) { +func TestAllowAndIgnoreViews(t *testing.T) { cmd := exec.Command("jet", "-source=SQLite", "-dsn=file://"+testDatabaseFilePath, - "-allow-tables=actor,Address,CATEGORY , city ,film,rental,store", - "-allow-views=customer_list, film_list,STAFF_LIst", - "-ignore-tables=actor,CATEGORY ,store", + "-views=customer_list, film_list,STAFF_LIst", "-ignore-views=customer_list", "-path="+genDestDir) + var stdOut bytes.Buffer cmd.Stderr = os.Stderr - cmd.Stdout = os.Stdout + cmd.Stdout = &stdOut err := cmd.Run() - require.NoError(t, err) + require.Error(t, err) + require.Equal(t, "exit status 1", err.Error()) - testutils.AssertFileNamesEqual(t, genDestDir+"/table", "actor.go", "address.go", "category.go", "city.go", - "film.go", "rental.go", "store.go", "table_use_schema.go") - - testutils.AssertFileNamesEqual(t, genDestDir+"/view", "customer_list.go", "film_list.go", "staff_list.go", - "view_use_schema.go") - - testutils.AssertFileNamesEqual(t, genDestDir+"/model", "actor.go", "address.go", "category.go", "city.go", - "film.go", "rental.go", "store.go", "customer_list.go", "film_list.go", "staff_list.go") + stdOutput := stdOut.String() + require.Contains(t, stdOutput, "ERROR: cannot use both -views and -ignore-views flags simultaneously. Please specify only one option.") } From b0636c27cada9eecac9732b887aef990c9cab372 Mon Sep 17 00:00:00 2001 From: SanjaiyKumar Date: Thu, 26 Jun 2025 18:05:46 +0530 Subject: [PATCH 5/5] Renamed templateFilter allow property to ignore. Dropped the allow word from the vairbales. --- cmd/jet/main.go | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/cmd/jet/main.go b/cmd/jet/main.go index e7d2c66..5230bd6 100644 --- a/cmd/jet/main.go +++ b/cmd/jet/main.go @@ -51,14 +51,14 @@ var ( viewPkg string enumPkg string - allowTables string - allowViews string - allowEnums string + tables string + views string + enums string ) type templateFilter struct { names []string - allow bool + ignore bool } func init() { @@ -94,9 +94,9 @@ func init() { 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.") - flag.StringVar(&allowTables, "tables", "", `Comma-separated list of tables to allow.`) - flag.StringVar(&allowViews, "views", "", `Comma-separated list of views to allow.`) - flag.StringVar(&allowEnums, "enums", "", `Comma-separated list of enums to allow.`) + flag.StringVar(&tables, "tables", "", `Comma-separated list of tables to generate.`) + flag.StringVar(&views, "views", "", `Comma-separated list of views to generate.`) + flag.StringVar(&enums, "enums", "", `Comma-separated list of enums to generate.`) } func main() { @@ -108,9 +108,9 @@ func main() { } source := getSource() - tablesFilter := createTemplateFilter(ignoreTables, allowTables, "tables") - viewsFilter := createTemplateFilter(ignoreViews, allowViews, "views") - enumsFilter := createTemplateFilter(ignoreEnums, allowEnums, "enums") + tablesFilter := createTemplateFilter(ignoreTables, tables, "tables") + viewsFilter := createTemplateFilter(ignoreViews, views, "views") + enumsFilter := createTemplateFilter(ignoreEnums, enums, "enums") var err error @@ -319,28 +319,28 @@ func createTemplateFilter(ignoreList, allowList, filterType string) templateFilt if allowList != "" { return templateFilter{ names: parseList(allowList), - allow: true, + ignore: false, } } return templateFilter{ names: parseList(ignoreList), - allow: false, + ignore: true, } } func shouldSkipTable(table metadata.Table, filter templateFilter) bool { - if filter.allow { - return !strslice.Contains(filter.names, strings.ToLower(table.Name)) + if filter.ignore { + return strslice.Contains(filter.names, strings.ToLower(table.Name)) } - return strslice.Contains(filter.names, strings.ToLower(table.Name)) + return !strslice.Contains(filter.names, strings.ToLower(table.Name)) } func shouldSkipEnum(enum metadata.Enum, filter templateFilter) bool { - if filter.allow { - return !strslice.Contains(filter.names, strings.ToLower(enum.Name)) + if filter.ignore { + return strslice.Contains(filter.names, strings.ToLower(enum.Name)) } - return strslice.Contains(filter.names, strings.ToLower(enum.Name)) + return !strslice.Contains(filter.names, strings.ToLower(enum.Name)) }