Bind custom unmarshaler
Это содержимое пока не доступно на вашем языке.
To override Gin’s default binding logic, define a function on your type that satisfies the encoding.TextUnmarshaler interface from the Go standard library. Then specify parser=encoding.TextUnmarshaler in the uri/form tag of the field being bound.
package main
import ( "encoding" "strings"
"github.com/gin-gonic/gin")
type Birthday string
func (b *Birthday) UnmarshalText(text []byte) error { *b = Birthday(strings.Replace(string(text), "-", "/", -1)) return nil}
var _ encoding.TextUnmarshaler = (*Birthday)(nil)
func main() { route := gin.Default() var request struct { Birthday Birthday `form:"birthday,parser=encoding.TextUnmarshaler"` Birthdays []Birthday `form:"birthdays,parser=encoding.TextUnmarshaler" collection_format:"csv"` BirthdaysDefault []Birthday `form:"birthdaysDef,default=2020-09-01;2020-09-02,parser=encoding.TextUnmarshaler" collection_format:"csv"` } route.GET("/test", func(ctx *gin.Context) { _ = ctx.BindQuery(&request) ctx.JSON(200, request) }) _ = route.Run(":8088")}Test it with:
curl 'localhost:8088/test?birthday=2000-01-01&birthdays=2000-01-01,2000-01-02'Result:
{"Birthday":"2000/01/01","Birthdays":["2000/01/01","2000/01/02"],"BirthdaysDefault":["2020/09/01","2020/09/02"]}Note: If
parser=encoding.TextUnmarshaleris specified for a type that does not implementencoding.TextUnmarshaler, Gin will ignore it and proceed with its default binding logic.
Using BindUnmarshaler
If a type already implements encoding.TextUnmarshaler but you want to customize how Gin binds the type differently (e.g., to change what error message is returned), you can implement the dedicated BindUnmarshaler interface instead.
package main
import ( "strings"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin/binding")
type Birthday string
func (b *Birthday) UnmarshalParam(param string) error { *b = Birthday(strings.Replace(param, "-", "/", -1)) return nil}
var _ binding.BindUnmarshaler = (*Birthday)(nil)
func main() { route := gin.Default() var request struct { Birthday Birthday `form:"birthday"` Birthdays []Birthday `form:"birthdays" collection_format:"csv"` BirthdaysDefault []Birthday `form:"birthdaysDef,default=2020-09-01;2020-09-02" collection_format:"csv"` } route.GET("/test", func(ctx *gin.Context) { _ = ctx.BindQuery(&request) ctx.JSON(200, request) }) _ = route.Run(":8088")}Note: If a type implements both
encoding.TextUnmarshalerandBindUnmarshaler, Gin will useBindUnmarshalerby default unless you specifyparser=encoding.TextUnmarshalerin the binding tag.