Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions Grid.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,48 @@ func (grid Grid) Cols() []Col {
}

// Col returns the column matching the name
func (grid Grid) Col(name string) Col {
func (grid Grid) Col(name string) *Col {
var colMatch Col
for i := 0; i < grid.ColCount(); i++ {
col := grid.ColAt(i)
if grid.cols[i].name == name {
colMatch = col
return colMatch
return &colMatch
}
}
panic("Unknown column: " + name)
return nil
}

// ColAt returns the column at the index
func (grid Grid) ColAt(index int) Col {
return grid.cols[index]
}

// Meta returns the grid-level metadata
func (grid Grid) RenameCol(from string, to string) Grid {
builder := NewGridBuilder()
builder.AddMeta(grid.meta.items)
for _, col := range grid.cols {
if col.name == from {
builder.AddCol(to, col.meta.items)
} else {
builder.AddCol(col.name, col.meta.items)
}
}
for _, row := range grid.rows {
newItems := map[string]Val{}
for name, val := range row.items {
if name == from {
newItems[to] = val
} else {
newItems[name] = val
}
}
builder.AddRowDict(NewDict(newItems))
}
return builder.ToGrid()
}

// RowCount returns the count of rows
func (grid Grid) RowCount() int {
return len(grid.rows)
Expand Down
17 changes: 17 additions & 0 deletions Grid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ func TestGrid_ColAt(t *testing.T) {
}
}

func TestGrid_RenameCol(t *testing.T) {
grid := newGridSimple()
renamedGrid := grid.RenameCol("siteName", "siteName2")

assert.Equal(t, grid.ColCount(), renamedGrid.ColCount())
assert.Equal(t, grid.RowCount(), renamedGrid.RowCount())
assert.NotNil(t, grid.Col("siteName"))
assert.Nil(t, grid.Col("siteName2"))
assert.Nil(t, renamedGrid.Col("siteName"))
assert.NotNil(t, renamedGrid.Col("siteName2"))

for i, row := range grid.Rows() {
renamedRow := renamedGrid.RowAt(i)
assert.Equal(t, row.Get("siteName"), renamedRow.Get("siteName2"))
}
}

func TestGrid_RowCount(t *testing.T) {
grid := newGridSimple()
if grid.RowCount() != 2 {
Expand Down