mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-04 00:10:25 +08:00
refactor: interface{} -> any
feat: 新增外链菜单
This commit is contained in:
@@ -49,25 +49,25 @@ func NotEmpty(str string, msg string, params ...any) {
|
||||
}
|
||||
}
|
||||
|
||||
func NotNil(data interface{}, msg string, params ...any) {
|
||||
func NotNil(data any, msg string, params ...any) {
|
||||
if reflect.ValueOf(data).IsNil() {
|
||||
panic(NewBizErr(fmt.Sprintf(msg, params...)))
|
||||
}
|
||||
}
|
||||
|
||||
func NotBlank(data interface{}, msg string, params ...any) {
|
||||
func NotBlank(data any, msg string, params ...any) {
|
||||
if utils.IsBlank(data) {
|
||||
panic(NewBizErr(fmt.Sprintf(msg, params...)))
|
||||
}
|
||||
}
|
||||
|
||||
func IsEquals(data interface{}, data1 interface{}, msg string) {
|
||||
func IsEquals(data any, data1 any, msg string) {
|
||||
if data != data1 {
|
||||
panic(NewBizErr(msg))
|
||||
}
|
||||
}
|
||||
|
||||
func Nil(data interface{}, msg string) {
|
||||
func Nil(data any, msg string) {
|
||||
if !reflect.ValueOf(data).IsNil() {
|
||||
panic(NewBizErr(msg))
|
||||
}
|
||||
|
||||
8
server/pkg/cache/cache.go
vendored
8
server/pkg/cache/cache.go
vendored
@@ -2,17 +2,17 @@ package cache
|
||||
|
||||
type Cache interface {
|
||||
// 添加缓存,如果缓存则返回错误
|
||||
Add(k string, v interface{}) error
|
||||
Add(k string, v any) error
|
||||
|
||||
// 如果不存在则添加缓存值,否则直接返回
|
||||
AddIfAbsent(k string, v interface{})
|
||||
AddIfAbsent(k string, v any)
|
||||
|
||||
// 如果存在则直接返回,否则调用getValue回调函数获取值并添加该缓存值
|
||||
// @return 缓存值
|
||||
ComputeIfAbsent(k string, getValueFunc func(string) (interface{}, error)) (interface{}, error)
|
||||
ComputeIfAbsent(k string, getValueFunc func(string) (any, error)) (any, error)
|
||||
|
||||
// 获取缓存值,参数1为值,参数2->是否存在该缓存
|
||||
Get(k string) (interface{}, bool)
|
||||
Get(k string) (any, bool)
|
||||
|
||||
// 缓存数量
|
||||
Count() int
|
||||
|
||||
52
server/pkg/cache/timed_cache.go
vendored
52
server/pkg/cache/timed_cache.go
vendored
@@ -10,10 +10,10 @@ import (
|
||||
)
|
||||
|
||||
type Item struct {
|
||||
Value interface{} // 对象
|
||||
Expiration int64 // 缓存有效时间
|
||||
UseCount int64 // 使用次数
|
||||
AccessTime int64 // 访问时间
|
||||
Value any // 对象
|
||||
Expiration int64 // 缓存有效时间
|
||||
UseCount int64 // 使用次数
|
||||
AccessTime int64 // 访问时间
|
||||
}
|
||||
|
||||
// 是否过期
|
||||
@@ -26,7 +26,7 @@ func (item Item) Expired() bool {
|
||||
|
||||
// 是否过期
|
||||
// @return 值 and 是否过期
|
||||
func (item *Item) GetValue(updateAccessTime bool) (interface{}, bool) {
|
||||
func (item *Item) GetValue(updateAccessTime bool) (any, bool) {
|
||||
isExpired := item.Expired()
|
||||
// 更新最后访问时间,用于增加值的有效期
|
||||
if !isExpired && updateAccessTime {
|
||||
@@ -52,15 +52,15 @@ type TimedCache struct {
|
||||
type timedcache struct {
|
||||
defaultExpiration time.Duration
|
||||
updateAccessTime bool // 是否更新最后访问时间
|
||||
items map[interface{}]*Item
|
||||
items map[any]*Item
|
||||
mu sync.RWMutex
|
||||
onEvicted func(interface{}, interface{}) // 移除时回调函数
|
||||
onEvicted func(any, any) // 移除时回调函数
|
||||
janitor *janitor
|
||||
}
|
||||
|
||||
// Add an item to the cache only if an item doesn't already exist for the given
|
||||
// key, or if the existing item has expired. Returns an error otherwise.
|
||||
func (c *timedcache) Add(k interface{}, x interface{}, d time.Duration) error {
|
||||
func (c *timedcache) Add(k any, x any, d time.Duration) error {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
_, found := c.get(k)
|
||||
@@ -71,13 +71,13 @@ func (c *timedcache) Add(k interface{}, x interface{}, d time.Duration) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *timedcache) Put(k interface{}, x interface{}) {
|
||||
func (c *timedcache) Put(k any, x any) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
c.set(k, x, c.defaultExpiration)
|
||||
}
|
||||
|
||||
func (c *timedcache) AddIfAbsent(k interface{}, x interface{}) {
|
||||
func (c *timedcache) AddIfAbsent(k any, x any) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
_, found := c.get(k)
|
||||
@@ -87,7 +87,7 @@ func (c *timedcache) AddIfAbsent(k interface{}, x interface{}) {
|
||||
c.set(k, x, c.defaultExpiration)
|
||||
}
|
||||
|
||||
func (c *timedcache) ComputeIfAbsent(k interface{}, getValueFunc func(interface{}) (interface{}, error)) (interface{}, error) {
|
||||
func (c *timedcache) ComputeIfAbsent(k any, getValueFunc func(any) (any, error)) (any, error) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
value, found := c.get(k)
|
||||
@@ -103,7 +103,7 @@ func (c *timedcache) ComputeIfAbsent(k interface{}, getValueFunc func(interface{
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func (c *timedcache) set(k interface{}, x interface{}, d time.Duration) {
|
||||
func (c *timedcache) set(k any, x any, d time.Duration) {
|
||||
var e int64
|
||||
if d == DefaultExpiration {
|
||||
d = c.defaultExpiration
|
||||
@@ -120,13 +120,13 @@ func (c *timedcache) set(k interface{}, x interface{}, d time.Duration) {
|
||||
|
||||
// Get an item from the cache. Returns the item or nil, and a bool indicating
|
||||
// whether the key was found.
|
||||
func (c *timedcache) Get(k interface{}) (interface{}, bool) {
|
||||
func (c *timedcache) Get(k any) (any, bool) {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
return c.get(k)
|
||||
}
|
||||
|
||||
func (c *timedcache) get(k interface{}) (interface{}, bool) {
|
||||
func (c *timedcache) get(k any) (any, bool) {
|
||||
item, found := c.items[k]
|
||||
if !found {
|
||||
return nil, false
|
||||
@@ -145,7 +145,7 @@ func (c *timedcache) get(k interface{}) (interface{}, bool) {
|
||||
// item's value is not an integer, if it was not found, or if it is not
|
||||
// possible to increment it by n. To retrieve the incremented value, use one
|
||||
// of the specialized methods, e.g. IncrementInt64.
|
||||
func (c *timedcache) Increment(k interface{}, n int64) error {
|
||||
func (c *timedcache) Increment(k any, n int64) error {
|
||||
c.mu.Lock()
|
||||
v, found := c.items[k]
|
||||
if !found || v.Expired() {
|
||||
@@ -198,10 +198,10 @@ func (c *timedcache) Count() int {
|
||||
}
|
||||
|
||||
// Copies all unexpired items in the cache into a new map and returns it.
|
||||
func (c *timedcache) Items() map[interface{}]*Item {
|
||||
func (c *timedcache) Items() map[any]*Item {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
m := make(map[interface{}]*Item, len(c.items))
|
||||
m := make(map[any]*Item, len(c.items))
|
||||
now := time.Now().UnixNano()
|
||||
for k, v := range c.items {
|
||||
// "Inlining" of Expired
|
||||
@@ -216,7 +216,7 @@ func (c *timedcache) Items() map[interface{}]*Item {
|
||||
}
|
||||
|
||||
// 删除指定key的数据
|
||||
func (c *timedcache) Delete(k interface{}) {
|
||||
func (c *timedcache) Delete(k any) {
|
||||
c.mu.Lock()
|
||||
v, evicted := c.delete(k)
|
||||
c.mu.Unlock()
|
||||
@@ -225,7 +225,7 @@ func (c *timedcache) Delete(k interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *timedcache) delete(k interface{}) (interface{}, bool) {
|
||||
func (c *timedcache) delete(k any) (any, bool) {
|
||||
// 如果有移除回调函数,则返回值及是否有删除回调函数用于进行回调处理
|
||||
if c.onEvicted != nil {
|
||||
if v, found := c.items[k]; found {
|
||||
@@ -238,8 +238,8 @@ func (c *timedcache) delete(k interface{}) (interface{}, bool) {
|
||||
}
|
||||
|
||||
type keyAndValue struct {
|
||||
key interface{}
|
||||
value interface{}
|
||||
key any
|
||||
value any
|
||||
}
|
||||
|
||||
// Delete all expired items from the cache.
|
||||
@@ -265,7 +265,7 @@ func (c *timedcache) DeleteExpired() {
|
||||
// 清空所有缓存
|
||||
func (c *timedcache) Clear() {
|
||||
c.mu.Lock()
|
||||
c.items = map[interface{}]*Item{}
|
||||
c.items = map[any]*Item{}
|
||||
c.mu.Unlock()
|
||||
}
|
||||
|
||||
@@ -378,7 +378,7 @@ func runJanitor(c *timedcache, ci time.Duration) {
|
||||
go j.Run(c)
|
||||
}
|
||||
|
||||
func newCache(de time.Duration, m map[interface{}]*Item) *timedcache {
|
||||
func newCache(de time.Duration, m map[any]*Item) *timedcache {
|
||||
if de == 0 {
|
||||
de = -1
|
||||
}
|
||||
@@ -389,7 +389,7 @@ func newCache(de time.Duration, m map[interface{}]*Item) *timedcache {
|
||||
return c
|
||||
}
|
||||
|
||||
func newCacheWithJanitor(de time.Duration, ci time.Duration, m map[interface{}]*Item) *TimedCache {
|
||||
func newCacheWithJanitor(de time.Duration, ci time.Duration, m map[any]*Item) *TimedCache {
|
||||
c := newCache(de, m)
|
||||
// This trick ensures that the janitor goroutine (which--granted it
|
||||
// was enabled--is running DeleteExpired on c forever) does not keep
|
||||
@@ -410,12 +410,12 @@ func newCacheWithJanitor(de time.Duration, ci time.Duration, m map[interface{}]*
|
||||
// manually. If the cleanup interval is less than one, expired items are not
|
||||
// deleted from the cache before calling c.DeleteExpired().
|
||||
func NewTimedCache(defaultExpiration, cleanupInterval time.Duration) *TimedCache {
|
||||
items := make(map[interface{}]*Item)
|
||||
items := make(map[any]*Item)
|
||||
return newCacheWithJanitor(defaultExpiration, cleanupInterval, items)
|
||||
}
|
||||
|
||||
// 调用删除函数时,会回调该剔除函数
|
||||
func (c *TimedCache) OnEvicted(f func(interface{}, interface{})) *TimedCache {
|
||||
func (c *TimedCache) OnEvicted(f func(any, any)) *TimedCache {
|
||||
c.mu.Lock()
|
||||
c.onEvicted = f
|
||||
c.mu.Unlock()
|
||||
|
||||
@@ -13,14 +13,14 @@ import (
|
||||
)
|
||||
|
||||
// 绑定并校验请求结构体参数
|
||||
func BindJsonAndValid(g *gin.Context, data interface{}) {
|
||||
func BindJsonAndValid(g *gin.Context, data any) {
|
||||
if err := g.ShouldBindJSON(data); err != nil {
|
||||
panic(biz.NewBizErr(err.Error()))
|
||||
}
|
||||
}
|
||||
|
||||
// 绑定查询字符串到
|
||||
func BindQuery(g *gin.Context, data interface{}) {
|
||||
func BindQuery(g *gin.Context, data any) {
|
||||
if err := g.ShouldBindQuery(data); err != nil {
|
||||
panic(biz.NewBizErr(err.Error()))
|
||||
}
|
||||
@@ -65,12 +65,12 @@ func Download(g *gin.Context, reader io.Reader, filename string) {
|
||||
}
|
||||
|
||||
// 返回统一成功结果
|
||||
func SuccessRes(g *gin.Context, data interface{}) {
|
||||
func SuccessRes(g *gin.Context, data any) {
|
||||
g.JSON(http.StatusOK, model.Success(data))
|
||||
}
|
||||
|
||||
// 返回失败结果集
|
||||
func ErrorRes(g *gin.Context, err interface{}) {
|
||||
func ErrorRes(g *gin.Context, err any) {
|
||||
switch t := err.(type) {
|
||||
case biz.BizError:
|
||||
g.JSON(http.StatusOK, model.Error(t))
|
||||
|
||||
@@ -86,7 +86,7 @@ func (r *RequestWrapper) PostJson(body string) *ResponseWrapper {
|
||||
return request(r)
|
||||
}
|
||||
|
||||
func (r *RequestWrapper) PostObj(body interface{}) *ResponseWrapper {
|
||||
func (r *RequestWrapper) PostObj(body any) *ResponseWrapper {
|
||||
marshal, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return createRequestError(errors.New("解析json obj错误"))
|
||||
@@ -158,7 +158,7 @@ func (r *ResponseWrapper) IsSuccess() bool {
|
||||
return r.StatusCode == 200
|
||||
}
|
||||
|
||||
func (r *ResponseWrapper) BodyToObj(objPtr interface{}) error {
|
||||
func (r *ResponseWrapper) BodyToObj(objPtr any) error {
|
||||
_ = json.Unmarshal(r.Body, &objPtr)
|
||||
return r.getError()
|
||||
}
|
||||
@@ -167,8 +167,8 @@ func (r *ResponseWrapper) BodyToString() (string, error) {
|
||||
return string(r.Body), r.getError()
|
||||
}
|
||||
|
||||
func (r *ResponseWrapper) BodyToMap() (map[string]interface{}, error) {
|
||||
var res map[string]interface{}
|
||||
func (r *ResponseWrapper) BodyToMap() (map[string]any, error) {
|
||||
var res map[string]any
|
||||
err := json.Unmarshal(r.Body, &res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -67,13 +67,13 @@ func Tx(funcs ...func(db *gorm.DB) error) (err error) {
|
||||
//
|
||||
// 若error不为nil则为不存在该记录
|
||||
// @param model 数据库映射实体模型
|
||||
func GetById(model interface{}, id uint64, cols ...string) error {
|
||||
func GetById(model any, id uint64, cols ...string) error {
|
||||
return global.Db.Select(cols).Where("id = ?", id).First(model).Error
|
||||
}
|
||||
|
||||
// 根据map条件查询列表,map中的值如果为数组,则使用in查询
|
||||
// @param model 数据库映射实体模型
|
||||
func GetByIdIn(model interface{}, list interface{}, ids []uint64, orderBy ...string) {
|
||||
func GetByIdIn(model any, list any, ids []uint64, orderBy ...string) {
|
||||
var orderByStr string
|
||||
if orderBy == nil {
|
||||
orderByStr = "id desc"
|
||||
@@ -84,7 +84,7 @@ func GetByIdIn(model interface{}, list interface{}, ids []uint64, orderBy ...str
|
||||
}
|
||||
|
||||
// 根据map指定条件查询列表
|
||||
func SelectByMap(model interface{}, list interface{}, where map[string]interface{}, orderBy ...string) {
|
||||
func SelectByMap(model any, list any, where map[string]any, orderBy ...string) {
|
||||
var orderByStr string
|
||||
if orderBy == nil {
|
||||
orderByStr = "id desc"
|
||||
@@ -95,7 +95,7 @@ func SelectByMap(model interface{}, list interface{}, where map[string]interface
|
||||
}
|
||||
|
||||
// 根据model指定条件统计数量
|
||||
func CountBy(model interface{}) int64 {
|
||||
func CountBy(model any) int64 {
|
||||
var count int64
|
||||
global.Db.Model(model).Where(model).Count(&count)
|
||||
return count
|
||||
@@ -104,7 +104,7 @@ func CountBy(model interface{}) int64 {
|
||||
// 根据map为条件统计数量,map中的值如果为数组,则使用in查询
|
||||
// @param model 数据库映射实体模型
|
||||
// @param where 条件map
|
||||
func CountByMap(model interface{}, where map[string]interface{}) int64 {
|
||||
func CountByMap(model any, where map[string]any) int64 {
|
||||
var count int64
|
||||
global.Db.Model(model).Where(where).Count(&count)
|
||||
return count
|
||||
@@ -119,32 +119,32 @@ func CountBySql(sql string) int64 {
|
||||
|
||||
// 根据id更新model,更新字段为model中不为空的值,即int类型不为0,ptr类型不为nil这类字段值
|
||||
// @param model 数据库映射实体模型
|
||||
func UpdateById(model interface{}) error {
|
||||
func UpdateById(model any) error {
|
||||
return global.Db.Model(model).Updates(model).Error
|
||||
}
|
||||
|
||||
// 根据id删除model
|
||||
// @param model 数据库映射实体模型
|
||||
func DeleteById(model interface{}, id uint64) error {
|
||||
func DeleteById(model any, id uint64) error {
|
||||
return global.Db.Delete(model, "id = ?", id).Error
|
||||
}
|
||||
|
||||
// 根据条件删除
|
||||
// @param model 数据库映射实体模型
|
||||
func DeleteByCondition(model interface{}) error {
|
||||
func DeleteByCondition(model any) error {
|
||||
return global.Db.Where(model).Delete(model).Error
|
||||
}
|
||||
|
||||
// 插入model
|
||||
// @param model 数据库映射实体模型
|
||||
func Insert(model interface{}) error {
|
||||
func Insert(model any) error {
|
||||
return global.Db.Create(model).Error
|
||||
}
|
||||
|
||||
// 获取满足model中不为空的字段值条件的所有数据.
|
||||
//
|
||||
// @param list为数组类型 如 var users *[]User,可指定为非model结构体,即只包含需要返回的字段结构体
|
||||
func ListBy(model interface{}, list interface{}, cols ...string) {
|
||||
func ListBy(model any, list any, cols ...string) {
|
||||
global.Db.Model(model).Select(cols).Where(model).Order("id desc").Find(list)
|
||||
}
|
||||
|
||||
@@ -152,7 +152,7 @@ func ListBy(model interface{}, list interface{}, cols ...string) {
|
||||
//
|
||||
// @param list为数组类型 如 var users *[]User,可指定为非model结构体
|
||||
// @param model 数据库映射实体模型
|
||||
func ListByOrder(model interface{}, list interface{}, order ...string) {
|
||||
func ListByOrder(model any, list any, order ...string) {
|
||||
var orderByStr string
|
||||
if order == nil {
|
||||
orderByStr = "id desc"
|
||||
@@ -166,7 +166,7 @@ func ListByOrder(model interface{}, list interface{}, order ...string) {
|
||||
//
|
||||
// 若 error不为nil,则为不存在该记录
|
||||
// @param model 数据库映射实体模型
|
||||
func GetBy(model interface{}, cols ...string) error {
|
||||
func GetBy(model any, cols ...string) error {
|
||||
return global.Db.Select(cols).Where(model).First(model).Error
|
||||
}
|
||||
|
||||
@@ -175,12 +175,12 @@ func GetBy(model interface{}, cols ...string) error {
|
||||
// @param toModel 需要查询的字段
|
||||
//
|
||||
// 若 error不为nil,则为不存在该记录
|
||||
func GetByConditionTo(conditionModel interface{}, toModel interface{}) error {
|
||||
func GetByConditionTo(conditionModel any, toModel any) error {
|
||||
return global.Db.Model(conditionModel).Where(conditionModel).First(toModel).Error
|
||||
}
|
||||
|
||||
// 获取分页结果
|
||||
func GetPage(pageParam *PageParam, model interface{}, conditionModel interface{}, toModels interface{}, orderBy ...string) *PageResult {
|
||||
func GetPage(pageParam *PageParam, model any, conditionModel any, toModels any, orderBy ...string) *PageResult {
|
||||
var count int64
|
||||
err := global.Db.Model(model).Where(conditionModel).Count(&count).Error
|
||||
biz.ErrIsNilAppendErr(err, " 查询错误:%s")
|
||||
@@ -202,7 +202,7 @@ func GetPage(pageParam *PageParam, model interface{}, conditionModel interface{}
|
||||
}
|
||||
|
||||
// 根据sql获取分页对象
|
||||
func GetPageBySql(sql string, param *PageParam, toModel interface{}, args ...interface{}) *PageResult {
|
||||
func GetPageBySql(sql string, param *PageParam, toModel any, args ...any) *PageResult {
|
||||
db := global.Db
|
||||
selectIndex := strings.Index(sql, "SELECT ") + 7
|
||||
fromIndex := strings.Index(sql, " FROM")
|
||||
@@ -222,12 +222,12 @@ func GetPageBySql(sql string, param *PageParam, toModel interface{}, args ...int
|
||||
return &PageResult{Total: int64(count), List: toModel}
|
||||
}
|
||||
|
||||
func GetListBySql(sql string, params ...interface{}) []map[string]interface{} {
|
||||
var maps []map[string]interface{}
|
||||
func GetListBySql(sql string, params ...any) []map[string]any {
|
||||
var maps []map[string]any
|
||||
global.Db.Raw(sql, params...).Scan(&maps)
|
||||
return maps
|
||||
}
|
||||
|
||||
func GetListBySql2Model(sql string, toEntity interface{}, params ...interface{}) error {
|
||||
func GetListBySql2Model(sql string, toEntity any, params ...any) error {
|
||||
return global.Db.Raw(sql, params...).Find(toEntity).Error
|
||||
}
|
||||
|
||||
@@ -13,9 +13,9 @@ const (
|
||||
|
||||
// 统一返回结果结构体
|
||||
type Result struct {
|
||||
Code int16 `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Data interface{} `json:"data"`
|
||||
Code int16 `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Data any `json:"data"`
|
||||
}
|
||||
|
||||
// 将Result转为json字符串
|
||||
@@ -34,7 +34,7 @@ func (r *Result) IsSuccess() bool {
|
||||
|
||||
// 返回成功状态的Result
|
||||
// @param data 成功附带的数据消息
|
||||
func Success(data interface{}) *Result {
|
||||
func Success(data any) *Result {
|
||||
return &Result{Code: SuccessCode, Msg: SuccessMsg, Data: data}
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ func Del(key string) {
|
||||
cli.Del(context.TODO(), key)
|
||||
}
|
||||
|
||||
func HSet(key string, field string, val interface{}) {
|
||||
func HSet(key string, field string, val any) {
|
||||
cli.HSet(context.TODO(), key, field, val)
|
||||
}
|
||||
|
||||
|
||||
@@ -81,7 +81,7 @@ func getLogMsg(rc *Ctx) string {
|
||||
return msg
|
||||
}
|
||||
|
||||
func getErrMsg(rc *Ctx, err interface{}) string {
|
||||
func getErrMsg(rc *Ctx, err any) string {
|
||||
msg := rc.LogInfo.Description
|
||||
if !utils.IsBlank(rc.ReqParam) {
|
||||
msg = msg + fmt.Sprintf("\n--> %s", utils.ToString(rc.ReqParam))
|
||||
|
||||
@@ -106,7 +106,7 @@ func UseAfterHandlerInterceptor(b HandlerInterceptorFunc) {
|
||||
}
|
||||
|
||||
// 应用指定处理器拦截器,如果有一个错误则直接返回错误
|
||||
func ApplyHandlerInterceptor(his HandlerInterceptors, rc *Ctx) interface{} {
|
||||
func ApplyHandlerInterceptor(his HandlerInterceptors, rc *Ctx) any {
|
||||
for _, handler := range his {
|
||||
if err := handler(rc); err != nil {
|
||||
return err
|
||||
|
||||
@@ -50,7 +50,7 @@ func ParseToken(tokenStr string) (*model.LoginAccount, error) {
|
||||
return nil, errors.New("token error")
|
||||
}
|
||||
// Parse token
|
||||
token, err := jwt.Parse(tokenStr, func(token *jwt.Token) (interface{}, error) {
|
||||
token, err := jwt.Parse(tokenStr, func(token *jwt.Token) (any, error) {
|
||||
return []byte(JwtKey), nil
|
||||
})
|
||||
if err != nil || token == nil {
|
||||
|
||||
@@ -6,8 +6,8 @@ import (
|
||||
|
||||
// 数组比较
|
||||
// 依次返回,新增值,删除值,以及不变值
|
||||
func ArrayCompare(newArr []interface{}, oldArr []interface{}, compareFun func(interface{}, interface{}) bool) ([]interface{}, []interface{}, []interface{}) {
|
||||
var unmodifierValue []interface{}
|
||||
func ArrayCompare(newArr []any, oldArr []any, compareFun func(any, any) bool) ([]any, []any, []any) {
|
||||
var unmodifierValue []any
|
||||
ni, oi := 0, 0
|
||||
for {
|
||||
if ni >= len(newArr) {
|
||||
|
||||
@@ -6,9 +6,9 @@ import (
|
||||
)
|
||||
|
||||
func TestArrayCompare(t *testing.T) {
|
||||
newArr := []interface{}{1, 2, 3, 5}
|
||||
oldArr := []interface{}{3, 6}
|
||||
add, del, unmodifier := ArrayCompare(newArr, oldArr, func(i1, i2 interface{}) bool {
|
||||
newArr := []any{1, 2, 3, 5}
|
||||
oldArr := []any{3, 6}
|
||||
add, del, unmodifier := ArrayCompare(newArr, oldArr, func(i1, i2 any) bool {
|
||||
return i1.(int) == i2.(int)
|
||||
})
|
||||
fmt.Println(add...)
|
||||
|
||||
@@ -5,8 +5,8 @@ import (
|
||||
"mayfly-go/pkg/global"
|
||||
)
|
||||
|
||||
func Json2Map(jsonStr string) map[string]interface{} {
|
||||
var res map[string]interface{}
|
||||
func Json2Map(jsonStr string) map[string]any {
|
||||
var res map[string]any
|
||||
if jsonStr == "" {
|
||||
return res
|
||||
}
|
||||
|
||||
@@ -5,11 +5,11 @@ import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func GetString4Map(m map[string]interface{}, key string) string {
|
||||
func GetString4Map(m map[string]any, key string) string {
|
||||
return m[key].(string)
|
||||
}
|
||||
|
||||
func GetInt4Map(m map[string]interface{}, key string) int {
|
||||
func GetInt4Map(m map[string]any, key string) int {
|
||||
i := m[key]
|
||||
iKind := reflect.TypeOf(i).Kind()
|
||||
if iKind == reflect.Int {
|
||||
@@ -24,21 +24,21 @@ func GetInt4Map(m map[string]interface{}, key string) int {
|
||||
|
||||
// map构造器
|
||||
type mapBuilder struct {
|
||||
m map[string]interface{}
|
||||
m map[string]any
|
||||
}
|
||||
|
||||
func MapBuilder(key string, value interface{}) *mapBuilder {
|
||||
func MapBuilder(key string, value any) *mapBuilder {
|
||||
mb := new(mapBuilder)
|
||||
mb.m = make(map[string]interface{}, 4)
|
||||
mb.m = make(map[string]any, 4)
|
||||
mb.m[key] = value
|
||||
return mb
|
||||
}
|
||||
|
||||
func (mb *mapBuilder) Put(key string, value interface{}) *mapBuilder {
|
||||
func (mb *mapBuilder) Put(key string, value any) *mapBuilder {
|
||||
mb.m[key] = value
|
||||
return mb
|
||||
}
|
||||
|
||||
func (mb *mapBuilder) ToMap() map[string]interface{} {
|
||||
func (mb *mapBuilder) ToMap() map[string]any {
|
||||
return mb.m
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ func UnicodeIndex(str, substr string) int {
|
||||
}
|
||||
|
||||
// 字符串模板解析
|
||||
func TemplateResolve(temp string, data interface{}) string {
|
||||
func TemplateResolve(temp string, data any) string {
|
||||
t, _ := template.New("string-temp").Parse(temp)
|
||||
var tmplBytes bytes.Buffer
|
||||
|
||||
@@ -93,7 +93,7 @@ func TemplateResolve(temp string, data interface{}) string {
|
||||
return tmplBytes.String()
|
||||
}
|
||||
|
||||
func ReverStrTemplate(temp, str string, res map[string]interface{}) {
|
||||
func ReverStrTemplate(temp, str string, res map[string]any) {
|
||||
index := UnicodeIndex(temp, "{")
|
||||
ei := UnicodeIndex(temp, "}") + 1
|
||||
next := StrTrim(temp[ei:])
|
||||
@@ -118,7 +118,7 @@ func ReverStrTemplate(temp, str string, res map[string]interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
func ToString(value interface{}) string {
|
||||
func ToString(value any) string {
|
||||
// interface 转 string
|
||||
var key string
|
||||
if value == nil {
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
// Copy copy things,引用至copier
|
||||
func Copy(toValue interface{}, fromValue interface{}) (err error) {
|
||||
func Copy(toValue any, fromValue any) (err error) {
|
||||
var (
|
||||
isSlice bool
|
||||
amount = 1
|
||||
@@ -133,7 +133,7 @@ func Copy(toValue interface{}, fromValue interface{}) (err error) {
|
||||
}
|
||||
|
||||
// 对结构体的每个字段以及字段值执行doWith回调函数, 包括匿名属性的字段
|
||||
func DoWithFields(str interface{}, doWith func(fType reflect.StructField, fValue reflect.Value) error) error {
|
||||
func DoWithFields(str any, doWith func(fType reflect.StructField, fValue reflect.Value) error) error {
|
||||
t := IndirectType(reflect.TypeOf(str))
|
||||
if t.Kind() != reflect.Struct {
|
||||
return errors.New("非结构体")
|
||||
@@ -217,7 +217,7 @@ func set(to, from reflect.Value) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func Map2Struct(m map[string]interface{}, s interface{}) error {
|
||||
func Map2Struct(m map[string]any, s any) error {
|
||||
toValue := Indirect(reflect.ValueOf(s))
|
||||
if !toValue.CanAddr() {
|
||||
return errors.New("to value is unaddressable")
|
||||
@@ -275,7 +275,7 @@ func Map2Struct(m map[string]interface{}, s interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func Maps2Structs(maps []map[string]interface{}, structs interface{}) error {
|
||||
func Maps2Structs(maps []map[string]any, structs any) error {
|
||||
structsV := reflect.Indirect(reflect.ValueOf(structs))
|
||||
valType := structsV.Type()
|
||||
valElemType := valType.Elem()
|
||||
@@ -314,8 +314,8 @@ func getFiledValueByPath(path string, value reflect.Value) reflect.Value {
|
||||
return value
|
||||
}
|
||||
|
||||
func getInnerStructMaps(m map[string]interface{}) map[string]map[string]interface{} {
|
||||
key2map := make(map[string]map[string]interface{})
|
||||
func getInnerStructMaps(m map[string]any) map[string]map[string]any {
|
||||
key2map := make(map[string]map[string]any)
|
||||
for k, v := range m {
|
||||
if !strings.Contains(k, ".") {
|
||||
continue
|
||||
@@ -324,7 +324,7 @@ func getInnerStructMaps(m map[string]interface{}) map[string]map[string]interfac
|
||||
prefix := k[0:lastIndex]
|
||||
m2 := key2map[prefix]
|
||||
if m2 == nil {
|
||||
key2map[prefix] = map[string]interface{}{k[lastIndex+1:]: v}
|
||||
key2map[prefix] = map[string]any{k[lastIndex+1:]: v}
|
||||
} else {
|
||||
m2[k[lastIndex+1:]] = v
|
||||
}
|
||||
@@ -335,7 +335,7 @@ func getInnerStructMaps(m map[string]interface{}) map[string]map[string]interfac
|
||||
|
||||
// decode等方法摘抄自mapstructure库
|
||||
|
||||
func decode(name string, input interface{}, outVal reflect.Value) error {
|
||||
func decode(name string, input any, outVal reflect.Value) error {
|
||||
var inputVal reflect.Value
|
||||
if input != nil {
|
||||
inputVal = reflect.ValueOf(input)
|
||||
@@ -374,7 +374,7 @@ func decode(name string, input interface{}, outVal reflect.Value) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func decodeInt(name string, data interface{}, val reflect.Value) error {
|
||||
func decodeInt(name string, data any, val reflect.Value) error {
|
||||
dataVal := reflect.Indirect(reflect.ValueOf(data))
|
||||
dataKind := getKind(dataVal)
|
||||
dataType := dataVal.Type()
|
||||
@@ -416,7 +416,7 @@ func decodeInt(name string, data interface{}, val reflect.Value) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func decodeUint(name string, data interface{}, val reflect.Value) error {
|
||||
func decodeUint(name string, data any, val reflect.Value) error {
|
||||
dataVal := reflect.Indirect(reflect.ValueOf(data))
|
||||
dataKind := getKind(dataVal)
|
||||
dataType := dataVal.Type()
|
||||
@@ -472,7 +472,7 @@ func decodeUint(name string, data interface{}, val reflect.Value) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func decodeFloat(name string, data interface{}, val reflect.Value) error {
|
||||
func decodeFloat(name string, data any, val reflect.Value) error {
|
||||
dataVal := reflect.Indirect(reflect.ValueOf(data))
|
||||
dataKind := getKind(dataVal)
|
||||
dataType := dataVal.Type()
|
||||
@@ -514,7 +514,7 @@ func decodeFloat(name string, data interface{}, val reflect.Value) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func decodeString(name string, data interface{}, val reflect.Value) error {
|
||||
func decodeString(name string, data any, val reflect.Value) error {
|
||||
dataVal := reflect.Indirect(reflect.ValueOf(data))
|
||||
dataKind := getKind(dataVal)
|
||||
|
||||
@@ -566,7 +566,7 @@ func decodeString(name string, data interface{}, val reflect.Value) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func decodePtr(name string, data interface{}, val reflect.Value) (bool, error) {
|
||||
func decodePtr(name string, data any, val reflect.Value) (bool, error) {
|
||||
// If the input data is nil, then we want to just set the output
|
||||
// pointer to be nil as well.
|
||||
isNil := data == nil
|
||||
|
||||
@@ -55,7 +55,7 @@ func TestGetFieldNames(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMaps2Structs(t *testing.T) {
|
||||
mapInstance := make(map[string]interface{})
|
||||
mapInstance := make(map[string]any)
|
||||
mapInstance["Username"] = "liang637210"
|
||||
mapInstance["Id"] = 28
|
||||
mapInstance["CreateTime"] = time.Now()
|
||||
@@ -66,7 +66,7 @@ func TestMaps2Structs(t *testing.T) {
|
||||
mapInstance["Inner.Dest.Username"] = "inner dest uername"
|
||||
mapInstance["Inner.Dest.Inner.Desc"] = "inner dest inner desc"
|
||||
|
||||
mapInstance2 := make(map[string]interface{})
|
||||
mapInstance2 := make(map[string]any)
|
||||
mapInstance2["Username"] = "liang6372102"
|
||||
mapInstance2["Id"] = 282
|
||||
mapInstance2["CreateTime"] = time.Now()
|
||||
@@ -77,7 +77,7 @@ func TestMaps2Structs(t *testing.T) {
|
||||
mapInstance2["Inner.Dest.Username"] = "inner dest uername2"
|
||||
mapInstance2["Inner.Dest.Inner.Desc"] = "inner dest inner desc2"
|
||||
|
||||
maps := make([]map[string]interface{}, 2)
|
||||
maps := make([]map[string]any, 2)
|
||||
maps[0] = mapInstance
|
||||
maps[1] = mapInstance2
|
||||
res := new([]Src)
|
||||
@@ -88,7 +88,7 @@ func TestMaps2Structs(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMap2Struct(t *testing.T) {
|
||||
mapInstance := make(map[string]interface{})
|
||||
mapInstance := make(map[string]any)
|
||||
mapInstance["Username"] = "liang637210"
|
||||
mapInstance["Id"] = 12
|
||||
mapInstance["CreateTime"] = time.Now()
|
||||
@@ -99,7 +99,7 @@ func TestMap2Struct(t *testing.T) {
|
||||
mapInstance["Inner.Dest.Username"] = "inner dest uername"
|
||||
mapInstance["Inner.Dest.Inner.Desc"] = "inner dest inner desc"
|
||||
|
||||
//innerMap := make(map[string]interface{})
|
||||
//innerMap := make(map[string]any)
|
||||
//innerMap["Name"] = "Innername"
|
||||
|
||||
//a := new(Src)
|
||||
@@ -149,8 +149,8 @@ func TestMap2Struct(t *testing.T) {
|
||||
fmt.Printf("map2struct后得到的 struct 内容为:%v", s)
|
||||
}
|
||||
|
||||
func getPrefixKeyMap(m map[string]interface{}) map[string]map[string]interface{} {
|
||||
key2map := make(map[string]map[string]interface{})
|
||||
func getPrefixKeyMap(m map[string]any) map[string]map[string]any {
|
||||
key2map := make(map[string]map[string]any)
|
||||
for k, v := range m {
|
||||
if !strings.Contains(k, ".") {
|
||||
continue
|
||||
@@ -159,7 +159,7 @@ func getPrefixKeyMap(m map[string]interface{}) map[string]map[string]interface{}
|
||||
prefix := k[0:lastIndex]
|
||||
m2 := key2map[prefix]
|
||||
if m2 == nil {
|
||||
key2map[prefix] = map[string]interface{}{k[lastIndex+1:]: v}
|
||||
key2map[prefix] = map[string]any{k[lastIndex+1:]: v}
|
||||
} else {
|
||||
m2[k[lastIndex+1:]] = v
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"text/template"
|
||||
)
|
||||
|
||||
func parse(t *template.Template, vars interface{}) string {
|
||||
func parse(t *template.Template, vars any) string {
|
||||
var tmplBytes bytes.Buffer
|
||||
|
||||
err := t.Execute(&tmplBytes, vars)
|
||||
@@ -18,7 +18,7 @@ func parse(t *template.Template, vars interface{}) string {
|
||||
// 模板字符串解析
|
||||
// @param str 模板字符串
|
||||
// @param vars 参数变量
|
||||
func TemplateParse(str string, vars interface{}) string {
|
||||
func TemplateParse(str string, vars any) string {
|
||||
tmpl, err := template.New("tmpl").Parse(str)
|
||||
|
||||
if err != nil {
|
||||
|
||||
@@ -9,7 +9,7 @@ type INode interface {
|
||||
// IsRoot 判断当前节点是否是顶层根节点
|
||||
IsRoot() bool
|
||||
|
||||
SetChildren(childern interface{})
|
||||
SetChildren(childern any)
|
||||
}
|
||||
|
||||
type INodes []INode
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
// 从指定路径加载yaml文件
|
||||
func LoadYml(path string, out interface{}) error {
|
||||
func LoadYml(path string, out any) error {
|
||||
yamlFileBytes, readErr := os.ReadFile(path)
|
||||
if readErr != nil {
|
||||
return readErr
|
||||
@@ -21,7 +21,7 @@ func LoadYml(path string, out interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func LoadYmlByString(yamlStr string, out interface{}) error {
|
||||
func LoadYmlByString(yamlStr string, out any) error {
|
||||
// yaml解析
|
||||
return yaml.Unmarshal([]byte(yamlStr), out)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user