From 73a8bb7a012c81c12cf88ddbbdfa248938f4665a Mon Sep 17 00:00:00 2001 From: Alexander Melentyev Date: Sun, 22 Aug 2021 21:43:00 +0300 Subject: [PATCH] Group types and vars --- auth.go | 16 ++++---- binding/binding.go | 40 ++++++++++--------- binding/binding_nomsgpack.go | 40 ++++++++++--------- binding/form.go | 8 ++-- binding/json.go | 20 +++++----- errors.go | 16 ++++---- fs.go | 14 ++++--- gin.go | 30 ++++++++------ logger.go | 76 ++++++++++++++++++------------------ mode.go | 22 ++++++----- response_writer.go | 52 ++++++++++++------------ routergroup.go | 60 ++++++++++++++-------------- tree.go | 20 +++++----- 13 files changed, 221 insertions(+), 193 deletions(-) diff --git a/auth.go b/auth.go index 4d8a6ce4..bb351ba2 100644 --- a/auth.go +++ b/auth.go @@ -16,15 +16,17 @@ import ( // AuthUserKey is the cookie name for user credential in basic auth. const AuthUserKey = "user" -// Accounts defines a key/value for user/pass list of authorized logins. -type Accounts map[string]string +type ( + // Accounts defines a key/value for user/pass list of authorized logins. + Accounts map[string]string -type authPair struct { - value string - user string -} + authPair struct { + value string + user string + } -type authPairs []authPair + authPairs []authPair +) func (a authPairs) searchCredential(authValue string) (string, bool) { if authValue == "" { diff --git a/binding/binding.go b/binding/binding.go index deb71661..6553098d 100644 --- a/binding/binding.go +++ b/binding/binding.go @@ -24,27 +24,29 @@ const ( MIMEYAML = "application/x-yaml" ) -// Binding describes the interface which needs to be implemented for binding the -// data present in the request such as JSON request body, query parameters or -// the form POST. -type Binding interface { - Name() string - Bind(*http.Request, interface{}) error -} +type ( + // Binding describes the interface which needs to be implemented for binding the + // data present in the request such as JSON request body, query parameters or + // the form POST. + Binding interface { + Name() string + Bind(*http.Request, interface{}) error + } -// BindingBody adds BindBody method to Binding. BindBody is similar with Bind, -// but it reads the body from supplied bytes instead of req.Body. -type BindingBody interface { - Binding - BindBody([]byte, interface{}) error -} + // BindingBody adds BindBody method to Binding. BindBody is similar with Bind, + // but it reads the body from supplied bytes instead of req.Body. + BindingBody interface { + Binding + BindBody([]byte, interface{}) error + } -// BindingUri adds BindUri method to Binding. BindUri is similar with Bind, -// but it read the Params. -type BindingUri interface { - Name() string - BindUri(map[string][]string, interface{}) error -} + // BindingUri adds BindUri method to Binding. BindUri is similar with Bind, + // but it read the Params. + BindingUri interface { + Name() string + BindUri(map[string][]string, interface{}) error + } +) // StructValidator is the minimal interface which needs to be implemented in // order for it to be used as the validator engine for ensuring the correctness diff --git a/binding/binding_nomsgpack.go b/binding/binding_nomsgpack.go index 23424470..2c1349cf 100644 --- a/binding/binding_nomsgpack.go +++ b/binding/binding_nomsgpack.go @@ -22,27 +22,29 @@ const ( MIMEYAML = "application/x-yaml" ) -// Binding describes the interface which needs to be implemented for binding the -// data present in the request such as JSON request body, query parameters or -// the form POST. -type Binding interface { - Name() string - Bind(*http.Request, interface{}) error -} +type ( + // Binding describes the interface which needs to be implemented for binding the + // data present in the request such as JSON request body, query parameters or + // the form POST. + Binding interface { + Name() string + Bind(*http.Request, interface{}) error + } -// BindingBody adds BindBody method to Binding. BindBody is similar with Bind, -// but it reads the body from supplied bytes instead of req.Body. -type BindingBody interface { - Binding - BindBody([]byte, interface{}) error -} + // BindingBody adds BindBody method to Binding. BindBody is similar with Bind, + // but it reads the body from supplied bytes instead of req.Body. + BindingBody interface { + Binding + BindBody([]byte, interface{}) error + } -// BindingUri adds BindUri method to Binding. BindUri is similar with Bind, -// but it read the Params. -type BindingUri interface { - Name() string - BindUri(map[string][]string, interface{}) error -} + // BindingUri adds BindUri method to Binding. BindUri is similar with Bind, + // but it read the Params. + BindingUri interface { + Name() string + BindUri(map[string][]string, interface{}) error + } +) // StructValidator is the minimal interface which needs to be implemented in // order for it to be used as the validator engine for ensuring the correctness diff --git a/binding/form.go b/binding/form.go index 040af9e2..7ea3d95b 100644 --- a/binding/form.go +++ b/binding/form.go @@ -10,9 +10,11 @@ import ( const defaultMemory = 32 << 20 -type formBinding struct{} -type formPostBinding struct{} -type formMultipartBinding struct{} +type ( + formBinding struct{} + formPostBinding struct{} + formMultipartBinding struct{} +) func (formBinding) Name() string { return "form" diff --git a/binding/json.go b/binding/json.go index 45aaa494..60ba2e90 100644 --- a/binding/json.go +++ b/binding/json.go @@ -13,16 +13,18 @@ import ( "github.com/gin-gonic/gin/internal/json" ) -// EnableDecoderUseNumber is used to call the UseNumber method on the JSON -// Decoder instance. UseNumber causes the Decoder to unmarshal a number into an -// interface{} as a Number instead of as a float64. -var EnableDecoderUseNumber = false +var ( + // EnableDecoderUseNumber is used to call the UseNumber method on the JSON + // Decoder instance. UseNumber causes the Decoder to unmarshal a number into an + // interface{} as a Number instead of as a float64. + EnableDecoderUseNumber = false -// EnableDecoderDisallowUnknownFields is used to call the DisallowUnknownFields method -// on the JSON Decoder instance. DisallowUnknownFields causes the Decoder to -// return an error when the destination is a struct and the input contains object -// keys which do not match any non-ignored, exported fields in the destination. -var EnableDecoderDisallowUnknownFields = false + // EnableDecoderDisallowUnknownFields is used to call the DisallowUnknownFields method + // on the JSON Decoder instance. DisallowUnknownFields causes the Decoder to + // return an error when the destination is a struct and the input contains object + // keys which do not match any non-ignored, exported fields in the destination. + EnableDecoderDisallowUnknownFields = false +) type jsonBinding struct{} diff --git a/errors.go b/errors.go index 0f276c13..7290f6ca 100644 --- a/errors.go +++ b/errors.go @@ -30,14 +30,16 @@ const ( ErrorTypeNu = 2 ) -// Error represents a error's specification. -type Error struct { - Err error - Type ErrorType - Meta interface{} -} +type ( + // Error represents a error's specification. + Error struct { + Err error + Type ErrorType + Meta interface{} + } -type errorMsgs []*Error + errorMsgs []*Error +) var _ error = &Error{} diff --git a/fs.go b/fs.go index e5f3d602..6ca3eded 100644 --- a/fs.go +++ b/fs.go @@ -9,13 +9,15 @@ import ( "os" ) -type onlyFilesFS struct { - fs http.FileSystem -} +type ( + onlyFilesFS struct { + fs http.FileSystem + } -type neuteredReaddirFile struct { - http.File -} + neuteredReaddirFile struct { + http.File + } +) // Dir returns a http.FileSystem that can be used by http.FileServer(). It is used internally // in router.Static(). diff --git a/gin.go b/gin.go index 6ab2be66..9137f674 100644 --- a/gin.go +++ b/gin.go @@ -27,11 +27,13 @@ var ( var defaultPlatform string -// HandlerFunc defines the handler used by gin middleware as return value. -type HandlerFunc func(*Context) +type ( + // HandlerFunc defines the handler used by gin middleware as return value. + HandlerFunc func(*Context) -// HandlersChain defines a HandlerFunc array. -type HandlersChain []HandlerFunc + // HandlersChain defines a HandlerFunc array. + HandlersChain []HandlerFunc +) // Last returns the last handler in the chain. ie. the last handler is the main one. func (c HandlersChain) Last() HandlerFunc { @@ -41,16 +43,18 @@ func (c HandlersChain) Last() HandlerFunc { return nil } -// RouteInfo represents a request route's specification which contains method and path and its handler. -type RouteInfo struct { - Method string - Path string - Handler string - HandlerFunc HandlerFunc -} +type ( + // RouteInfo represents a request route's specification which contains method and path and its handler. + RouteInfo struct { + Method string + Path string + Handler string + HandlerFunc HandlerFunc + } -// RoutesInfo defines a RouteInfo array. -type RoutesInfo []RouteInfo + // RoutesInfo defines a RouteInfo array. + RoutesInfo []RouteInfo +) // Trusted platforms const ( diff --git a/logger.go b/logger.go index 22138a8d..54995297 100644 --- a/logger.go +++ b/logger.go @@ -35,48 +35,50 @@ const ( var consoleColorMode = autoColor -// LoggerConfig defines the config for Logger middleware. -type LoggerConfig struct { - // Optional. Default value is gin.defaultLogFormatter - Formatter LogFormatter +type ( + // LoggerConfig defines the config for Logger middleware. + LoggerConfig struct { + // Optional. Default value is gin.defaultLogFormatter + Formatter LogFormatter - // Output is a writer where logs are written. - // Optional. Default value is gin.DefaultWriter. - Output io.Writer + // Output is a writer where logs are written. + // Optional. Default value is gin.DefaultWriter. + Output io.Writer - // SkipPaths is a url path array which logs are not written. - // Optional. - SkipPaths []string -} + // SkipPaths is a url path array which logs are not written. + // Optional. + SkipPaths []string + } -// LogFormatter gives the signature of the formatter function passed to LoggerWithFormatter -type LogFormatter func(params LogFormatterParams) string + // LogFormatter gives the signature of the formatter function passed to LoggerWithFormatter + LogFormatter func(params LogFormatterParams) string -// LogFormatterParams is the structure any formatter will be handed when time to log comes -type LogFormatterParams struct { - Request *http.Request + // LogFormatterParams is the structure any formatter will be handed when time to log comes + LogFormatterParams struct { + Request *http.Request - // TimeStamp shows the time after the server returns a response. - TimeStamp time.Time - // StatusCode is HTTP response code. - StatusCode int - // Latency is how much time the server cost to process a certain request. - Latency time.Duration - // ClientIP equals Context's ClientIP method. - ClientIP string - // Method is the HTTP method given to the request. - Method string - // Path is a path the client requests. - Path string - // ErrorMessage is set if error has occurred in processing the request. - ErrorMessage string - // isTerm shows whether does gin's output descriptor refers to a terminal. - isTerm bool - // BodySize is the size of the Response Body - BodySize int - // Keys are the keys set on the request's context. - Keys map[string]interface{} -} + // TimeStamp shows the time after the server returns a response. + TimeStamp time.Time + // StatusCode is HTTP response code. + StatusCode int + // Latency is how much time the server cost to process a certain request. + Latency time.Duration + // ClientIP equals Context's ClientIP method. + ClientIP string + // Method is the HTTP method given to the request. + Method string + // Path is a path the client requests. + Path string + // ErrorMessage is set if error has occurred in processing the request. + ErrorMessage string + // isTerm shows whether does gin's output descriptor refers to a terminal. + isTerm bool + // BodySize is the size of the Response Body + BodySize int + // Keys are the keys set on the request's context. + Keys map[string]interface{} + } +) // StatusCodeColor is the ANSI color for appropriately logging http status code to a terminal. func (p *LogFormatterParams) StatusCodeColor() string { diff --git a/mode.go b/mode.go index 4d199df3..7062d9b4 100644 --- a/mode.go +++ b/mode.go @@ -29,17 +29,19 @@ const ( testCode ) -// DefaultWriter is the default io.Writer used by Gin for debug output and -// middleware output like Logger() or Recovery(). -// Note that both Logger and Recovery provides custom ways to configure their -// output io.Writer. -// To support coloring in Windows use: -// import "github.com/mattn/go-colorable" -// gin.DefaultWriter = colorable.NewColorableStdout() -var DefaultWriter io.Writer = os.Stdout +var ( + // DefaultWriter is the default io.Writer used by Gin for debug output and + // middleware output like Logger() or Recovery(). + // Note that both Logger and Recovery provides custom ways to configure their + // output io.Writer. + // To support coloring in Windows use: + // import "github.com/mattn/go-colorable" + // gin.DefaultWriter = colorable.NewColorableStdout() + DefaultWriter io.Writer = os.Stdout -// DefaultErrorWriter is the default io.Writer used by Gin to debug errors -var DefaultErrorWriter io.Writer = os.Stderr + // DefaultErrorWriter is the default io.Writer used by Gin to debug errors + DefaultErrorWriter io.Writer = os.Stderr +) var ( ginMode = debugCode diff --git a/response_writer.go b/response_writer.go index 26826689..da5bc964 100644 --- a/response_writer.go +++ b/response_writer.go @@ -16,38 +16,40 @@ const ( defaultStatus = http.StatusOK ) -// ResponseWriter ... -type ResponseWriter interface { - http.ResponseWriter - http.Hijacker - http.Flusher - http.CloseNotifier +type ( + // ResponseWriter ... + ResponseWriter interface { + http.ResponseWriter + http.Hijacker + http.Flusher + http.CloseNotifier - // Returns the HTTP response status code of the current request. - Status() int + // Returns the HTTP response status code of the current request. + Status() int - // Returns the number of bytes already written into the response http body. - // See Written() - Size() int + // Returns the number of bytes already written into the response http body. + // See Written() + Size() int - // Writes the string into the response body. - WriteString(string) (int, error) + // Writes the string into the response body. + WriteString(string) (int, error) - // Returns true if the response body was already written. - Written() bool + // Returns true if the response body was already written. + Written() bool - // Forces to write the http header (status code + headers). - WriteHeaderNow() + // Forces to write the http header (status code + headers). + WriteHeaderNow() - // get the http.Pusher for server push - Pusher() http.Pusher -} + // get the http.Pusher for server push + Pusher() http.Pusher + } -type responseWriter struct { - http.ResponseWriter - size int - status int -} + responseWriter struct { + http.ResponseWriter + size int + status int + } +) var _ ResponseWriter = &responseWriter{} diff --git a/routergroup.go b/routergroup.go index bb24bd52..6e82ddfb 100644 --- a/routergroup.go +++ b/routergroup.go @@ -16,39 +16,41 @@ var ( regEnLetter = regexp.MustCompile("^[A-Z]+$") ) -// IRouter defines all router handle interface includes single and group router. -type IRouter interface { - IRoutes - Group(string, ...HandlerFunc) *RouterGroup -} +type ( + // IRouter defines all router handle interface includes single and group router. + IRouter interface { + IRoutes + Group(string, ...HandlerFunc) *RouterGroup + } -// IRoutes defines all router handle interface. -type IRoutes interface { - Use(...HandlerFunc) IRoutes + // IRoutes defines all router handle interface. + IRoutes interface { + Use(...HandlerFunc) IRoutes - Handle(string, string, ...HandlerFunc) IRoutes - Any(string, ...HandlerFunc) IRoutes - GET(string, ...HandlerFunc) IRoutes - POST(string, ...HandlerFunc) IRoutes - DELETE(string, ...HandlerFunc) IRoutes - PATCH(string, ...HandlerFunc) IRoutes - PUT(string, ...HandlerFunc) IRoutes - OPTIONS(string, ...HandlerFunc) IRoutes - HEAD(string, ...HandlerFunc) IRoutes + Handle(string, string, ...HandlerFunc) IRoutes + Any(string, ...HandlerFunc) IRoutes + GET(string, ...HandlerFunc) IRoutes + POST(string, ...HandlerFunc) IRoutes + DELETE(string, ...HandlerFunc) IRoutes + PATCH(string, ...HandlerFunc) IRoutes + PUT(string, ...HandlerFunc) IRoutes + OPTIONS(string, ...HandlerFunc) IRoutes + HEAD(string, ...HandlerFunc) IRoutes - StaticFile(string, string) IRoutes - Static(string, string) IRoutes - StaticFS(string, http.FileSystem) IRoutes -} + StaticFile(string, string) IRoutes + Static(string, string) IRoutes + StaticFS(string, http.FileSystem) IRoutes + } -// RouterGroup is used internally to configure router, a RouterGroup is associated with -// a prefix and an array of handlers (middleware). -type RouterGroup struct { - Handlers HandlersChain - basePath string - engine *Engine - root bool -} + // RouterGroup is used internally to configure router, a RouterGroup is associated with + // a prefix and an array of handlers (middleware). + RouterGroup struct { + Handlers HandlersChain + basePath string + engine *Engine + root bool + } +) var _ IRouter = &RouterGroup{} diff --git a/tree.go b/tree.go index fb0a5935..d0dce170 100644 --- a/tree.go +++ b/tree.go @@ -19,16 +19,18 @@ var ( strStar = []byte("*") ) -// Param is a single URL parameter, consisting of a key and a value. -type Param struct { - Key string - Value string -} +type ( + // Param is a single URL parameter, consisting of a key and a value. + Param struct { + Key string + Value string + } -// Params is a Param-slice, as returned by the router. -// The slice is ordered, the first URL parameter is also the first slice value. -// It is therefore safe to read values by the index. -type Params []Param + // Params is a Param-slice, as returned by the router. + // The slice is ordered, the first URL parameter is also the first slice value. + // It is therefore safe to read values by the index. + Params []Param +) // Get returns the value of the first Param which key matches the given name and a boolean true. // If no matching Param is found, an empty string is returned and a boolean false .