1 Star 0 Fork 1

flyiot/flylibs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
FlySettings.go 2.85 KB
一键复制 编辑 原始数据 按行查看 历史
flyrainning 提交于 2023-07-06 15:15 . u
package FlySettings
import (
"encoding/base64"
"errors"
"strings"
"gitee.com/flyiot/flylibs/FlyDatabase"
"gitee.com/flyiot/flylibs/FlyJSON"
jsoniter "github.com/json-iterator/go"
)
type FlySettings struct {
FlyDB FlyDatabase.FlyDatabaseInterface
Ready bool
API jsoniter.API
}
func NewFlySettings() *FlySettings {
s := FlySettings{}
return &s
}
// 获取串口配置名称
func (s *FlySettings) Open(FlyDB FlyDatabase.FlyDatabaseInterface) error {
s.Ready = false
s.API = FlyJSON.MinAPI()
s.FlyDB = FlyDB
_, err := s.FlyDB.GetDB().DB()
if err != nil {
return err
}
err = s.FlyDB.AddTable(&FlySettingsTable{})
if err != nil {
return err
}
s.Ready = true
return nil
}
func (s *FlySettings) Set(key string, value any) error {
if !s.Ready {
return errors.New("settings not ready")
}
b, err := s.API.Marshal(value)
if err != nil {
return err
}
var row FlySettingsTable
DB := s.FlyDB.GetDB()
DB.FirstOrCreate(&row, FlySettingsTable{Key: key})
row.Value = string(b)
res := DB.Save(row)
return res.Error
}
func (s *FlySettings) GetObj(key string, obj any) error {
val := s.Get(key)
err := val.LastError()
if err != nil {
return err
}
val.ToVal(obj)
return val.LastError()
}
func (s *FlySettings) Get(key string) jsoniter.Any {
if !s.Ready {
return s.API.Get([]byte(""))
}
str := ""
var row FlySettingsTable
DB := s.FlyDB.GetDB()
DB.First(&row, "key = ?", key)
if len(row.Value) > 0 {
str = row.Value
}
return s.API.Get([]byte(str))
}
func (s *FlySettings) GetString(key string, defaults string) string {
any := s.Get(key)
v := any.ToString()
if v == "" {
return defaults
}
if any.LastError() == nil {
return v
}
return defaults
}
func (s *FlySettings) GetInt(key string, defaults int) int {
any := s.Get(key)
v := any.ToInt()
if any.LastError() == nil {
return v
}
return defaults
}
func (s *FlySettings) GetInt64(key string, defaults int64) int64 {
any := s.Get(key)
v := any.ToInt64()
if any.LastError() == nil {
return v
}
return defaults
}
func (s *FlySettings) GetFloat64(key string, defaults float64) float64 {
any := s.Get(key)
v := any.ToFloat64()
if any.LastError() == nil {
return v
}
return defaults
}
func (s *FlySettings) GetBool(key string, defaults bool) bool {
any := s.Get(key)
v := any.ToString()
if any.LastError() == nil {
v = strings.ToLower(v)
v = strings.TrimSpace(v)
if v == "" || v == "0" || v == "false" || v == "f" || v == "null" || v == "nil" || v == "undefined" || v == "empty" || v == "{}" || v == "[]" || v == `""` || v == `''` {
return false
}
if len(v) > 0 {
return true
}
}
return defaults
}
func (s *FlySettings) SetByte(key string, value []byte) {
bstr := base64.StdEncoding.EncodeToString(value)
s.Set(key, bstr)
}
func (s *FlySettings) GetByte(key string) ([]byte, error) {
str := s.GetString(key, "")
return base64.StdEncoding.DecodeString(str)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/flyiot/flylibs.git
git@gitee.com:flyiot/flylibs.git
flyiot
flylibs
flylibs
741ab3437bd2

搜索帮助

0d507c66 1850385 C8b1a773 1850385