From 4eb85c20861ee8a081c589757d8a9d80e5ca3990 Mon Sep 17 00:00:00 2001 From: Alexander Trost Date: Thu, 26 Dec 2024 15:03:42 +0100 Subject: [PATCH] feat: add generator skip option for table columns Signed-off-by: Alexander Trost --- generator/template/file_templates.go | 10 ++++ generator/template/model_template.go | 2 + generator/template/sql_builder_template.go | 1 + tests/postgres/generator_template_test.go | 60 +++++++++++++++++++--- 4 files changed, 66 insertions(+), 7 deletions(-) diff --git a/generator/template/file_templates.go b/generator/template/file_templates.go index 1538031d..bc5c1080 100644 --- a/generator/template/file_templates.go +++ b/generator/template/file_templates.go @@ -14,9 +14,11 @@ var tableSQLBuilderTemplate = ` {{define "column-list" -}} {{- range $i, $c := . }} {{- $field := columnField $c}} + {{- if not $field.Skip}} {{- if gt $i 0 }}, {{end}}{{$field.Name}}Column {{- end}} {{- end}} +{{- end}} package {{package}} @@ -33,7 +35,9 @@ type {{structImplName}} struct { // Columns {{- range $i, $c := .Columns}} {{- $field := columnField $c}} +{{- if not $field.Skip}} {{$field.Name}} {{dialect.PackageName}}.Column{{$field.Type}} {{golangComment .Comment}} +{{- end}} {{- end}} AllColumns {{dialect.PackageName}}.ColumnList @@ -77,7 +81,9 @@ func new{{tableTemplate.TypeName}}Impl(schemaName, tableName, alias string) {{st var ( {{- range $i, $c := .Columns}} {{- $field := columnField $c}} +{{- if not $field.Skip }} {{$field.Name}}Column = {{dialect.PackageName}}.{{$field.Type}}Column("{{$c.Name}}") +{{- end}} {{- end}} allColumns = {{dialect.PackageName}}.ColumnList{ {{template "column-list" .Columns}} } mutableColumns = {{dialect.PackageName}}.ColumnList{ {{template "column-list" .MutableColumns}} } @@ -89,7 +95,9 @@ func new{{tableTemplate.TypeName}}Impl(schemaName, tableName, alias string) {{st //Columns {{- range $i, $c := .Columns}} {{- $field := columnField $c}} +{{- if not $field.Skip }} {{$field.Name}}: {{$field.Name}}Column, +{{- end}} {{- end}} AllColumns: allColumns, @@ -124,7 +132,9 @@ import ( type {{$modelTableTemplate.TypeName}} struct { {{- range .Columns}} {{- $field := structField .}} +{{- if not $field.Skip}} {{$field.Name}} {{$field.Type.Name}} ` + "{{$field.TagsString}}" + ` {{golangComment .Comment}} +{{- end }} {{- end}} } diff --git a/generator/template/model_template.go b/generator/template/model_template.go index 990b4096..84f46d70 100644 --- a/generator/template/model_template.go +++ b/generator/template/model_template.go @@ -159,6 +159,7 @@ type TableModelField struct { Name string Type Type Tags []string + Skip bool } // DefaultTableModelField returns default TableModelField implementation @@ -173,6 +174,7 @@ func DefaultTableModelField(columnMetaData metadata.Column) TableModelField { Name: dbidentifier.ToGoIdentifier(columnMetaData.Name), Type: getType(columnMetaData), Tags: tags, + Skip: false, } } diff --git a/generator/template/sql_builder_template.go b/generator/template/sql_builder_template.go index 3b158b85..a72e8e99 100644 --- a/generator/template/sql_builder_template.go +++ b/generator/template/sql_builder_template.go @@ -131,6 +131,7 @@ func (tb TableSQLBuilder) UseColumn(columnsFunc func(column metadata.Column) Tab // TableSQLBuilderColumn is template for table sql builder column type TableSQLBuilderColumn struct { + Skip bool Name string Type string } diff --git a/tests/postgres/generator_template_test.go b/tests/postgres/generator_template_test.go index 9d323bd9..6f18d33a 100644 --- a/tests/postgres/generator_template_test.go +++ b/tests/postgres/generator_template_test.go @@ -20,13 +20,15 @@ import ( const tempTestDir = "./.tempTestDir" -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 ( + defaultModelPath = filepath.Join(tempTestDir, "jetdb/dvds/model") + defaultSqlBuilderPath = filepath.Join(tempTestDir, "jetdb/dvds/table") + defaultActorModelFilePath = filepath.Join(tempTestDir, "jetdb/dvds/model", "actor.go") + defaultTableSQLBuilderFilePath = filepath.Join(tempTestDir, "jetdb/dvds/table") + defaultViewSQLBuilderFilePath = filepath.Join(tempTestDir, "jetdb/dvds/view") + defaultEnumSQLBuilderFilePath = filepath.Join(tempTestDir, "jetdb/dvds/enum") + defaultActorSQLBuilderFilePath = filepath.Join(tempTestDir, "jetdb/dvds/table", "actor.go") +) var dbConnection = postgres.DBConnection{ Host: dbconfig.PgHost, @@ -479,6 +481,50 @@ func TestGeneratorTemplate_SQLBuilder_ChangeColumnTypes(t *testing.T) { require.Contains(t, actor, "ActorID postgres.ColumnString") } +func TestGeneratorTemplate_Model_SQLBuilder_SkipColumn(t *testing.T) { + err := postgres.Generate( + tempTestDir, + dbConnection, + template.Default(postgres2.Dialect). + UseSchema(func(schemaMetaData metadata.Schema) template.Schema { + return template.DefaultSchema(schemaMetaData). + UseSQLBuilder(template.DefaultSQLBuilder(). + UseTable(func(table metadata.Table) template.TableSQLBuilder { + return template.DefaultTableSQLBuilder(table). + UseColumn(func(column metadata.Column) template.TableSQLBuilderColumn { + defaultColumn := template.DefaultTableSQLBuilderColumn(column) + + if defaultColumn.Name == "ActorID" { + defaultColumn.Skip = true + } + + return defaultColumn + }) + }), + ).UseModel(template.DefaultModel(). + UseTable(func(table metadata.Table) template.TableModel { + return template.DefaultTableModel(table). + UseField(func(column metadata.Column) template.TableModelField { + defaultColumn := template.DefaultTableModelField(column) + + if defaultColumn.Name == "ActorID" { + defaultColumn.Skip = true + } + + return defaultColumn + }) + }), + ) + }), + ) + + require.Nil(t, err) + + actor := file2.Exists(t, defaultActorSQLBuilderFilePath) + require.NotContains(t, actor, "ActorID") + require.Contains(t, actor, "FirstName ") +} + func TestRenameEnumValueName(t *testing.T) { err := postgres.Generate( tempTestDir,