Group types and vars

This commit is contained in:
Alexander Melentyev 2021-08-22 21:43:00 +03:00
parent 4e7584175d
commit 73a8bb7a01
13 changed files with 221 additions and 193 deletions

View File

@ -16,15 +16,17 @@ import (
// AuthUserKey is the cookie name for user credential in basic auth.
const AuthUserKey = "user"
type (
// Accounts defines a key/value for user/pass list of authorized logins.
type Accounts map[string]string
Accounts map[string]string
type authPair struct {
authPair struct {
value string
user string
}
type authPairs []authPair
authPairs []authPair
)
func (a authPairs) searchCredential(authValue string) (string, bool) {
if authValue == "" {

View File

@ -24,27 +24,29 @@ const (
MIMEYAML = "application/x-yaml"
)
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.
type Binding interface {
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 {
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 {
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

View File

@ -22,27 +22,29 @@ const (
MIMEYAML = "application/x-yaml"
)
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.
type Binding interface {
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 {
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 {
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

View File

@ -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"

View File

@ -13,16 +13,18 @@ import (
"github.com/gin-gonic/gin/internal/json"
)
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.
var EnableDecoderUseNumber = false
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 = false
)
type jsonBinding struct{}

View File

@ -30,14 +30,16 @@ const (
ErrorTypeNu = 2
)
type (
// Error represents a error's specification.
type Error struct {
Error struct {
Err error
Type ErrorType
Meta interface{}
}
type errorMsgs []*Error
errorMsgs []*Error
)
var _ error = &Error{}

6
fs.go
View File

@ -9,13 +9,15 @@ import (
"os"
)
type onlyFilesFS struct {
type (
onlyFilesFS struct {
fs http.FileSystem
}
type neuteredReaddirFile struct {
neuteredReaddirFile struct {
http.File
}
)
// Dir returns a http.FileSystem that can be used by http.FileServer(). It is used internally
// in router.Static().

12
gin.go
View File

@ -27,11 +27,13 @@ var (
var defaultPlatform string
type (
// HandlerFunc defines the handler used by gin middleware as return value.
type HandlerFunc func(*Context)
HandlerFunc func(*Context)
// HandlersChain defines a HandlerFunc array.
type HandlersChain []HandlerFunc
HandlersChain []HandlerFunc
)
// Last returns the last handler in the chain. ie. the last handler is the main one.
func (c HandlersChain) Last() HandlerFunc {
@ -41,8 +43,9 @@ func (c HandlersChain) Last() HandlerFunc {
return nil
}
type (
// RouteInfo represents a request route's specification which contains method and path and its handler.
type RouteInfo struct {
RouteInfo struct {
Method string
Path string
Handler string
@ -50,7 +53,8 @@ type RouteInfo struct {
}
// RoutesInfo defines a RouteInfo array.
type RoutesInfo []RouteInfo
RoutesInfo []RouteInfo
)
// Trusted platforms
const (

View File

@ -35,8 +35,9 @@ const (
var consoleColorMode = autoColor
type (
// LoggerConfig defines the config for Logger middleware.
type LoggerConfig struct {
LoggerConfig struct {
// Optional. Default value is gin.defaultLogFormatter
Formatter LogFormatter
@ -50,10 +51,10 @@ type LoggerConfig struct {
}
// LogFormatter gives the signature of the formatter function passed to LoggerWithFormatter
type LogFormatter func(params LogFormatterParams) string
LogFormatter func(params LogFormatterParams) string
// LogFormatterParams is the structure any formatter will be handed when time to log comes
type LogFormatterParams struct {
LogFormatterParams struct {
Request *http.Request
// TimeStamp shows the time after the server returns a response.
@ -77,6 +78,7 @@ type LogFormatterParams struct {
// 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 {

View File

@ -29,6 +29,7 @@ const (
testCode
)
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
@ -36,10 +37,11 @@ const (
// To support coloring in Windows use:
// import "github.com/mattn/go-colorable"
// gin.DefaultWriter = colorable.NewColorableStdout()
var DefaultWriter io.Writer = os.Stdout
DefaultWriter io.Writer = os.Stdout
// DefaultErrorWriter is the default io.Writer used by Gin to debug errors
var DefaultErrorWriter io.Writer = os.Stderr
DefaultErrorWriter io.Writer = os.Stderr
)
var (
ginMode = debugCode

View File

@ -16,8 +16,9 @@ const (
defaultStatus = http.StatusOK
)
type (
// ResponseWriter ...
type ResponseWriter interface {
ResponseWriter interface {
http.ResponseWriter
http.Hijacker
http.Flusher
@ -43,11 +44,12 @@ type ResponseWriter interface {
Pusher() http.Pusher
}
type responseWriter struct {
responseWriter struct {
http.ResponseWriter
size int
status int
}
)
var _ ResponseWriter = &responseWriter{}

View File

@ -16,14 +16,15 @@ var (
regEnLetter = regexp.MustCompile("^[A-Z]+$")
)
type (
// IRouter defines all router handle interface includes single and group router.
type IRouter interface {
IRouter interface {
IRoutes
Group(string, ...HandlerFunc) *RouterGroup
}
// IRoutes defines all router handle interface.
type IRoutes interface {
IRoutes interface {
Use(...HandlerFunc) IRoutes
Handle(string, string, ...HandlerFunc) IRoutes
@ -43,12 +44,13 @@ type IRoutes interface {
// RouterGroup is used internally to configure router, a RouterGroup is associated with
// a prefix and an array of handlers (middleware).
type RouterGroup struct {
RouterGroup struct {
Handlers HandlersChain
basePath string
engine *Engine
root bool
}
)
var _ IRouter = &RouterGroup{}

View File

@ -19,8 +19,9 @@ var (
strStar = []byte("*")
)
type (
// Param is a single URL parameter, consisting of a key and a value.
type Param struct {
Param struct {
Key string
Value string
}
@ -28,7 +29,8 @@ type Param struct {
// 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 []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 .