Skip to content

Commit

Permalink
Add ColumnNames function (#745)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-artie committed Jun 18, 2024
1 parent e4231ed commit e28d9f2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
27 changes: 10 additions & 17 deletions lib/typing/columns/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,23 +181,7 @@ func (c *Columns) GetColumn(name string) (Column, bool) {
// This is used mostly for the SQL MERGE queries.
// TODO: Replace all uses of [GetColumnsToUpdate] with [ValidColumns]
func (c *Columns) GetColumnsToUpdate() []string {
if c == nil {
return []string{}
}

c.RLock()
defer c.RUnlock()

var cols []string
for _, col := range c.columns {
if col.KindDetails == typing.Invalid {
continue
}

cols = append(cols, col.Name())
}

return cols
return ColumnNames(c.ValidColumns())
}

// ValidColumns will filter all the `Invalid` columns so that we do not update them.
Expand Down Expand Up @@ -266,3 +250,12 @@ func RemoveDeleteColumnMarker(cols []Column) ([]Column, bool) {
cols = slices.DeleteFunc(slices.Clone(cols), func(col Column) bool { return col.Name() == constants.DeleteColumnMarker })
return cols, len(cols) != origLength
}

// ColumnNames takes a slice of [Column] and returns the names as a slice of strings.
func ColumnNames(cols []Column) []string {
result := make([]string, len(cols))
for i, col := range cols {
result[i] = col.Name()
}
return result
}
11 changes: 11 additions & 0 deletions lib/typing/columns/columns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,14 @@ func TestRemoveDeleteColumnMarker(t *testing.T) {
assert.Equal(t, []Column{col1, col2, col3}, result)
}
}

func TestColumnNames(t *testing.T) {
assert.Empty(t, ColumnNames(nil))

cols := []Column{
NewColumn("a", typing.Invalid),
NewColumn("b", typing.Invalid),
NewColumn("c", typing.Invalid),
}
assert.Equal(t, []string{"a", "b", "c"}, ColumnNames(cols))
}

0 comments on commit e28d9f2

Please sign in to comment.