Add SetTagIdentifier

This commit is contained in:
Lunny Xiao 2021-02-09 11:56:29 +08:00
parent 4629bb1717
commit ae48cdeca5
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
3 changed files with 41 additions and 0 deletions

View File

@ -183,6 +183,10 @@ func (engine *Engine) SetDisableGlobalCache(disable bool) {
engine.cacherMgr.SetDisableGlobalCache(disable)
}
func (engine *Engine) SetTagIdentifier(tagName string) {
engine.tagParser.SetIdentifier(tagName)
}
// DriverName return the current sql driver's name
func (engine *Engine) DriverName() string {
return engine.driverName

View File

@ -34,6 +34,7 @@ type Parser struct {
tableCache sync.Map // map[reflect.Type]*schemas.Table
}
// NewParser create a new tag parser
func NewParser(identifier string, dialect dialects.Dialect, tableMapper, columnMapper names.Mapper, cacherMgr *caches.Manager) *Parser {
return &Parser{
identifier: identifier,
@ -45,24 +46,34 @@ func NewParser(identifier string, dialect dialects.Dialect, tableMapper, columnM
}
}
// GetTableMapper returns the table mapper
func (parser *Parser) GetTableMapper() names.Mapper {
return parser.tableMapper
}
// SetTableMapper sets table mapper
func (parser *Parser) SetTableMapper(mapper names.Mapper) {
parser.ClearCaches()
parser.tableMapper = mapper
}
// GetColumnMapper returns the column mapper
func (parser *Parser) GetColumnMapper() names.Mapper {
return parser.columnMapper
}
// SetColumnMapper set column mapper
func (parser *Parser) SetColumnMapper(mapper names.Mapper) {
parser.ClearCaches()
parser.columnMapper = mapper
}
// SetIdentifier set tag identifier
func (parser *Parser) SetIdentifier(tagName string) {
parser.identifier = tagName
}
// ParseWithCache parse with cache
func (parser *Parser) ParseWithCache(v reflect.Value) (*schemas.Table, error) {
t := v.Type()
tableI, ok := parser.tableCache.Load(t)

View File

@ -12,6 +12,7 @@ import (
"xorm.io/xorm/caches"
"xorm.io/xorm/dialects"
"xorm.io/xorm/names"
"xorm.io/xorm/schemas"
)
type ParseTableName1 struct{}
@ -80,3 +81,28 @@ func TestUnexportField(t *testing.T) {
assert.NotEqual(t, "public", col.Name)
}
}
type ParseTableName3 struct {
Name string `orm:"varchar(50)"`
}
func TestParseTableNameWithOtherIdentifier(t *testing.T) {
parser := NewParser(
"xorm",
dialects.QueryDialect("mysql"),
names.SnakeMapper{},
names.SnakeMapper{},
caches.NewManager(),
)
parser.SetIdentifier("orm")
table, err := parser.Parse(reflect.ValueOf(new(ParseTableName3)))
assert.NoError(t, err)
cols := table.Columns()
assert.EqualValues(t, 1, len(cols))
assert.EqualValues(t, "name", cols[0].Name)
assert.EqualValues(t, schemas.SQLType{
Name: "VARCHAR",
}, cols[0].SQLType)
assert.EqualValues(t, 50, cols[0].Length)
assert.EqualValues(t, true, cols[0].Nullable)
}