Skip to content

Commit

Permalink
Clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Sep 26, 2024
1 parent fa31580 commit 39d65c8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/rdbms/column/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,18 @@ func FilterOutExcludedColumns[T ~int, O any](columns []Column[T, O], excludeName
}
return result, nil
}

// FilterForIncludedColumns returns a list of columns including only those that match `includeNames`.
func FilterForIncludedColumns[T ~int, O any](columns []Column[T, O], includeNames []string) ([]Column[T, O], error) {
if len(includeNames) == 0 {
return columns, nil
}

var result []Column[T, O]
for _, column := range columns {
if slices.Contains(includeNames, column.Name) {
result = append(result, column)
}
}
return result, nil
}
21 changes: 21 additions & 0 deletions lib/rdbms/column/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,24 @@ func TestFilterOutExcludedColumns(t *testing.T) {
assert.ErrorContains(t, err, `cannot exclude primary key column "bar"`)
}
}

func TestFilterForIncludedColumns(t *testing.T) {
{
// Empty `includeNames`
value, err := FilterForIncludedColumns([]mockColumn{{Name: "foo"}}, []string{})
assert.NoError(t, err)
assert.Equal(t, value, []mockColumn{{Name: "foo"}})
}
{
// Non-empty `includeNames`, included column is not in list
value, err := FilterForIncludedColumns([]mockColumn{{Name: "foo"}}, []string{"bar"})
assert.NoError(t, err)
assert.Equal(t, value, []mockColumn(nil))
}
{
// Non-empty `includeNames`, included column is in list
value, err := FilterForIncludedColumns([]mockColumn{{Name: "foo"}, {Name: "bar"}}, []string{"bar"})
assert.NoError(t, err)
assert.Equal(t, value, []mockColumn{{Name: "bar"}})
}
}
7 changes: 7 additions & 0 deletions sources/postgres/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,18 @@ func NewPostgresAdapter(db *sql.DB, tableCfg config.PostgreSQLTable) (PostgresAd
return PostgresAdapter{}, fmt.Errorf("failed to load metadata for table %s.%s: %w", tableCfg.Schema, tableCfg.Name, err)
}

// Exclude columns (if any) from the table metadata
columns, err := column.FilterOutExcludedColumns(table.Columns, tableCfg.ExcludeColumns, table.PrimaryKeys)
if err != nil {
return PostgresAdapter{}, err
}

// Include columns (if any) from the table metadata
columns, err = column.FilterForIncludedColumns(columns, tableCfg.IncludeColumns)
if err != nil {
return PostgresAdapter{}, err
}

fieldConverters := make([]transformer.FieldConverter, len(columns))
for i, col := range columns {
converter, err := valueConverterForType(col.Type, col.Opts)
Expand Down

0 comments on commit 39d65c8

Please sign in to comment.