mirror of
https://github.com/gin-gonic/gin.git
synced 2025-04-26 19:46:51 +08:00
This commit adds support for parsing decimal numbers that start with a dot (e.g. '.1') in query parameters and form data. It implements the BindUnmarshaler interface to handle this special case. Fixes #4089
32 lines
508 B
Go
32 lines
508 B
Go
package main
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin/binding"
|
|
"net/http"
|
|
)
|
|
|
|
type QueryParams struct {
|
|
Amount binding.CustomDecimal `form:"amount"`
|
|
}
|
|
|
|
func main() {
|
|
r := gin.Default()
|
|
|
|
r.GET("/amount", func(c *gin.Context) {
|
|
var params QueryParams
|
|
if err := c.BindQuery(¶ms); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"amount": params.Amount.String(),
|
|
})
|
|
})
|
|
|
|
r.Run(":8080")
|
|
}
|