Commit 9e8faac4 by liuzhifei

ratio must gte 0

parent 3ef8b740
...@@ -2,6 +2,7 @@ package common ...@@ -2,6 +2,7 @@ package common
import ( import (
"encoding/json" "encoding/json"
"errors"
) )
var GroupRatio = map[string]float64{ var GroupRatio = map[string]float64{
...@@ -19,8 +20,14 @@ func GroupRatio2JSONString() string { ...@@ -19,8 +20,14 @@ func GroupRatio2JSONString() string {
} }
func UpdateGroupRatioByJSONString(jsonStr string) error { func UpdateGroupRatioByJSONString(jsonStr string) error {
GroupRatio = make(map[string]float64) tempGroupRatio := make(map[string]float64)
return json.Unmarshal([]byte(jsonStr), &GroupRatio) err := json.Unmarshal([]byte(jsonStr), &tempGroupRatio)
err = checkGroupRatio(tempGroupRatio)
if err != nil {
return err
}
GroupRatio = tempGroupRatio
return err
} }
func GetGroupRatio(name string) float64 { func GetGroupRatio(name string) float64 {
...@@ -31,3 +38,12 @@ func GetGroupRatio(name string) float64 { ...@@ -31,3 +38,12 @@ func GetGroupRatio(name string) float64 {
} }
return ratio 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
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment