Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Task binding cleanup (#2039)
Browse files Browse the repository at this point in the history
* moving to latest 2 go versions

* cleaning up the decoder on the binding package
  • Loading branch information
paganotoni authored Sep 4, 2020
1 parent 39711ae commit e44a95d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
25 changes: 20 additions & 5 deletions binding/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package binding

import (
"net/http"
"time"

"github.com/gobuffalo/buffalo/binding/decoders"
"github.com/gobuffalo/nulls"
"github.com/monoculum/formam"
)

Expand All @@ -14,16 +16,16 @@ var (
// information on how this impacts file uploads.
MaxFileMemory int64 = 5 * 1024 * 1024

formDecoder = formam.NewDecoder(&formam.DecoderOptions{
TagName: "form",
IgnoreUnknownKeys: true,
})
// formDecoder (formam) that will be used across ContentTypeBinders
formDecoder = buildFormDecoder()

// BaseRequestBinder is an instance of the requestBinder, it comes with preconfigured
// content type binders for HTML, JSON, XML and Files, as well as custom types decoders
// for time.Time and nulls.Time
BaseRequestBinder = NewRequestBinder(
NewHTMLContentTypeBinder(formDecoder),
HTMLContentTypeBinder{
decoder: formDecoder,
},
JSONContentTypeBinder{},
XMLRequestTypeBinder{},
FileRequestTypeBinder{
Expand All @@ -32,6 +34,19 @@ var (
)
)

// buildFormDecoder that will be used in the package. This method adds some custom decoders for time.Time and nulls.Time.
func buildFormDecoder() *formam.Decoder {
decoder := formam.NewDecoder(&formam.DecoderOptions{
TagName: "form",
IgnoreUnknownKeys: true,
})

decoder.RegisterCustomType(decoders.TimeDecoderFn(), []interface{}{time.Time{}}, nil)
decoder.RegisterCustomType(decoders.NullTimeDecoderFn(), []interface{}{nulls.Time{}}, nil)

return decoder
}

// RegisterTimeFormats allows to add custom time layouts that
// the binder will be able to use for decoding.
func RegisterTimeFormats(layouts ...string) {
Expand Down
14 changes: 0 additions & 14 deletions binding/html_content_type_binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package binding

import (
"net/http"
"time"

"github.com/gobuffalo/buffalo/binding/decoders"
"github.com/gobuffalo/nulls"
"github.com/monoculum/formam"
)

Expand All @@ -14,17 +11,6 @@ type HTMLContentTypeBinder struct {
decoder *formam.Decoder
}

// NewHTMLContentTypeBinder returns an instance of HTMLContentTypeBinder with
// custom type decoders registered for Time and nulls.Time
func NewHTMLContentTypeBinder(decoder *formam.Decoder) HTMLContentTypeBinder {
decoder.RegisterCustomType(decoders.TimeDecoderFn(), []interface{}{time.Time{}}, nil)
decoder.RegisterCustomType(decoders.NullTimeDecoderFn(), []interface{}{nulls.Time{}}, nil)

return HTMLContentTypeBinder{
decoder: decoder,
}
}

// ContentTypes that will be used to identify HTML requests
func (ht HTMLContentTypeBinder) ContentTypes() []string {
return []string{
Expand Down

0 comments on commit e44a95d

Please sign in to comment.