1
0
mirror of https://github.com/astaxie/beego.git synced 2025-06-14 03:30:39 +00:00
This commit is contained in:
michuan
2020-08-25 23:36:15 +08:00
parent 310161f9d4
commit 9bd3a27e80
5 changed files with 52 additions and 18 deletions

View File

@ -53,18 +53,24 @@ func (e *SliceStringField) FieldType() int {
}
func (e *SliceStringField) SetRaw(value interface{}) error {
switch d := value.(type) {
case []string:
e.Set(d)
case string:
if len(d) > 0 {
parts := strings.Split(d, ",")
f := func(str string) {
if len(str) > 0 {
parts := strings.Split(str, ",")
v := make([]string, 0, len(parts))
for _, p := range parts {
v = append(v, strings.TrimSpace(p))
}
e.Set(v)
}
}
switch d := value.(type) {
case []string:
e.Set(d)
case string:
f(d)
case []byte:
f(string(d))
default:
return fmt.Errorf("<SliceStringField.SetRaw> unknown value `%v`", value)
}
@ -96,6 +102,8 @@ func (e *JSONFieldTest) SetRaw(value interface{}) error {
switch d := value.(type) {
case string:
return json.Unmarshal([]byte(d), e)
case []byte:
return json.Unmarshal(d, e)
default:
return fmt.Errorf("<JSONField.SetRaw> unknown value `%v`", value)
}

View File

@ -19,6 +19,8 @@ import (
"fmt"
"reflect"
"time"
"github.com/pkg/errors"
)
// raw sql string prepared statement
@ -368,7 +370,15 @@ func (o *rawSet) QueryRow(containers ...interface{}) error {
field.Set(mf)
field = mf.Elem().FieldByIndex(fi.relModelInfo.fields.pk.fieldIndex)
}
o.setFieldValue(field, value)
if fi.isFielder {
fd := field.Addr().Interface().(Fielder)
err := fd.SetRaw(value)
if err != nil {
return errors.Errorf("set raw error:%s", err)
}
} else {
o.setFieldValue(field, value)
}
}
}
} else {
@ -509,7 +519,15 @@ func (o *rawSet) QueryRows(containers ...interface{}) (int64, error) {
field.Set(mf)
field = mf.Elem().FieldByIndex(fi.relModelInfo.fields.pk.fieldIndex)
}
o.setFieldValue(field, value)
if fi.isFielder {
fd := field.Addr().Interface().(Fielder)
err := fd.SetRaw(value)
if err != nil {
return 0, errors.Errorf("set raw error:%s", err)
}
} else {
o.setFieldValue(field, value)
}
}
}
} else {

View File

@ -769,6 +769,20 @@ func TestCustomField(t *testing.T) {
throwFailNow(t, AssertIs(user.Extra.Name, "beego"))
throwFailNow(t, AssertIs(user.Extra.Data, "orm"))
var users []User
Q := dDbBaser.TableQuote()
n, err := dORM.Raw(fmt.Sprintf("SELECT * FROM %suser%s where id=?", Q, Q), 2).QueryRows(&users)
throwFailNow(t, err)
throwFailNow(t, AssertIs(n, 1))
throwFailNow(t, AssertIs(users[0].Extra.Name, "beego"))
throwFailNow(t, AssertIs(users[0].Extra.Data, "orm"))
user = User{}
err = dORM.Raw(fmt.Sprintf("SELECT * FROM %suser%s where id=?", Q, Q), 2).QueryRow(&user)
throwFailNow(t, err)
throwFailNow(t, AssertIs(user.Extra.Name, "beego"))
throwFailNow(t, AssertIs(user.Extra.Data, "orm"))
}
func TestExpr(t *testing.T) {