121 lines
2.6 KiB
Go
121 lines
2.6 KiB
Go
package stringx
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"gitee.com/timedb/wheatCache/pkg/errorx"
|
|
"gitee.com/timedb/wheatCache/pkg/structure"
|
|
)
|
|
|
|
type StringSingle struct {
|
|
val *structure.Value
|
|
}
|
|
|
|
func NewStringSingle() structure.StringXInterface {
|
|
return &StringSingle{
|
|
val: structure.NewValue(),
|
|
}
|
|
}
|
|
|
|
func (s *StringSingle) SizeByte() int64 {
|
|
return int64(s.val.GetSize())
|
|
}
|
|
|
|
// RollBack TODO 事务相关, V2 实现
|
|
func (s *StringSingle) RollBack() error {
|
|
panic("not implemented") // TODO: Implement
|
|
}
|
|
|
|
// Begin 事务相关, V2 实现
|
|
func (s *StringSingle) Begin() error {
|
|
panic("not implemented") // TODO: Implement
|
|
}
|
|
|
|
// Comment 事务相关, V2 实现
|
|
func (s *StringSingle) Comment() error {
|
|
panic("not implemented") // TODO: Implement
|
|
}
|
|
|
|
func (s *StringSingle) Encode() ([]byte, error) {
|
|
panic("not implemented") // TODO: Implement
|
|
}
|
|
|
|
func (s *StringSingle) Set(val string) (string, structure.UpdateLength) {
|
|
length := s.val.ChangeValueLength(func() {
|
|
s.val.InferValue(val)
|
|
})
|
|
return val, length
|
|
}
|
|
|
|
func (s *StringSingle) Get() string {
|
|
return s.val.ToString()
|
|
}
|
|
|
|
func updateValueNotString(s *StringSingle, val int32) (string, error) {
|
|
switch s.val.GetDynamicType() {
|
|
case structure.DynamicNull:
|
|
s.val.SetInt(int64(val))
|
|
return strconv.Itoa(int(val)), nil
|
|
case structure.DynamicFloat:
|
|
f, err := s.val.ToFloat64()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
s.val.SetFloat64(f + float64(val))
|
|
return strconv.FormatFloat(f+float64(val), 'f', 2, 64), nil
|
|
case structure.DynamicInt:
|
|
i, err := s.val.ToInt()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
s.val.SetInt(int64(val) + i)
|
|
return strconv.Itoa(int(i + int64(val))), nil
|
|
default:
|
|
return "", errorx.New("string cannot perform add operations")
|
|
}
|
|
}
|
|
|
|
func (s *StringSingle) Add(renewal int32) (string, error) {
|
|
result, err := updateValueNotString(s, renewal)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (s *StringSingle) Reduce(renewal int32) (string, error) {
|
|
result, err := updateValueNotString(s, -1*renewal)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (s *StringSingle) Setbit(offer int32, val bool) structure.UpdateLength {
|
|
length := s.val.ChangeValueLength(func() {
|
|
s.val.SetByte(int(offer), val)
|
|
})
|
|
return length
|
|
}
|
|
|
|
func (s *StringSingle) Getbit(offer int32) (bool, error) {
|
|
b, err := s.val.GetByte(int(offer))
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return b, err
|
|
}
|
|
|
|
func (s *StringSingle) Getrange(start, end int32) (string, error) {
|
|
b, err := s.val.SliceByString(int(start), int(end))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(b), nil
|
|
}
|
|
|
|
func (s *StringSingle) GetLength() int {
|
|
return s.val.GetLength()
|
|
}
|