beego/core/bean/mock.go
Ming Deng f81689dfb1
Release v2.0.5 (#5033)
* add: generic cache random time offset expired.

* bugfix: Csrf token should be Secure and httpOnly, but not now

* fix: expose the Offset property to allow external modifications

* improving the concurrency performance of random value calculation

* add WithOffsetFunc to define private RandomExpireCache.offset field

* fix: add seconds definition

* build(deps): bump github.com/stretchr/testify from 1.7.1 to 1.8.0

Bumps [github.com/stretchr/testify](https://github.com/stretchr/testify) from 1.7.1 to 1.8.0.
- [Release notes](https://github.com/stretchr/testify/releases)
- [Commits](https://github.com/stretchr/testify/compare/v1.7.1...v1.8.0)

---
updated-dependencies:
- dependency-name: github.com/stretchr/testify
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

* fix 4907: force admin service http only

* Feat: add get all tasks function (#4999)

* feat: add get all tasks function

* Refine Comments : admin/profile.go,bean/mock.go,config/global.go... (#5009)

* Refine Comments

* refine comments for cache.go

* refine comments for log.go

* Update orm.go

* refine comments for orm_log.go,types.go

* Update utils.go

* Update doc.go

* refine comments for for four files (#5011)

* refine comments for cache.go

* refine comments for log.go

* Update orm.go

* refine comments for orm_log.go,types.go

* Update utils.go

* Update doc.go

* Update db.go

* fix pass []any as any in variadic function by asasalint (#5012)

* fix pass []any as any in variadic function

* add change log

* build(deps): bump go.opentelemetry.io/otel/trace from 1.7.0 to 1.8.0 (#5019)

Bumps [go.opentelemetry.io/otel/trace](https://github.com/open-telemetry/opentelemetry-go) from 1.7.0 to 1.8.0.
- [Release notes](https://github.com/open-telemetry/opentelemetry-go/releases)
- [Changelog](https://github.com/open-telemetry/opentelemetry-go/blob/main/CHANGELOG.md)
- [Commits](https://github.com/open-telemetry/opentelemetry-go/compare/v1.7.0...v1.8.0)

---
updated-dependencies:
- dependency-name: go.opentelemetry.io/otel/trace
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* refine comments for package core  (#5014)

* Refine Comments

* refine comments for cache.go

* refine comments for log.go

* Update orm.go

* refine comments for orm_log.go,types.go

* Update utils.go

* Update doc.go

* refine comments

* refine comments

* Update db.go

* refine comments for core

* build(deps): bump go.opentelemetry.io/otel/exporters/stdout/stdouttrace (#5018)

Bumps [go.opentelemetry.io/otel/exporters/stdout/stdouttrace](https://github.com/open-telemetry/opentelemetry-go) from 1.7.0 to 1.8.0.
- [Release notes](https://github.com/open-telemetry/opentelemetry-go/releases)
- [Changelog](https://github.com/open-telemetry/opentelemetry-go/blob/main/CHANGELOG.md)
- [Commits](https://github.com/open-telemetry/opentelemetry-go/compare/v1.7.0...v1.8.0)

---
updated-dependencies:
- dependency-name: go.opentelemetry.io/otel/exporters/stdout/stdouttrace
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* fix 5022: Miss assiging ln to graceful Server (#5028)

* prepare for releasing v2.0.5 (#5032)

Co-authored-by: auual <ding@ibyte.me>
Co-authored-by: Leon Ding <deen.job@qq.com>
Co-authored-by: dada0z <zhang.guangda@qq.com>
Co-authored-by: kevinzeng <kevinzeng@zego.im>
Co-authored-by: Kevin Tsang <39397413+ktalg@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: 日暮颂歌1991 <448081525@qq.com>
Co-authored-by: Regan Yue <1131625869@qq.com>
Co-authored-by: alingse <alingse@foxmail.com>
2022-07-30 16:11:51 +08:00

143 lines
3.6 KiB
Go

package bean
import (
"fmt"
"reflect"
"strconv"
"strings"
)
// Mock have a mock object ,it must be pointer of struct
// the element in mock object can be slices, structures, basic data types, pointers and interface
func Mock(v interface{}) (err error) {
pv := reflect.ValueOf(v)
// the input must be pointer of struct
if pv.Kind() != reflect.Ptr || pv.IsNil() {
err = fmt.Errorf("not a pointer of struct")
return
}
err = mock(pv)
return
}
func mock(pv reflect.Value) (err error) {
pt := pv.Type()
for i := 0; i < pt.Elem().NumField(); i++ {
ptt := pt.Elem().Field(i)
pvv := pv.Elem().FieldByName(ptt.Name)
if !pvv.CanSet() {
continue
}
kt := ptt.Type.Kind()
tagValue := ptt.Tag.Get("mock")
switch kt {
case reflect.Map:
continue
case reflect.Interface:
if pvv.IsNil() { // when interface is nil,can not sure the type
continue
}
pvv.Set(reflect.New(pvv.Elem().Type().Elem()))
err = mock(pvv.Elem())
case reflect.Ptr:
err = mockPtr(pvv, ptt.Type.Elem())
case reflect.Struct:
err = mock(pvv.Addr())
case reflect.Array, reflect.Slice:
err = mockSlice(tagValue, pvv)
case reflect.String:
pvv.SetString(tagValue)
case reflect.Bool:
err = mockBool(tagValue, pvv)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
value, e := strconv.ParseInt(tagValue, 10, 64)
if e != nil || pvv.OverflowInt(value) {
err = fmt.Errorf("the value:%s is invalid", tagValue)
}
pvv.SetInt(value)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
value, e := strconv.ParseUint(tagValue, 10, 64)
if e != nil || pvv.OverflowUint(value) {
err = fmt.Errorf("the value:%s is invalid", tagValue)
}
pvv.SetUint(value)
case reflect.Float32, reflect.Float64:
value, e := strconv.ParseFloat(tagValue, pvv.Type().Bits())
if e != nil || pvv.OverflowFloat(value) {
err = fmt.Errorf("the value:%s is invalid", tagValue)
}
pvv.SetFloat(value)
default:
}
if err != nil {
return
}
}
return
}
// mock slice value
func mockSlice(tagValue string, pvv reflect.Value) (err error) {
if tagValue == "" {
return
}
sliceMetas := strings.Split(tagValue, ":")
if len(sliceMetas) != 2 || sliceMetas[0] != "length" {
err = fmt.Errorf("the value:%s is invalid", tagValue)
return
}
length, e := strconv.Atoi(sliceMetas[1])
if e != nil {
return e
}
sliceType := reflect.SliceOf(pvv.Type().Elem()) // get slice type
itemType := sliceType.Elem() // get the type of item in slice
value := reflect.MakeSlice(sliceType, 0, length)
newSliceValue := make([]reflect.Value, 0, length)
for k := 0; k < length; k++ {
itemValue := reflect.New(itemType).Elem()
// if item in slice is struct or pointer,must set zero value
switch itemType.Kind() {
case reflect.Struct:
err = mock(itemValue.Addr())
case reflect.Ptr:
if itemValue.IsNil() {
itemValue.Set(reflect.New(itemType.Elem()))
if e := mock(itemValue); e != nil {
return e
}
}
}
newSliceValue = append(newSliceValue, itemValue)
if err != nil {
return
}
}
value = reflect.Append(value, newSliceValue...)
pvv.Set(value)
return
}
// mock bool value
func mockBool(tagValue string, pvv reflect.Value) (err error) {
switch tagValue {
case "true":
pvv.SetBool(true)
case "false":
pvv.SetBool(false)
default:
err = fmt.Errorf("the value:%s is invalid", tagValue)
}
return
}
// mock pointer
func mockPtr(pvv reflect.Value, ptt reflect.Type) (err error) {
if pvv.IsNil() {
pvv.Set(reflect.New(ptt)) // must set nil value to zero value
}
err = mock(pvv)
return
}