From aa1f481f373b98af91de8baf4e14a9fc96e3d4d6 Mon Sep 17 00:00:00 2001 From: joshblakeley Date: Tue, 8 May 2018 15:54:11 +0100 Subject: [PATCH] remove uses of fmt for lower level operations --- event_system.go | 7 +++---- handler_error.go | 7 +++---- handler_success.go | 4 +--- main.go | 4 ++-- mw_api_rate_limit.go | 3 +-- mw_url_rewrite.go | 14 +++++++++----- mw_validate_json.go | 2 +- 7 files changed, 20 insertions(+), 21 deletions(-) diff --git a/event_system.go b/event_system.go index c7e2129cb2b..6e588f4628a 100644 --- a/event_system.go +++ b/event_system.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/base64" "errors" - "fmt" "net/http" "time" @@ -167,17 +166,17 @@ func (l *LogMessageEventHandler) Init(handlerConf interface{}) error { // HandleEvent will be fired when the event handler instance is found in an APISpec EventPaths object during a request chain func (l *LogMessageEventHandler) HandleEvent(em config.EventMessage) { - logMsg := fmt.Sprintf("%s:%s", l.prefix, em.Type) + logMsg := l.prefix + ":" + string(em.Type) // We can handle specific event types easily if em.Type == EventQuotaExceeded { msgConf := em.Meta.(EventKeyFailureMeta) - logMsg = fmt.Sprintf("%s:%s:%s:%s", logMsg, msgConf.Key, msgConf.Origin, msgConf.Path) + logMsg = logMsg + ":" + msgConf.Key + ":" + msgConf.Origin + ":" + msgConf.Path } if em.Type == EventBreakerTriggered { msgConf := em.Meta.(EventCurcuitBreakerMeta) - logMsg = fmt.Sprintf("%s:%s:%s: [STATUS] %v", logMsg, msgConf.APIID, msgConf.Path, msgConf.CircuitEvent) + logMsg = logMsg + ":" + msgConf.APIID + ":" + msgConf.Path + ": [STATUS] " + string(msgConf.CircuitEvent) } log.Warning(logMsg) diff --git a/handler_error.go b/handler_error.go index 92f268c0515..382dc24348a 100644 --- a/handler_error.go +++ b/handler_error.go @@ -3,7 +3,6 @@ package main import ( "bytes" "encoding/base64" - "fmt" "net/http" "runtime/pprof" "strconv" @@ -47,20 +46,20 @@ func (e *ErrorHandler) HandleError(w http.ResponseWriter, r *http.Request, errMs w.Header().Set("Content-Type", contentType) - templateName := fmt.Sprintf("error_%s.%s", strconv.Itoa(errCode), templateExtension) + templateName := "error_" + strconv.Itoa(errCode) + "." + templateExtension // Try to use an error template that matches the HTTP error code and the content type: 500.json, 400.xml, etc. tmpl := templates.Lookup(templateName) // Fallback to a generic error template, but match the content type: error.json, error.xml, etc. if tmpl == nil { - templateName = fmt.Sprintf("%s.%s", defaultTemplateName, templateExtension) + templateName = defaultTemplateName + "." + templateExtension tmpl = templates.Lookup(templateName) } // If no template is available for this content type, fallback to "error.json". if tmpl == nil { - templateName = fmt.Sprintf("%s.%s", defaultTemplateName, defaultTemplateFormat) + templateName = defaultTemplateName + "." + defaultTemplateFormat tmpl = templates.Lookup(templateName) w.Header().Set("Content-Type", defaultContentType) } diff --git a/handler_success.go b/handler_success.go index 0b6b8ef897c..d415432e13e 100644 --- a/handler_success.go +++ b/handler_success.go @@ -14,8 +14,6 @@ import ( "github.com/TykTechnologies/tyk/request" - "fmt" - "github.com/TykTechnologies/tyk/config" "github.com/TykTechnologies/tyk/user" ) @@ -62,7 +60,7 @@ func tagHeaders(r *http.Request, th []string, tags []string) []string { if ok { for _, val := range v { - tagName := fmt.Sprintf("%s-%s", cleanK, val) + tagName := cleanK + "-" + val tags = append(tags, tagName) } } diff --git a/main.go b/main.go index 0f52cfaeb3e..9cc7203faa8 100644 --- a/main.go +++ b/main.go @@ -1125,7 +1125,7 @@ func start() { func generateListener(listenPort int) (net.Listener, error) { listenAddress := config.Global().ListenAddress - targetPort := fmt.Sprintf("%s:%d", listenAddress, listenPort) + targetPort := listenAddress + ":" + strconv.Itoa(listenPort) if httpServerOptions := config.Global().HttpServerOptions; httpServerOptions.UseSSL { mainLog.Info("--> Using SSL (https)") @@ -1213,7 +1213,7 @@ func listen(listener, controlListener net.Listener, err error) { readTimeout := defReadTimeout writeTimeout := defWriteTimeout - targetPort := fmt.Sprintf("%s:%d", config.Global().ListenAddress, config.Global().ListenPort) + targetPort := config.Global().ListenAddress + ":" + strconv.Itoa(config.Global().ListenPort) if config.Global().HttpServerOptions.ReadTimeout > 0 { readTimeout = time.Duration(config.Global().HttpServerOptions.ReadTimeout) * time.Second } diff --git a/mw_api_rate_limit.go b/mw_api_rate_limit.go index 3f86c3d16a0..3218d54db39 100644 --- a/mw_api_rate_limit.go +++ b/mw_api_rate_limit.go @@ -2,7 +2,6 @@ package main import ( "errors" - "fmt" "net/http" "strconv" @@ -30,7 +29,7 @@ func (k *RateLimitForAPI) EnabledForSpec() bool { } // We'll init here - k.keyName = fmt.Sprintf("apilimiter-%s%s", k.Spec.OrgID, k.Spec.APIID) + k.keyName = "apilimiter-" + k.Spec.OrgID + k.Spec.APIID // Set last updated on each load to ensure we always use a new rate limit bucket k.apiSess = &user.SessionState{ diff --git a/mw_url_rewrite.go b/mw_url_rewrite.go index 5ca593e743b..eba631d7b2a 100644 --- a/mw_url_rewrite.go +++ b/mw_url_rewrite.go @@ -352,7 +352,7 @@ func checkHeaderTrigger(r *http.Request, options map[string]apidef.StringRegexMa for i, v := range vals { b := mr.Check(v) if len(b) > 0 { - kn := fmt.Sprintf("trigger-%d-%s-%d", triggernum, mhCN, i) + kn := "trigger-" + strconv.Itoa(triggernum) + "-" + mhCN + "-" + strconv.Itoa(i) contextData[kn] = b fCount++ } @@ -382,7 +382,8 @@ func checkQueryString(r *http.Request, options map[string]apidef.StringRegexMap, for i, v := range vals { b := mr.Check(v) if len(b) > 0 { - kn := fmt.Sprintf("trigger-%d-%s-%d", triggernum, mv, i) + kn := "trigger-" + strconv.Itoa(triggernum) + "-" + mv + "-" + strconv.Itoa(i) + contextData[kn] = b fCount++ } @@ -411,7 +412,8 @@ func checkPathParts(r *http.Request, options map[string]apidef.StringRegexMap, a for _, part := range pathParts { b := mr.Check(part) if len(b) > 0 { - kn := fmt.Sprintf("trigger-%d-%s-%d", triggernum, mv, fCount) + kn := "trigger-" + strconv.Itoa(triggernum) + "-" + mv + "-" + strconv.Itoa(fCount) + contextData[kn] = b fCount++ } @@ -440,7 +442,8 @@ func checkSessionTrigger(r *http.Request, sess *user.SessionState, options map[s if valOk { b := mr.Check(val) if len(b) > 0 { - kn := fmt.Sprintf("trigger-%d-%s", triggernum, mh) + kn := "trigger-" + strconv.Itoa(triggernum) + "-" + mh + contextData[kn] = b fCount++ } @@ -467,7 +470,8 @@ func checkPayload(r *http.Request, options apidef.StringRegexMap, triggernum int b := options.Check(string(bodyBytes)) if len(b) > 0 { - kn := fmt.Sprintf("trigger-%d-payload", triggernum) + kn := "trigger-" + strconv.Itoa(triggernum) + "-payload" + contextData[kn] = string(b) return true } diff --git a/mw_validate_json.go b/mw_validate_json.go index 988c0a147dc..0d451df3233 100644 --- a/mw_validate_json.go +++ b/mw_validate_json.go @@ -82,7 +82,7 @@ func (k *ValidateJSON) formatError(schemaErrors []gojsonschema.ResultError) erro if i == 0 { errStr = desc.String() } else { - errStr = fmt.Sprintf("%s; %s", errStr, desc) + errStr = errStr + "; " + desc.String() } }