Add a way to lookup table columns by name
This commit is contained in:
parent
fe93ccffeb
commit
231b45d57a
2 changed files with 48 additions and 0 deletions
11
bdb/table.go
11
bdb/table.go
|
@ -10,3 +10,14 @@ type Table struct {
|
|||
|
||||
IsJoinTable bool
|
||||
}
|
||||
|
||||
// GetColumn by name. Panics if not found (for use in templates).
|
||||
func (t Table) GetColumn(name string) (col Column) {
|
||||
for _, c := range t.Columns {
|
||||
if c.Name == name {
|
||||
return c
|
||||
}
|
||||
}
|
||||
|
||||
panic("hello")
|
||||
}
|
||||
|
|
37
bdb/table_test.go
Normal file
37
bdb/table_test.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
package bdb
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestGetColumn(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
table := Table{
|
||||
Columns: []Column{
|
||||
Column{Name: "one"},
|
||||
},
|
||||
}
|
||||
|
||||
c := table.GetColumn("one")
|
||||
|
||||
if c.Name != "one" {
|
||||
t.Error("didn't get column")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetColumnMissing(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
table := Table{
|
||||
Columns: []Column{
|
||||
Column{Name: "one"},
|
||||
},
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if r := recover(); r == nil {
|
||||
t.Error("expected a panic failure")
|
||||
}
|
||||
}()
|
||||
|
||||
table.GetColumn("missing")
|
||||
}
|
Loading…
Add table
Reference in a new issue