From bac9bb30ce56ceceaaf350b5b2a042c36753e3d8 Mon Sep 17 00:00:00 2001 From: Aaron L Date: Mon, 11 Jul 2016 23:48:30 -0700 Subject: [PATCH] Add a GetTable helper. --- bdb/table.go | 17 +++++++++++++++-- bdb/table_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/bdb/table.go b/bdb/table.go index 90f9a85..3bdbd9f 100644 --- a/bdb/table.go +++ b/bdb/table.go @@ -1,5 +1,7 @@ package bdb +import "fmt" + // Table metadata from the database schema. type Table struct { Name string @@ -11,7 +13,18 @@ type Table struct { 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) { for _, c := range t.Columns { 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)) } diff --git a/bdb/table_test.go b/bdb/table_test.go index 3b637ab..9a26449 100644 --- a/bdb/table_test.go +++ b/bdb/table_test.go @@ -2,6 +2,36 @@ package bdb 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) { t.Parallel()