Fix bug when json with a real SQLType in the tag
This commit is contained in:
parent
f50aacd38b
commit
3624e09e56
|
@ -250,10 +250,14 @@ func (parser *Parser) parseFieldWithTags(table *schemas.Table, fieldIndex int, f
|
|||
}
|
||||
|
||||
if col.SQLType.Name == "" {
|
||||
var err error
|
||||
col.SQLType, err = parser.getSQLTypeByType(field.Type)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if col.IsJSON {
|
||||
col.SQLType = schemas.SQLType{Name: schemas.Text}
|
||||
} else {
|
||||
var err error
|
||||
col.SQLType, err = parser.getSQLTypeByType(field.Type)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
if ctx.isUnsigned && col.SQLType.IsNumeric() && !strings.HasPrefix(col.SQLType.Name, "UNSIGNED") {
|
||||
|
|
|
@ -617,3 +617,41 @@ func TestParseWithSQLType(t *testing.T) {
|
|||
assert.EqualValues(t, "DATETIME", table.Columns()[3].SQLType.Name)
|
||||
assert.EqualValues(t, "UUID", table.Columns()[4].SQLType.Name)
|
||||
}
|
||||
|
||||
func TestParseWithJSONLongText(t *testing.T) {
|
||||
parser := NewParser(
|
||||
"db",
|
||||
dialects.QueryDialect("mysql"),
|
||||
names.GonicMapper{
|
||||
"JSON": true,
|
||||
},
|
||||
names.GonicMapper{
|
||||
"JSON": true,
|
||||
},
|
||||
caches.NewManager(),
|
||||
)
|
||||
|
||||
type StructWithJSONLongText struct {
|
||||
Col1 string `db:"LongText json"`
|
||||
}
|
||||
|
||||
table, err := parser.Parse(reflect.ValueOf(new(StructWithJSONLongText)))
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, "struct_with_json_long_text", table.Name)
|
||||
assert.EqualValues(t, 1, len(table.Columns()))
|
||||
assert.EqualValues(t, "col1", table.Columns()[0].Name)
|
||||
assert.EqualValues(t, "LONGTEXT", table.Columns()[0].SQLType.Name)
|
||||
assert.EqualValues(t, true, table.Columns()[0].IsJSON)
|
||||
|
||||
type StructWithJSONLongText2 struct {
|
||||
Col1 string `db:"json"`
|
||||
}
|
||||
|
||||
table, err = parser.Parse(reflect.ValueOf(new(StructWithJSONLongText2)))
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, "struct_with_json_long_text2", table.Name)
|
||||
assert.EqualValues(t, 1, len(table.Columns()))
|
||||
assert.EqualValues(t, "col1", table.Columns()[0].Name)
|
||||
assert.EqualValues(t, "TEXT", table.Columns()[0].SQLType.Name)
|
||||
assert.EqualValues(t, true, table.Columns()[0].IsJSON)
|
||||
}
|
||||
|
|
14
tags/tag.go
14
tags/tag.go
|
@ -124,10 +124,16 @@ var defaultTagHandlers = map[string]Handler{
|
|||
"EXTENDS": ExtendsTagHandler,
|
||||
"UNSIGNED": UnsignedTagHandler,
|
||||
"COLLATE": CollateTagHandler,
|
||||
"JSON": JSONTagHandler,
|
||||
"JSONB": JSONTagHandler,
|
||||
}
|
||||
|
||||
func init() {
|
||||
for k := range schemas.SqlTypes {
|
||||
// don't override default tag handlers
|
||||
if _, ok := defaultTagHandlers[k]; ok {
|
||||
continue
|
||||
}
|
||||
defaultTagHandlers[k] = SQLTypeTagHandler
|
||||
}
|
||||
}
|
||||
|
@ -293,12 +299,14 @@ func CollateTagHandler(ctx *Context) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func JSONTagHandler(ctx *Context) error {
|
||||
ctx.col.IsJSON = true
|
||||
return nil
|
||||
}
|
||||
|
||||
// SQLTypeTagHandler describes SQL Type tag handler
|
||||
func SQLTypeTagHandler(ctx *Context) error {
|
||||
ctx.col.SQLType = schemas.SQLType{Name: ctx.tagUname}
|
||||
if ctx.tagUname == "JSON" || ctx.tagUname == "JSONB" {
|
||||
ctx.col.IsJSON = true
|
||||
}
|
||||
if len(ctx.params) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue