Releases: ggicci/httpin
v0.19.0
New Feature
Introduced a new directive omitempty
, which works on encoding a Go struct to an http.Request. Just think of how omitempty
works in encoding/json
package. Here in httpin, omitempty
will omit adding keys to Query, Form, Header when the corresponding value is empty. See reflect.Value.IsZero
for more details about how we judge an empty value.
Thanks to @alecsammon who made this feature availalbe for all httpin users.
v0.18.1
v0.18.0
Fixes #101
Breaking Change and Migration Guide
func DecodeTo(req *http.Request, input any, opts ...core.Option) error
API has been renamed to DecodeTo
, it will populate the given input
value instead of creating a new instance internally.
How to migrate to v0.18.0?
Since we haven't changed the signature of this function, we can migrate by replacing httpin.Decode
with httpin.DecodeTo
in your codebase.
New API
Added new func Decode[T any](req *http.Request, opts ...core.Option) (*T, error)
API, example usage:
type AddUserInput struct {
Username string `in:"form=username"`
}
if input, err := httpin.Decode[AddUserInput](req); err != nil { ... }
// input.Username
v0.17.0
#99 Added echo integration, register a "path" directive that can work with echo router.
import httpin_integration "github.com/ggicci/httpin/integration"
func init() {
e := echo.New()
httpin_integration.UseEchoRouter("path", e)
// or
httpin_integration.UseEchoPathRouter(e)
}
Thanks
v0.16.0
API Changes
Added optional parameter opts ...core.Option
to the following APIs:
httpin.Decode(req *http.Request, input any) error
->httpin.Decode(req *http.Request, input any, opts ...core.Option) error
httpin.NewRequest(method, url string, input any) (*http.Request, error)
->httpin.NewRequest(method, url string, input any, opts ...core.Option) (*http.Request, error)
httpin.NewRequestWithContext(ctx context.Context, method, url string, input any) (*http.Request, error)
->httpin.NewRequestWithContext(ctx context.Context, method, url string, input any, opts ...core.Option) (*http.Request, error)
Added a new variable httpin.Option
where is a collection of the options.
v0.15.3
v0.15.2
Register the path directive by default, but it only supports encoding function. For a complete path directive, users still need to use the integration
subpackage:
import httpin_integration "github.com/ggicci/httpin/integration"
func init() {
httpin_integration.UseGochiURLParam("gochi", chi.URLParam)
}
v0.15.1
v0.15.0
New Features
- Include encoding functionality to this package. From now on, httpin doesn't only support decoding an HTTP request to a Go struct, it supports encoding a Go struct to an HTTP request as well. Simply call
httpin.NewRequest
method. Because all the builtin directives now support encoding functions. - Introduced a new directive named
nonzero
. Which is used to cover/correct miss-use of therequired
directive, usenonzero
instead ofrequired
to ensure that the field not be a zero/empty value. - For custom struct types, you don't need to register it to httpin any more, as long as you implement the
Stringable
interface.
Breaking Changes
- Less APIs are exposed under package
httpin
. I hope the list will be limited to the following, which I would like to keep in the first stable release (v1.0.0):httpin.New
: creates acore.Core
instance;httpin.NewInput
: creates a HTTP middleware;httpin.Input
: the context key to retrieve the decoded struct instance that was injected tohttp.Request.Context
by httpin;httpin.Decode
: decode an HTTP request to a struct instance;httpin.NewRequest
: encode a struct instance to an HTTP request;httpin.NewRequestWithContext
: callhttpin.NewRequest
with acontext.Context
;httpin.File
: the type for manipulating file uploading in httpin;httpin.UploadFile
: upload file by filename;httpin.UploadStream
: upload file by stream.
- APIs to integrate with third-party packages are moved to the
integration
subpackage:httpin.UseGochiURLParam
: registers apath
directive for thegochi
routing package, usehttpin_integration.UseGochiURLParam
instead;httpin.UseGorillaMux
: registers apath
directive for thegorilla/mux
routing package, usehttpin_integration.UseGorillaMux
instead;
- All the other APIs used to extend http functions are moved to
core
subpackage and many of them have been changed because the newly introduced encoding ability:httpin.RegisterValueTypeDecoder[T]
: usehttpin_core.RegisterCoder
instead;httpin.RegisterNamedDecoder[T]
: usehttpin_core.RegisterNamedCoder[T]
instead;httpin.RegisterFileTypeDecoder[T]
: usehttpin_core.RegisterFileCoder[T]
instead;httpin.RegisterBodyDecoder
: usehttpin_core.RegisterBodyFormat
instead;httpin.ReplaceDefaultErrorHandler
: usehttpin_core.RegisterErrorHandler
instead;- DEPRECATED
httpin.ReplaceValueTypeDecoder[T]
: usehttpin_core.RegisterCoder
instead; - DEPRECATED
httpin.ReplaceNamedDecoder[T]
: usehttpin_core.RegisterNamedCoder[T]
instead; - DEPRECATED
httpin.ReplaceFileTypeDecoder[T]
: usehttpin_core.RegisterFileCoder[T]
instead; - DEPRECATED
httpin.ReplaceBodyDecoder
: usehttpin_core.RegisterBodyFormat
instead;
- httpin no longer run directives defined in nested structs, you have to either use
httpin_core.WithNestedDirectivesEnabled
option or enable it manually withhttpin_core.EnableNestedDirectives
.