컨텐츠로 건너뛰기

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:

Terminal window
curl 'localhost:8088/test?birthday=2000-01-01&birthdays=2000-01-01,2000-01-02'

Result:

Terminal window
{"Birthday":"2000/01/01","Birthdays":["2000/01/01","2000/01/02"],"BirthdaysDefault":["2020/09/01","2020/09/02"]}

Note: If parser=encoding.TextUnmarshaler is specified for a type that does not implement encoding.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.TextUnmarshaler and BindUnmarshaler, Gin will use BindUnmarshaler by default unless you specify parser=encoding.TextUnmarshaler in the binding tag.