From 9758e55b72aa9faf6060cec46f98732450a95205 Mon Sep 17 00:00:00 2001 From: UlricQin Date: Thu, 15 Jul 2021 18:46:40 +0800 Subject: [PATCH] code refactor: extract _s and _e func --- http/http_funcs.go | 13 +++++++++++-- http/router_collect_rule.go | 7 +++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/http/http_funcs.go b/http/http_funcs.go index 33deb0ed..06b074f2 100644 --- a/http/http_funcs.go +++ b/http/http_funcs.go @@ -1,6 +1,7 @@ package http import ( + "fmt" "net/http" "strconv" "strings" @@ -15,12 +16,20 @@ import ( const defaultLimit = 20 +func _e(format string, a ...interface{}) error { + return fmt.Errorf(_s(format, a...)) +} + +func _s(format string, a ...interface{}) string { + return i18n.Sprintf(format, a...) +} + func dangerous(v interface{}, code ...int) { ierr.Dangerous(v, code...) } func bomb(code int, format string, a ...interface{}) { - ierr.Bomb(code, i18n.Sprintf(format, a...)) + ierr.Bomb(code, _s(format, a...)) } func bind(c *gin.Context, ptr interface{}) { @@ -138,7 +147,7 @@ func renderMessage(c *gin.Context, v interface{}, statusCode ...int) { switch t := v.(type) { case string: - c.JSON(code, gin.H{"err": i18n.Sprintf(t)}) + c.JSON(code, gin.H{"err": _s(t)}) case error: c.JSON(code, gin.H{"err": t.Error()}) } diff --git a/http/router_collect_rule.go b/http/router_collect_rule.go index e3473bf8..7f0309d7 100644 --- a/http/router_collect_rule.go +++ b/http/router_collect_rule.go @@ -9,7 +9,6 @@ import ( "github.com/didi/nightingale/v5/cache" "github.com/didi/nightingale/v5/models" - "github.com/didi/nightingale/v5/pkg/i18n" ) type collectRuleForm struct { @@ -244,15 +243,15 @@ func includeIllegalChar(s string) bool { // 生成返回错误信息 func genErrMsg(sign string) string { - return i18n.Sprintf("regular match failed, please check your configuration:[%s]", sign) + return _s("regular match failed, please check your configuration:[%s]", sign) } // 生成子串匹配错误信息 func genSubErrMsg(sign string) string { - return i18n.Sprintf("regular match was successful. according to the configuration, it does not get the substring inside(), please check your configuration:[%s]", sign) + return _s("regular match was successful. according to the configuration, it does not get the substring inside(), please check your configuration:[%s]", sign) } // 生成子串匹配错误信息 func genIllegalCharErrMsg() string { - return i18n.Sprintf(`key or value of tag contains illegal characters:[:,/=\r\n\t]`) + return _s(`key or value of tag contains illegal characters:[:,/=\r\n\t]`) } -- GitLab