ratio must gte 0

This commit is contained in:
liuzhifei
2024-09-20 17:51:42 +08:00
parent c6ff785a83
commit 4eae3b2177

View File

@@ -2,6 +2,7 @@ package common
import (
"encoding/json"
"errors"
)
var GroupRatio = map[string]float64{
@@ -19,8 +20,14 @@ func GroupRatio2JSONString() string {
}
func UpdateGroupRatioByJSONString(jsonStr string) error {
GroupRatio = make(map[string]float64)
return json.Unmarshal([]byte(jsonStr), &GroupRatio)
tempGroupRatio := make(map[string]float64)
err := json.Unmarshal([]byte(jsonStr), &tempGroupRatio)
err = checkGroupRatio(tempGroupRatio)
if err != nil {
return err
}
GroupRatio = tempGroupRatio
return err
}
func GetGroupRatio(name string) float64 {
@@ -31,3 +38,12 @@ func GetGroupRatio(name string) float64 {
}
return ratio
}
func checkGroupRatio(checkGroupRatio map[string]float64) error {
for name, ratio := range checkGroupRatio {
if ratio < 0 {
return errors.New("group ratio must be greater than 0: " + name)
}
}
return nil
}