63 lines
1.2 KiB
Go
63 lines
1.2 KiB
Go
|
package models
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"strconv"
|
||
|
)
|
||
|
|
||
|
func (m *BotModel) GetBytes(path []string) ([]byte, error) {
|
||
|
var err error
|
||
|
var v []byte
|
||
|
// Value is not cached, try to pull it from the DB
|
||
|
if len(path) > 2 {
|
||
|
path, key := path[:len(path)-1], path[len(path)-1]
|
||
|
v, err = m.db.GetBytes(path, key)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
}
|
||
|
return v, nil
|
||
|
}
|
||
|
|
||
|
func (m *BotModel) SetBytes(path []string, val []byte) error {
|
||
|
if len(path) > 1 {
|
||
|
path, key := path[:len(path)-1], path[len(path)-1]
|
||
|
err := m.db.SetBytes(path, key, val)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
return errors.New("Invalid path")
|
||
|
}
|
||
|
|
||
|
func (m *BotModel) GetString(path []string) (string, error) {
|
||
|
bts, err := m.GetBytes(path)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
if len(bts) == 0 {
|
||
|
return "", nil
|
||
|
}
|
||
|
return string(bts), nil
|
||
|
}
|
||
|
|
||
|
func (m *BotModel) SetString(path []string, val string) error {
|
||
|
return m.SetBytes(path, []byte(val))
|
||
|
}
|
||
|
|
||
|
func (m *BotModel) GetInt(path []string) (int, error) {
|
||
|
bts, err := m.GetBytes(path)
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
if len(bts) == 0 {
|
||
|
return 0, nil
|
||
|
}
|
||
|
return strconv.Atoi(string(bts))
|
||
|
}
|
||
|
|
||
|
func (m *BotModel) SetInt(path []string, val int) error {
|
||
|
return m.SetString(path, strconv.Itoa(val))
|
||
|
}
|