From 697156f6f6fee139ed16c6585c73addd1b49d961 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E=E6=89=87=E6=BB=91=E7=BF=94=E7=BF=BC?= Date: Sun, 29 Sep 2024 03:10:42 +0800 Subject: [PATCH] SplitHTTP Config: Add capability to parse int in string format (#3855) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit “100” → 100 “-1” → -1 “” → 0 --- infra/conf/common.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/infra/conf/common.go b/infra/conf/common.go index 9d31bc0a..24664859 100644 --- a/infra/conf/common.go +++ b/infra/conf/common.go @@ -252,10 +252,23 @@ type Int32Range struct { } func (v *Int32Range) UnmarshalJSON(data []byte) error { - var stringrange string + var str string var rawint int32 - if err := json.Unmarshal(data, &stringrange); err == nil { - pair := strings.SplitN(stringrange, "-", 2) + if err := json.Unmarshal(data, &str); err == nil { + // for number in string format like "114" or "-1" + if value, err := strconv.Atoi(str); err == nil { + v.From = int32(value) + v.To = int32(value) + return nil + } + // for empty "", we treat it as 0 + if str == "" { + v.From = 0 + v.To = 0 + return nil + } + // for range value, like "114-514" + pair := strings.SplitN(str, "-", 2) if len(pair) == 2 { from, err := strconv.Atoi(pair[0]) to, err2 := strconv.Atoi(pair[1])