Fix json tag with other type #1822
|
@ -394,6 +394,60 @@ func TestJSONString(t *testing.T) {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.EqualValues(t, 1, len(jss))
|
assert.EqualValues(t, 1, len(jss))
|
||||||
assert.True(t, `["1","2"]` == jss[0].Content || `["1", "2"]` == jss[0].Content)
|
assert.True(t, `["1","2"]` == jss[0].Content || `["1", "2"]` == jss[0].Content)
|
||||||
|
|
||||||
|
type JsonAnonymousStruct struct {
|
||||||
|
Id int64
|
||||||
|
JsonString `xorm:"'json_string' JSON LONGTEXT"`
|
||||||
|
}
|
||||||
|
|
||||||
|
assertSync(t, new(JsonAnonymousStruct))
|
||||||
|
|
||||||
|
_, err = testEngine.Insert(&JsonAnonymousStruct{
|
||||||
|
JsonString: JsonString{
|
||||||
|
Content: "1",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
var jas JsonAnonymousStruct
|
||||||
|
has, err = testEngine.Get(&jas)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.True(t, has)
|
||||||
|
assert.EqualValues(t, 1, jas.Id)
|
||||||
|
assert.EqualValues(t, "1", jas.Content)
|
||||||
|
|
||||||
|
var jass []JsonAnonymousStruct
|
||||||
|
err = testEngine.Find(&jass)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, 1, len(jass))
|
||||||
|
assert.EqualValues(t, "1", jass[0].Content)
|
||||||
|
|
||||||
|
type JsonStruct struct {
|
||||||
|
Id int64
|
||||||
|
JSON JsonString `xorm:"'json_string' JSON LONGTEXT"`
|
||||||
|
}
|
||||||
|
|
||||||
|
assertSync(t, new(JsonStruct))
|
||||||
|
|
||||||
|
_, err = testEngine.Insert(&JsonStruct{
|
||||||
|
JSON: JsonString{
|
||||||
|
Content: "2",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
var jst JsonStruct
|
||||||
|
has, err = testEngine.Get(&jst)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.True(t, has)
|
||||||
|
assert.EqualValues(t, 1, jst.Id)
|
||||||
|
assert.EqualValues(t, "2", jst.JSON.Content)
|
||||||
|
|
||||||
|
var jsts []JsonStruct
|
||||||
|
err = testEngine.Find(&jsts)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, 1, len(jsts))
|
||||||
|
assert.EqualValues(t, "2", jsts[0].JSON.Content)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetActionMapping(t *testing.T) {
|
func TestGetActionMapping(t *testing.T) {
|
||||||
|
|
|
@ -704,7 +704,7 @@ func (statement *Statement) buildConds2(table *schemas.Table, bean interface{},
|
||||||
col.SQLType.IsBlob() || col.SQLType.Name == schemas.TimeStampz) {
|
col.SQLType.IsBlob() || col.SQLType.Name == schemas.TimeStampz) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if col.SQLType.IsJson() {
|
if col.IsJSON {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -813,7 +813,7 @@ func (statement *Statement) buildConds2(table *schemas.Table, bean interface{},
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if col.SQLType.IsJson() {
|
if col.IsJSON {
|
||||||
if col.SQLType.IsText() {
|
if col.SQLType.IsText() {
|
||||||
bytes, err := json.DefaultJSONHandler.Marshal(fieldValue.Interface())
|
bytes, err := json.DefaultJSONHandler.Marshal(fieldValue.Interface())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -204,7 +204,7 @@ func (statement *Statement) BuildUpdates(tableValue reflect.Value,
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if !col.SQLType.IsJson() {
|
if !col.IsJSON {
|
||||||
table, err := statement.tagParser.ParseWithCache(fieldValue)
|
table, err := statement.tagParser.ParseWithCache(fieldValue)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
val = fieldValue.Interface()
|
val = fieldValue.Interface()
|
||||||
|
|
|
@ -86,7 +86,7 @@ func (statement *Statement) Value2Interface(col *schemas.Column, fieldValue refl
|
||||||
return t.Float64, nil
|
return t.Float64, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if !col.SQLType.IsJson() {
|
if !col.IsJSON {
|
||||||
// !<winxxp>! 增加支持driver.Valuer接口的结构,如sql.NullString
|
// !<winxxp>! 增加支持driver.Valuer接口的结构,如sql.NullString
|
||||||
if v, ok := fieldValue.Interface().(driver.Valuer); ok {
|
if v, ok := fieldValue.Interface().(driver.Valuer); ok {
|
||||||
return v.Value()
|
return v.Value()
|
||||||
|
|
|
@ -51,6 +51,7 @@ type Column struct {
|
||||||
func NewColumn(name, fieldName string, sqlType SQLType, len1, len2 int, nullable bool) *Column {
|
func NewColumn(name, fieldName string, sqlType SQLType, len1, len2 int, nullable bool) *Column {
|
||||||
return &Column{
|
return &Column{
|
||||||
Name: name,
|
Name: name,
|
||||||
|
IsJSON: sqlType.IsJson(),
|
||||||
TableName: "",
|
TableName: "",
|
||||||
FieldName: fieldName,
|
FieldName: fieldName,
|
||||||
SQLType: sqlType,
|
SQLType: sqlType,
|
||||||
|
|
|
@ -503,7 +503,7 @@ func (session *Session) slice2Bean(scanResults []interface{}, fields []string, b
|
||||||
fieldType := fieldValue.Type()
|
fieldType := fieldValue.Type()
|
||||||
hasAssigned := false
|
hasAssigned := false
|
||||||
|
|
||||||
if col.SQLType.IsJson() {
|
if col.IsJSON {
|
||||||
var bs []byte
|
var bs []byte
|
||||||
if rawValueType.Kind() == reflect.String {
|
if rawValueType.Kind() == reflect.String {
|
||||||
bs = []byte(vv.String())
|
bs = []byte(vv.String())
|
||||||
|
@ -683,7 +683,7 @@ func (session *Session) slice2Bean(scanResults []interface{}, fields []string, b
|
||||||
session.engine.logger.Errorf("sql.Sanner error: %v", err)
|
session.engine.logger.Errorf("sql.Sanner error: %v", err)
|
||||||
hasAssigned = false
|
hasAssigned = false
|
||||||
}
|
}
|
||||||
} else if col.SQLType.IsJson() {
|
} else if col.IsJSON {
|
||||||
if rawValueType.Kind() == reflect.String {
|
if rawValueType.Kind() == reflect.String {
|
||||||
hasAssigned = true
|
hasAssigned = true
|
||||||
x := reflect.New(fieldType)
|
x := reflect.New(fieldType)
|
||||||
|
|
|
@ -226,6 +226,9 @@ func CommentTagHandler(ctx *Context) error {
|
||||||
// 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.tagName}
|
ctx.col.SQLType = schemas.SQLType{Name: ctx.tagName}
|
||||||
|
if strings.EqualFold(ctx.tagName, "JSON") {
|
||||||
|
ctx.col.IsJSON = true
|
||||||
|
}
|
||||||
if len(ctx.params) > 0 {
|
if len(ctx.params) > 0 {
|
||||||
if ctx.tagName == schemas.Enum {
|
if ctx.tagName == schemas.Enum {
|
||||||
ctx.col.EnumOptions = make(map[string]int)
|
ctx.col.EnumOptions = make(map[string]int)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user