From 247490c35ddb32a864115ece245869cb1a8e9819 Mon Sep 17 00:00:00 2001 From: zhaoyupeng Date: Mon, 14 Jul 2025 11:16:34 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20resp=20=E5=A4=9A=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resp/resp.go | 52 +++++++++++++++++++++++++++++++++-------------- resp/resp_test.go | 9 ++++++++ 2 files changed, 46 insertions(+), 15 deletions(-) create mode 100644 resp/resp_test.go diff --git a/resp/resp.go b/resp/resp.go index f44db83..d05bdfa 100644 --- a/resp/resp.go +++ b/resp/resp.go @@ -43,23 +43,45 @@ func RE(c *gin.Context, err error) { func _r(c *gin.Context, r *res, args ...any) { length := len(args) - switch length { - case 0: - break - case 1: - if msg, ok := args[0].(string); ok { - r.Msg = msg - } - case 2: - r.Data = args[1] - case 3: - r.Err = args[2] - case 4: - if code, err := cast.ToIntE(args[3]); err == nil { - r.Code = code - } + + if length == 0 { + goto END } + if length >= 4 { + goto H4 + } + + if length >= 3 { + goto H3 + } + + if length >= 2 { + goto H2 + } + if length >= 1 { + goto H1 + } + +H4: + if code, err := cast.ToIntE(args[3]); err == nil { + r.Code = code + } +H3: + if es, ok := args[2].(error); ok { + r.Err = es.Error() + } else { + r.Err = args[2] + } +H2: + r.Data = args[1] +H1: + if msg, ok := args[0].(string); ok { + r.Msg = msg + } + +END: + if r.Msg == "" { r.Msg = Msg(r.Status) } diff --git a/resp/resp_test.go b/resp/resp_test.go new file mode 100644 index 0000000..8952b70 --- /dev/null +++ b/resp/resp_test.go @@ -0,0 +1,9 @@ +package resp + +import ( + "testing" +) + +func TestResp(t *testing.T) { + +}