Skip to content

Commit

Permalink
feat: add generator skip option for table columns
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Trost <[email protected]>
  • Loading branch information
galexrt committed Jan 7, 2025
1 parent f9ed359 commit c36440d
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 11 deletions.
13 changes: 9 additions & 4 deletions generator/template/file_templates.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions generator/template/model_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ type TableModelField struct {
Name string
Type Type
Tags []string
Skip bool
}

// DefaultTableModelField returns default TableModelField implementation
Expand All @@ -173,6 +174,7 @@ func DefaultTableModelField(columnMetaData metadata.Column) TableModelField {
Name: dbidentifier.ToGoIdentifier(columnMetaData.Name),
Type: getType(columnMetaData),
Tags: tags,
Skip: false,
}
}

Expand Down
11 changes: 11 additions & 0 deletions generator/template/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,17 @@ func processTableSQLBuilder(fileTypes, dirPath string,
return insertedRowAlias(dialect)
},
"golangComment": formatGolangComment,
"columnList": func(columns []metadata.Column) string {
names := []string{}
for _, col := range columns {
bc := tableSQLBuilder.Column(col)
if bc.Skip {
continue
}
names = append(names, bc.Name+"Column")
}
return strings.Join(names, ", ")
},
})
if err != nil {
return fmt.Errorf("failed to generate table sql builder type %s: %w", tableSQLBuilder.TypeName, err)
Expand Down
1 change: 1 addition & 0 deletions generator/template/sql_builder_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
65 changes: 58 additions & 7 deletions tests/postgres/generator_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -479,6 +481,55 @@ 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 == "FirstName" {
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 == "FirstName" {
defaultColumn.Skip = true
}

return defaultColumn
})
}),
)
}),
)

require.Nil(t, err)

actorSql := file2.Exists(t, defaultActorSQLBuilderFilePath)
require.NotContains(t, actorSql, "FirstName")
require.Contains(t, actorSql, "ActorID")

actorModel := file2.Exists(t, defaultActorModelFilePath)
require.NotContains(t, actorModel, "FirstName")
require.Contains(t, actorModel, "ActorID")
}

func TestRenameEnumValueName(t *testing.T) {
err := postgres.Generate(
tempTestDir,
Expand Down

0 comments on commit c36440d

Please sign in to comment.