Allow JSON and JSONB
This commit is contained in:
parent
3624e09e56
commit
f17e4f5cd6
|
@ -26,6 +26,7 @@ type Column struct {
|
||||||
FieldIndex []int // Available only when parsed from a struct
|
FieldIndex []int // Available only when parsed from a struct
|
||||||
SQLType SQLType
|
SQLType SQLType
|
||||||
IsJSON bool
|
IsJSON bool
|
||||||
|
IsJSONB bool
|
||||||
Length int64
|
Length int64
|
||||||
Length2 int64
|
Length2 int64
|
||||||
Nullable bool
|
Nullable bool
|
||||||
|
|
|
@ -251,7 +251,9 @@ func (parser *Parser) parseFieldWithTags(table *schemas.Table, fieldIndex int, f
|
||||||
|
|
||||||
if col.SQLType.Name == "" {
|
if col.SQLType.Name == "" {
|
||||||
if col.IsJSON {
|
if col.IsJSON {
|
||||||
col.SQLType = schemas.SQLType{Name: schemas.Text}
|
col.SQLType = schemas.SQLType{Name: schemas.Json}
|
||||||
|
} else if col.IsJSONB {
|
||||||
|
col.SQLType = schemas.SQLType{Name: schemas.Jsonb}
|
||||||
} else {
|
} else {
|
||||||
var err error
|
var err error
|
||||||
col.SQLType, err = parser.getSQLTypeByType(field.Type)
|
col.SQLType, err = parser.getSQLTypeByType(field.Type)
|
||||||
|
|
|
@ -652,6 +652,18 @@ func TestParseWithJSONLongText(t *testing.T) {
|
||||||
assert.EqualValues(t, "struct_with_json_long_text2", table.Name)
|
assert.EqualValues(t, "struct_with_json_long_text2", table.Name)
|
||||||
assert.EqualValues(t, 1, len(table.Columns()))
|
assert.EqualValues(t, 1, len(table.Columns()))
|
||||||
assert.EqualValues(t, "col1", table.Columns()[0].Name)
|
assert.EqualValues(t, "col1", table.Columns()[0].Name)
|
||||||
assert.EqualValues(t, "TEXT", table.Columns()[0].SQLType.Name)
|
assert.EqualValues(t, "JSON", table.Columns()[0].SQLType.Name)
|
||||||
assert.EqualValues(t, true, table.Columns()[0].IsJSON)
|
assert.EqualValues(t, true, table.Columns()[0].IsJSON)
|
||||||
|
|
||||||
|
type StructWithJSONLongText3 struct {
|
||||||
|
Col1 string `db:"jsonb"`
|
||||||
|
}
|
||||||
|
|
||||||
|
table, err = parser.Parse(reflect.ValueOf(new(StructWithJSONLongText3)))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, "struct_with_json_long_text3", table.Name)
|
||||||
|
assert.EqualValues(t, 1, len(table.Columns()))
|
||||||
|
assert.EqualValues(t, "col1", table.Columns()[0].Name)
|
||||||
|
assert.EqualValues(t, "JSONB", table.Columns()[0].SQLType.Name)
|
||||||
|
assert.EqualValues(t, true, table.Columns()[0].IsJSONB)
|
||||||
}
|
}
|
||||||
|
|
|
@ -125,7 +125,7 @@ var defaultTagHandlers = map[string]Handler{
|
||||||
"UNSIGNED": UnsignedTagHandler,
|
"UNSIGNED": UnsignedTagHandler,
|
||||||
"COLLATE": CollateTagHandler,
|
"COLLATE": CollateTagHandler,
|
||||||
"JSON": JSONTagHandler,
|
"JSON": JSONTagHandler,
|
||||||
"JSONB": JSONTagHandler,
|
"JSONB": JSONBTagHandler,
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -304,6 +304,11 @@ func JSONTagHandler(ctx *Context) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func JSONBTagHandler(ctx *Context) error {
|
||||||
|
ctx.col.IsJSONB = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// SQLTypeTagHandler describes SQL Type tag handler
|
// SQLTypeTagHandler describes SQL Type tag handler
|
||||||
func SQLTypeTagHandler(ctx *Context) error {
|
func SQLTypeTagHandler(ctx *Context) error {
|
||||||
ctx.col.SQLType = schemas.SQLType{Name: ctx.tagUname}
|
ctx.col.SQLType = schemas.SQLType{Name: ctx.tagUname}
|
||||||
|
|
Loading…
Reference in New Issue