mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-11-13 12:43:45 +08:00
发布v2.13.1版本,更新内容请查看:https://github.com/bufanyun/hotgo/blob/v2.0/docs/guide-zh-CN/start-update-log.md
This commit is contained in:
61
server/internal/library/dict/dict.go
Normal file
61
server/internal/library/dict/dict.go
Normal file
@@ -0,0 +1,61 @@
|
||||
// Package dict
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package dict
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"hash/fnv"
|
||||
"hotgo/internal/model"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const (
|
||||
BuiltinId int64 = -1 // 内置字典ID
|
||||
EnumsId int64 = -2 // 枚举字典ID
|
||||
FuncId int64 = -3 // 方法字典ID
|
||||
)
|
||||
|
||||
var NotExistKeyError = errors.New("not exist key")
|
||||
|
||||
// GetOptions 获取内置选项
|
||||
func GetOptions(ctx context.Context, key string) (opts []*model.Option, err error) {
|
||||
opts = GetEnumsOptions(key)
|
||||
if opts != nil {
|
||||
return
|
||||
}
|
||||
return GetFuncOptions(ctx, key)
|
||||
}
|
||||
|
||||
// GetOptionsById 通过类型ID获取内置选项
|
||||
func GetOptionsById(ctx context.Context, id int64) (opts []*model.Option, err error) {
|
||||
for _, v := range GetAllEnums() {
|
||||
if v.Id == id {
|
||||
return v.Opts, nil
|
||||
}
|
||||
}
|
||||
|
||||
for _, v := range GetAllFunc() {
|
||||
if v.Id == id {
|
||||
return LoadFuncOptions(ctx, v)
|
||||
}
|
||||
}
|
||||
|
||||
err = NotExistKeyError
|
||||
return
|
||||
}
|
||||
|
||||
// GenIdHash 生成字典id
|
||||
func GenIdHash(str string, t int64) int64 {
|
||||
prefix := 10000 * t
|
||||
h := fnv.New32a()
|
||||
h.Write([]byte("dict" + str))
|
||||
|
||||
idStr := fmt.Sprintf("%d%d", prefix, int64(h.Sum32()))
|
||||
id, _ := strconv.ParseInt(idStr, 10, 64)
|
||||
return id
|
||||
}
|
||||
Reference in New Issue
Block a user