fix deepsource bug
This commit is contained in:
parent
4d6b22ef08
commit
580d694b0e
@ -1,5 +1,6 @@
|
|||||||
# developing
|
# developing
|
||||||
|
|
||||||
|
- Add: Convenient way to generate mock object [4620](https://github.com/beego/beego/issues/4397)
|
||||||
- Lint: use golangci-lint. [4619](https://github.com/beego/beego/pull/4619)
|
- Lint: use golangci-lint. [4619](https://github.com/beego/beego/pull/4619)
|
||||||
- Chore: format code. [4615](https://github.com/beego/beego/pull/4615)
|
- Chore: format code. [4615](https://github.com/beego/beego/pull/4615)
|
||||||
- Env: non-empty GOBIN & GOPATH. [4613](https://github.com/beego/beego/pull/4613)
|
- Env: non-empty GOBIN & GOPATH. [4613](https://github.com/beego/beego/pull/4613)
|
||||||
|
|||||||
@ -10,7 +10,6 @@ import (
|
|||||||
// the mock object must be pointer of struct
|
// the mock object must be pointer of struct
|
||||||
// the element in mock object can be slices, structures, basic data types, pointers and interface
|
// the element in mock object can be slices, structures, basic data types, pointers and interface
|
||||||
func Mock(v interface{}) (err error) {
|
func Mock(v interface{}) (err error) {
|
||||||
|
|
||||||
pv := reflect.ValueOf(v)
|
pv := reflect.ValueOf(v)
|
||||||
// the input must be pointer of struct
|
// the input must be pointer of struct
|
||||||
if pv.Kind() != reflect.Ptr || pv.IsNil() {
|
if pv.Kind() != reflect.Ptr || pv.IsNil() {
|
||||||
@ -26,7 +25,7 @@ func mock(pv reflect.Value) (err error) {
|
|||||||
for i := 0; i < pt.Elem().NumField(); i++ {
|
for i := 0; i < pt.Elem().NumField(); i++ {
|
||||||
ptt := pt.Elem().Field(i)
|
ptt := pt.Elem().Field(i)
|
||||||
pvv := pv.Elem().FieldByName(ptt.Name)
|
pvv := pv.Elem().FieldByName(ptt.Name)
|
||||||
if !pvv.CanSet() || !pvv.CanAddr() {
|
if !pvv.CanSet() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
kt := ptt.Type.Kind()
|
kt := ptt.Type.Kind()
|
||||||
@ -79,8 +78,12 @@ func mock(pv reflect.Value) (err error) {
|
|||||||
|
|
||||||
// mock slice value
|
// mock slice value
|
||||||
func mockSlice(tagValue string, pvv reflect.Value) (err error) {
|
func mockSlice(tagValue string, pvv reflect.Value) (err error) {
|
||||||
|
if len(tagValue) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
sliceMetas := strings.Split(tagValue, ":")
|
sliceMetas := strings.Split(tagValue, ":")
|
||||||
if len(sliceMetas) != 2 || sliceMetas[0] != "length" {
|
if len(sliceMetas) != 2 || sliceMetas[0] != "length" {
|
||||||
|
err = fmt.Errorf("the value:%s is invalid", tagValue)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
length, e := strconv.Atoi(sliceMetas[1])
|
length, e := strconv.Atoi(sliceMetas[1])
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user