forked from p93542168/wheat-cache
perf(incr): option incr to values
This commit is contained in:
parent
5ce350f115
commit
778d5152c8
|
@ -1,9 +1,6 @@
|
|||
package stringx
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"gitee.com/wheat-os/wheatCache/pkg/errorx"
|
||||
"gitee.com/wheat-os/wheatCache/pkg/structure"
|
||||
)
|
||||
|
||||
|
@ -51,32 +48,8 @@ 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)
|
||||
result, err := s.val.Incr(renewal)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -84,7 +57,7 @@ func (s *StringSingle) Add(renewal int32) (string, error) {
|
|||
}
|
||||
|
||||
func (s *StringSingle) Reduce(renewal int32) (string, error) {
|
||||
result, err := updateValueNotString(s, -1*renewal)
|
||||
result, err := s.val.Incr(-1 * renewal)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
|
|
@ -187,3 +187,28 @@ func (v *Value) SliceByString(start, end int) ([]byte, error) {
|
|||
|
||||
return v.val[start:end], nil
|
||||
}
|
||||
|
||||
// 自增
|
||||
func (v *Value) Incr(renewal int32) (string, error) {
|
||||
switch v.GetDynamicType() {
|
||||
case DynamicNull:
|
||||
v.SetInt(int64(renewal))
|
||||
return strconv.Itoa(int(renewal)), nil
|
||||
case DynamicFloat:
|
||||
f, err := v.ToFloat64()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
v.SetFloat64(f + float64(renewal))
|
||||
return strconv.FormatFloat(f+float64(renewal), 'f', 2, 64), nil
|
||||
case DynamicInt:
|
||||
i, err := v.ToInt()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
v.SetInt(int64(renewal) + i)
|
||||
return strconv.Itoa(int(i + int64(renewal))), nil
|
||||
default:
|
||||
return "", errorx.New("string cannot perform add operations")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue