optimize code and reduce code cyclomatic complexity

This commit is contained in:
tylitianrui 2021-05-25 10:21:15 +08:00
parent 328d0b8076
commit 59171e3425
2 changed files with 28 additions and 30 deletions

View File

@ -22,11 +22,9 @@ func (formBinding) Bind(req *http.Request, obj interface{}) error {
if err := req.ParseForm(); err != nil { if err := req.ParseForm(); err != nil {
return err return err
} }
if err := req.ParseMultipartForm(defaultMemory); err != nil { if err := req.ParseMultipartForm(defaultMemory); err != nil && err != http.ErrNotMultipart {
if err != http.ErrNotMultipart {
return err return err
} }
}
if err := mapForm(obj, req.Form); err != nil { if err := mapForm(obj, req.Form); err != nil {
return err return err
} }

View File

@ -24,7 +24,9 @@ func IsDebugging() bool {
var DebugPrintRouteFunc func(httpMethod, absolutePath, handlerName string, nuHandlers int) var DebugPrintRouteFunc func(httpMethod, absolutePath, handlerName string, nuHandlers int)
func debugPrintRoute(httpMethod, absolutePath string, handlers HandlersChain) { func debugPrintRoute(httpMethod, absolutePath string, handlers HandlersChain) {
if IsDebugging() { if !IsDebugging() {
return
}
nuHandlers := len(handlers) nuHandlers := len(handlers)
handlerName := nameOfFunction(handlers.Last()) handlerName := nameOfFunction(handlers.Last())
if DebugPrintRouteFunc == nil { if DebugPrintRouteFunc == nil {
@ -33,10 +35,10 @@ func debugPrintRoute(httpMethod, absolutePath string, handlers HandlersChain) {
DebugPrintRouteFunc(httpMethod, absolutePath, handlerName, nuHandlers) DebugPrintRouteFunc(httpMethod, absolutePath, handlerName, nuHandlers)
} }
} }
}
func debugPrintLoadTemplate(tmpl *template.Template) { func debugPrintLoadTemplate(tmpl *template.Template) {
if IsDebugging() { if !IsDebugging() {
return
}
var buf strings.Builder var buf strings.Builder
for _, tmpl := range tmpl.Templates() { for _, tmpl := range tmpl.Templates() {
buf.WriteString("\t- ") buf.WriteString("\t- ")
@ -45,16 +47,16 @@ func debugPrintLoadTemplate(tmpl *template.Template) {
} }
debugPrint("Loaded HTML Templates (%d): \n%s\n", len(tmpl.Templates()), buf.String()) debugPrint("Loaded HTML Templates (%d): \n%s\n", len(tmpl.Templates()), buf.String())
} }
}
func debugPrint(format string, values ...interface{}) { func debugPrint(format string, values ...interface{}) {
if IsDebugging() { if !IsDebugging() {
return
}
if !strings.HasSuffix(format, "\n") { if !strings.HasSuffix(format, "\n") {
format += "\n" format += "\n"
} }
fmt.Fprintf(DefaultWriter, "[GIN-debug] "+format, values...) fmt.Fprintf(DefaultWriter, "[GIN-debug] "+format, values...)
} }
}
func getMinVer(v string) (uint64, error) { func getMinVer(v string) (uint64, error) {
first := strings.IndexByte(v, '.') first := strings.IndexByte(v, '.')
@ -95,9 +97,7 @@ at initialization. ie. before any route is registered or the router is listening
} }
func debugPrintError(err error) { func debugPrintError(err error) {
if err != nil { if err != nil && IsDebugging() {
if IsDebugging() {
fmt.Fprintf(DefaultErrorWriter, "[GIN-debug] [ERROR] %v\n", err) fmt.Fprintf(DefaultErrorWriter, "[GIN-debug] [ERROR] %v\n", err)
} }
} }
}