diff --git a/genny/fizz/cempty/mocks_test.go b/genny/fizz/cempty/mocks_test.go index f8dd9c37..d8f454d9 100644 --- a/genny/fizz/cempty/mocks_test.go +++ b/genny/fizz/cempty/mocks_test.go @@ -4,4 +4,4 @@ type mockTranslator struct{} func (mockTranslator) Name() string { return "test" -} \ No newline at end of file +} diff --git a/model.go b/model.go index 4690d549..d6b83bdc 100644 --- a/model.go +++ b/model.go @@ -88,19 +88,23 @@ func (m *Model) TableName() string { } t := reflect.TypeOf(m.Value) - name := m.typeName(t) + name, cacheKey := m.typeName(t) defer tableMapMu.Unlock() tableMapMu.Lock() - if tableMap[name] == "" { + if tableMap[cacheKey] == "" { m.tableName = nflect.Tableize(name) - tableMap[name] = m.tableName + tableMap[cacheKey] = m.tableName } - return tableMap[name] + return tableMap[cacheKey] } -func (m *Model) typeName(t reflect.Type) string { +func (m *Model) cacheKey(t reflect.Type) string { + return t.PkgPath() + "." + t.Name() +} + +func (m *Model) typeName(t reflect.Type) (name, cacheKey string) { if t.Kind() == reflect.Ptr { t = t.Elem() } @@ -117,14 +121,14 @@ func (m *Model) typeName(t reflect.Type) string { v := reflect.New(el) out := v.MethodByName("TableName").Call([]reflect.Value{}) name := out[0].String() - if tableMap[el.Name()] == "" { - tableMap[el.Name()] = name + if tableMap[m.cacheKey(el)] == "" { + tableMap[m.cacheKey(el)] = name } } - return el.Name() + return el.Name(), m.cacheKey(el) default: - return t.Name() + return t.Name(), m.cacheKey(t) } } diff --git a/model_test.go b/model_test.go index dfe26e8a..8de46ce1 100644 --- a/model_test.go +++ b/model_test.go @@ -4,7 +4,11 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "github.com/gobuffalo/pop/testdata/models/a" + "github.com/gobuffalo/pop/testdata/models/b" ) func Test_Model_TableName(t *testing.T) { @@ -36,6 +40,15 @@ func (tn) TableName() string { return "this is my table name" } +// A failing test case for #477 +func Test_TableNameCache(t *testing.T) { + r := assert.New(t) + r.Equal("usera", (&Model{Value: a.User{}}).TableName()) + r.Equal("userb", (&Model{Value: b.User{}}).TableName()) + r.Equal("usera", (&Model{Value: []a.User{}}).TableName()) + r.Equal("userb", (&Model{Value: []b.User{}}).TableName()) +} + func Test_TableName(t *testing.T) { r := require.New(t) diff --git a/testdata/models/a/user.go b/testdata/models/a/user.go new file mode 100644 index 00000000..86cdbfe2 --- /dev/null +++ b/testdata/models/a/user.go @@ -0,0 +1,7 @@ +package a + +type User struct{} + +func (u User) TableName() string { + return "usera" +} diff --git a/testdata/models/b/user.go b/testdata/models/b/user.go new file mode 100644 index 00000000..f870f18f --- /dev/null +++ b/testdata/models/b/user.go @@ -0,0 +1,7 @@ +package b + +type User struct{} + +func (u User) TableName() string { + return "userb" +}