From 4eae3b2177da85a0f945230884f5d68f6c2efc81 Mon Sep 17 00:00:00 2001 From: liuzhifei <2679431923@qq.com> Date: Fri, 20 Sep 2024 17:51:42 +0800 Subject: [PATCH] ratio must gte 0 --- common/group-ratio.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/common/group-ratio.go b/common/group-ratio.go index 416ba037..ef132624 100644 --- a/common/group-ratio.go +++ b/common/group-ratio.go @@ -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 +}