diff --git a/schemas/column.go b/schemas/column.go index 08d34b91..2da18ee5 100644 --- a/schemas/column.go +++ b/schemas/column.go @@ -26,6 +26,7 @@ type Column struct { FieldIndex []int // Available only when parsed from a struct SQLType SQLType IsJSON bool + IsJSONB bool Length int64 Length2 int64 Nullable bool diff --git a/tags/parser.go b/tags/parser.go index cb14b6fc..358c8a21 100644 --- a/tags/parser.go +++ b/tags/parser.go @@ -251,7 +251,9 @@ func (parser *Parser) parseFieldWithTags(table *schemas.Table, fieldIndex int, f if col.SQLType.Name == "" { 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 { var err error col.SQLType, err = parser.getSQLTypeByType(field.Type) diff --git a/tags/parser_test.go b/tags/parser_test.go index 839da3b0..2c76d454 100644 --- a/tags/parser_test.go +++ b/tags/parser_test.go @@ -652,6 +652,18 @@ func TestParseWithJSONLongText(t *testing.T) { 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, "JSON", table.Columns()[0].SQLType.Name) 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) } diff --git a/tags/tag.go b/tags/tag.go index dfb66a7e..a11f2305 100644 --- a/tags/tag.go +++ b/tags/tag.go @@ -125,7 +125,7 @@ var defaultTagHandlers = map[string]Handler{ "UNSIGNED": UnsignedTagHandler, "COLLATE": CollateTagHandler, "JSON": JSONTagHandler, - "JSONB": JSONTagHandler, + "JSONB": JSONBTagHandler, } func init() { @@ -304,6 +304,11 @@ func JSONTagHandler(ctx *Context) error { return nil } +func JSONBTagHandler(ctx *Context) error { + ctx.col.IsJSONB = true + return nil +} + // SQLTypeTagHandler describes SQL Type tag handler func SQLTypeTagHandler(ctx *Context) error { ctx.col.SQLType = schemas.SQLType{Name: ctx.tagUname}