mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 05:20:55 +00:00
Merge pull request #4160 from AllenX2018/Improve-orm.Fielder-function
fix issue #3776
This commit is contained in:
commit
8574e30b3a
@ -54,18 +54,24 @@ func (e *SliceStringField) FieldType() int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *SliceStringField) SetRaw(value interface{}) error {
|
func (e *SliceStringField) SetRaw(value interface{}) error {
|
||||||
switch d := value.(type) {
|
f := func(str string) {
|
||||||
case []string:
|
if len(str) > 0 {
|
||||||
e.Set(d)
|
parts := strings.Split(str, ",")
|
||||||
case string:
|
|
||||||
if len(d) > 0 {
|
|
||||||
parts := strings.Split(d, ",")
|
|
||||||
v := make([]string, 0, len(parts))
|
v := make([]string, 0, len(parts))
|
||||||
for _, p := range parts {
|
for _, p := range parts {
|
||||||
v = append(v, strings.TrimSpace(p))
|
v = append(v, strings.TrimSpace(p))
|
||||||
}
|
}
|
||||||
e.Set(v)
|
e.Set(v)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
switch d := value.(type) {
|
||||||
|
case []string:
|
||||||
|
e.Set(d)
|
||||||
|
case string:
|
||||||
|
f(d)
|
||||||
|
case []byte:
|
||||||
|
f(string(d))
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("<SliceStringField.SetRaw> unknown value `%v`", value)
|
return fmt.Errorf("<SliceStringField.SetRaw> unknown value `%v`", value)
|
||||||
}
|
}
|
||||||
@ -97,6 +103,8 @@ func (e *JSONFieldTest) SetRaw(value interface{}) error {
|
|||||||
switch d := value.(type) {
|
switch d := value.(type) {
|
||||||
case string:
|
case string:
|
||||||
return json.Unmarshal([]byte(d), e)
|
return json.Unmarshal([]byte(d), e)
|
||||||
|
case []byte:
|
||||||
|
return json.Unmarshal(d, e)
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("<JSONField.SetRaw> unknown value `%v`", value)
|
return fmt.Errorf("<JSONField.SetRaw> unknown value `%v`", value)
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@ package orm
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/pkg/errors"
|
||||||
"reflect"
|
"reflect"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -369,9 +370,17 @@ func (o *rawSet) QueryRow(containers ...interface{}) error {
|
|||||||
field.Set(mf)
|
field.Set(mf)
|
||||||
field = mf.Elem().FieldByIndex(fi.relModelInfo.fields.pk.fieldIndex)
|
field = mf.Elem().FieldByIndex(fi.relModelInfo.fields.pk.fieldIndex)
|
||||||
}
|
}
|
||||||
|
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)
|
o.setFieldValue(field, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
for i := 0; i < ind.NumField(); i++ {
|
for i := 0; i < ind.NumField(); i++ {
|
||||||
f := ind.Field(i)
|
f := ind.Field(i)
|
||||||
@ -510,9 +519,17 @@ func (o *rawSet) QueryRows(containers ...interface{}) (int64, error) {
|
|||||||
field.Set(mf)
|
field.Set(mf)
|
||||||
field = mf.Elem().FieldByIndex(fi.relModelInfo.fields.pk.fieldIndex)
|
field = mf.Elem().FieldByIndex(fi.relModelInfo.fields.pk.fieldIndex)
|
||||||
}
|
}
|
||||||
|
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)
|
o.setFieldValue(field, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// define recursive function
|
// define recursive function
|
||||||
var recursiveSetField func(rv reflect.Value)
|
var recursiveSetField func(rv reflect.Value)
|
||||||
|
@ -777,6 +777,20 @@ func TestCustomField(t *testing.T) {
|
|||||||
|
|
||||||
throwFailNow(t, AssertIs(user.Extra.Name, "beego"))
|
throwFailNow(t, AssertIs(user.Extra.Name, "beego"))
|
||||||
throwFailNow(t, AssertIs(user.Extra.Data, "orm"))
|
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) {
|
func TestExpr(t *testing.T) {
|
||||||
@ -2543,4 +2557,3 @@ func TestStrPkInsert(t *testing.T) {
|
|||||||
throwFailNow(t, AssertIs(err, nil))
|
throwFailNow(t, AssertIs(err, nil))
|
||||||
throwFailNow(t, AssertIs(vForTesting.Value, value))
|
throwFailNow(t, AssertIs(vForTesting.Value, value))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user