replace parser cached interface with dest interface

This commit is contained in:
sunyuanhua 2021-02-04 17:04:34 +08:00
parent 3d3208ed60
commit 60fac30335
2 changed files with 27 additions and 1 deletions

View File

@ -97,7 +97,8 @@ func Parse(dest interface{}, cacheStore *sync.Map, namer Namer) (*Schema, error)
modelValue := reflect.New(modelType) modelValue := reflect.New(modelType)
tableName := namer.TableName(modelType.Name()) tableName := namer.TableName(modelType.Name())
if tabler, ok := modelValue.Interface().(Tabler); ok { //if tabler, ok := modelValue.Interface().(Tabler); ok {
if tabler, ok := dest.(Tabler); ok {
tableName = tabler.TableName() tableName = tabler.TableName()
} }
if en, ok := namer.(embeddedNamer); ok { if en, ok := namer.(embeddedNamer); ok {

View File

@ -1,6 +1,7 @@
package schema_test package schema_test
import ( import (
"fmt"
"strings" "strings"
"sync" "sync"
"testing" "testing"
@ -297,3 +298,27 @@ func TestEmbeddedStructForCustomizedNamingStrategy(t *testing.T) {
}) })
} }
} }
type DynamicTable struct {
ID int
}
func (d *DynamicTable) TableName() string {
return fmt.Sprintf("dynamic_%d",d.ID)
}
func TestDynamicTableName(t *testing.T) {
for i:=0;i<=10;i++ {
dt := &DynamicTable{ID:i}
dynamic, err := schema.Parse(dt, &sync.Map{}, schema.NamingStrategy{})
if err != nil {
t.Fatalf("failed to parse pointer user, got error %v", err)
}
if dynamic.Table != dt.TableName(){
t.Errorf("Failed to customize table with TableName method")
}
}
}