Add a GetTable helper.

This commit is contained in:
Aaron L 2016-07-11 23:48:30 -07:00
parent 231b45d57a
commit bac9bb30ce
2 changed files with 45 additions and 2 deletions

View file

@ -1,5 +1,7 @@
package bdb package bdb
import "fmt"
// Table metadata from the database schema. // Table metadata from the database schema.
type Table struct { type Table struct {
Name string Name string
@ -11,7 +13,18 @@ type Table struct {
IsJoinTable bool IsJoinTable bool
} }
// GetColumn by name. Panics if not found (for use in templates). // GetTable by name. Panics if not found (for use in templates mostly).
func GetTable(tables []Table, name string) (tbl Table) {
for _, t := range tables {
if t.Name == name {
return t
}
}
panic(fmt.Sprintf("could not find table name: %s", name))
}
// GetColumn by name. Panics if not found (for use in templates mostly).
func (t Table) GetColumn(name string) (col Column) { func (t Table) GetColumn(name string) (col Column) {
for _, c := range t.Columns { for _, c := range t.Columns {
if c.Name == name { if c.Name == name {
@ -19,5 +32,5 @@ func (t Table) GetColumn(name string) (col Column) {
} }
} }
panic("hello") panic(fmt.Sprintf("could not find column name: %s", name))
} }

View file

@ -2,6 +2,36 @@ package bdb
import "testing" import "testing"
func TestGetTable(t *testing.T) {
t.Parallel()
tables := []Table{
{Name: "one"},
}
tbl := GetTable(tables, "one")
if tbl.Name != "one" {
t.Error("didn't get column")
}
}
func TestGetTableMissing(t *testing.T) {
t.Parallel()
tables := []Table{
{Name: "one"},
}
defer func() {
if r := recover(); r == nil {
t.Error("expected a panic failure")
}
}()
GetTable(tables, "missing")
}
func TestGetColumn(t *testing.T) { func TestGetColumn(t *testing.T) {
t.Parallel() t.Parallel()