segmentio / ksuid

K-Sortable Globally Unique IDs
https://segment.com/blog/a-brief-history-of-the-uuid/
MIT License
4.82k stars 174 forks source link

Support for gin query param unmarshaling? #82

Open APILLSBURY opened 1 month ago

APILLSBURY commented 1 month ago

Gin just added a new interface called BindUnmarshaler that allows types to define a UnmarshalParam method which lets them be used as query parameters.

Implementing this would allow binding KSUID API query params like this:

type queryParams struct {
  ID KSUID `form:"id"`
}

route.GET("/test", func(ctx *gin.Context) {
  var params queryParams{}
  _ = ctx.BindQuery(&params)
 entity, err := GetEntity(params.ID)
 ...
})

I think all that is necessary is adding this method:

func (i *KSUID) UnmarshalParam(s string) error {
    return i.UnmarshalText([]byte(s))
}

I'm happy to open a PR for it if that would be helpful.

APILLSBURY commented 1 month ago

Alternatively, adding an UnmarshalJSON method would solve this