Categorygithub.com/chenxiao1990/gin
modulepackage
1.4.3
Repository: https://github.com/chenxiao1990/gin.git
Documentation: pkg.go.dev

# README

fork 的gin

最终效果

uid在前端可以传 字符串,int 。如字符串无法转换成int会解析失败

post json 传参数
{
"uid":["111",222]
}

gin解析
type AA struct {
	UID []int `json:"uid"`
}
var a AA
err := c.ShouldBind(&a)

为什么要改

1. string to int

因为老项目中使用的动态语言php,好多uid的类型是 123 也是"123"

gin解析到struct有问题,因为不能更改所有老项目代码,所以只能改gin

找到gin的json decoder 设置在 gin/internal/json 这个“内部包”中,无法修改,只能fork了

2. time.Time format

time.Time 格式化成 "2006-01-02 15:03:04"

修改内容

实际上修改的是标准库的 encoding/json包的内容

但是为了不影响标准库,我拷贝了一份json放在了gin/internal/json中

然后让gin使用我修改后的json包。

代码修改:

包名github.com/gin-gonic/gin 替换成 github.com/chenxiao1990/gin
gin/internal/json/json.go中
import "github.com/chenxiao1990/gin/internal/json/json"

gin/internal/json/json/decode.go  946行增加

if len(s) == 0 {
	v.SetInt(0)
} else {
	n, err := strconv.ParseInt(string(s), 10, 64)
	if err != nil || v.OverflowInt(n) {
		d.saveError(&UnmarshalTypeError{Value: "string " + string(s), Type: v.Type(), Offset: int64(d.readIndex())})
		break
	}
	v.SetInt(n)
}
为了gin输出 time.Time的格式为 "2006-01-02 15:04:05"

gin/internal/json/json/encode.go  456行增加

t := reflect.TypeOf(m)
s := t.Name()
s2 := t.PkgPath()
if s2+"."+s == "time.Time" {
	timeFormart := "2006-01-02 15:04:05"
	b = make([]byte, 0, len(timeFormart)+2)
	b = append(b, '"')
	b = m.(time.Time).AppendFormat(b, timeFormart)
	b = append(b, '"')
}

后续使用可能会遇到的问题

比如你引用了 "github.com/DeanThompson/ginpprof"

会报类型错误

这种 github.com/chenxiao1990 与 github.com/gin-gonic 类型错误 都可以使用下面的方法强制转换

引用原始的gin  
import  ygin "github.com/gin-gonic/gin"

GRouter = gin.Default()
up := unsafe.Pointer(GRouter)
ginpprof.Wrapper((*ygin.Engine)(up))

# Packages

No description provided by the author
No description provided by the author
No description provided by the author

# Functions

BasicAuth returns a Basic HTTP Authorization middleware.
BasicAuthForRealm returns a Basic HTTP Authorization middleware.
Bind is a helper function for given interface object and returns a Gin middleware.
CreateTestContext returns a fresh engine and context for testing purposes.
Default returns an Engine instance with the Logger and Recovery middleware already attached.
Dir returns a http.Filesystem that can be used by http.FileServer().
DisableBindValidation closes the default validator.
DisableConsoleColor disables color output in the console.
EnableJsonDisallowUnknownFields sets true for binding.EnableDecoderDisallowUnknownFields to call the DisallowUnknownFields method on the JSON Decoder instance.
EnableJsonDecoderUseNumber sets true for binding.EnableDecoderUseNumber to call the UseNumber method on the JSON Decoder instance.
ErrorLogger returns a handlerfunc for any error type.
ErrorLoggerT returns a handlerfunc for a given error type.
ForceConsoleColor force color output in the console.
IsDebugging returns true if the framework is running in debug mode.
Logger instances a Logger middleware that will write the logs to gin.DefaultWriter.
LoggerWithConfig instance a Logger middleware with config.
LoggerWithFormatter instance a Logger middleware with the specified log format function.
LoggerWithWriter instance a Logger middleware with the specified writer buffer.
Mode returns currently gin mode.
New returns a new blank Engine instance without any middleware attached.
Recovery returns a middleware that recovers from any panics and writes a 500 if there was one.
RecoveryWithWriter returns a middleware for a given writer that recovers from any panics and writes a 500 if there was one.
SetMode sets gin mode according to input string.
WrapF is a helper function for wrapping http.HandlerFunc and returns a Gin middleware.
WrapH is a helper function for wrapping http.Handler and returns a Gin middleware.

# Constants

AuthUserKey is the cookie name for user credential in basic auth.
BindKey indicates a default bind key.
Content-Type MIME of the most common data formats.
DebugMode indicates gin mode is debug.
EnvGinMode indicates environment name for gin mode.
ErrorTypeAny indicates any other error.
ErrorTypeBind is used when Context.Bind() fails.
ErrorTypeNu indicates any other error.
ErrorTypePrivate indicates a private error.
ErrorTypePublic indicates a public error.
ErrorTypeRender is used when Context.Render() fails.
Content-Type MIME of the most common data formats.
Content-Type MIME of the most common data formats.
Content-Type MIME of the most common data formats.
Content-Type MIME of the most common data formats.
Content-Type MIME of the most common data formats.
Content-Type MIME of the most common data formats.
Content-Type MIME of the most common data formats.
Content-Type MIME of the most common data formats.
ReleaseMode indicates gin mode is release.
TestMode indicates gin mode is test.
Version is the current gin framework's version.

# Variables

DebugPrintRouteFunc indicates debug log output format.
DefaultErrorWriter is the default io.Writer used by Gin to debug errors.
DefaultWriter is the default io.Writer used by Gin for debug output and middleware output like Logger() or Recovery().

# Structs

Context is the most important part of gin.
Engine is the framework's instance, it contains the muxer, middleware and configuration settings.
Error represents a error's specification.
LogFormatterParams is the structure any formatter will be handed when time to log comes.
LoggerConfig defines the config for Logger middleware.
Negotiate contains all negotiations data.
Param is a single URL parameter, consisting of a key and a value.
RouteInfo represents a request route's specification which contains method and path and its handler.
RouterGroup is used internally to configure router, a RouterGroup is associated with a prefix and an array of handlers (middleware).

# Interfaces

IRouter defines all router handle interface includes single and group router.
IRoutes defines all router handle interface.
ResponseWriter ...

# Type aliases

Accounts defines a key/value for user/pass list of authorized logins.
ErrorType is an unsigned 64-bit error code as defined in the gin spec.
H is a shortcut for map[string]interface{}.
HandlerFunc defines the handler used by gin middleware as return value.
HandlersChain defines a HandlerFunc array.
LogFormatter gives the signature of the formatter function passed to LoggerWithFormatter.
Params is a Param-slice, as returned by the router.
RoutesInfo defines a RouteInfo array.