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

Task binding cleanup #2039

Merged
merged 2 commits into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
go-version: [1.13.x, 1.14.x]
go-version: [1.14.x, 1.15.x]
os: [macos-latest, windows-latest, ubuntu-latest]
env:
GO111MODULE: on
Expand Down
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
7 changes: 5 additions & 2 deletions render/js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ func Test_JavaScript_HTML_Partial(t *testing.T) {
bb := &bytes.Buffer{}

r.NoError(h.Render(bb, Data{}))
pre := `let a = "\x3Cdiv`
r.True(strings.HasPrefix(bb.String(), pre))
r.Contains(bb.String(), `id`)
r.Contains(bb.String(), `foo`)

// To check it has escaped the partial
r.NotContains(bb.String(), `<div id="foo">`)
}