diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 848ec4d..2f1d6d9 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -4,7 +4,7 @@ jobs: test: strategy: matrix: - go-version: [1.14.x,1.13.x] + go-version: [1.15.x,1.14.x,1.13.x] platform: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.platform }} steps: @@ -25,7 +25,7 @@ jobs: uses: actions/checkout@v2 - name: Lint - if: matrix.platform == 'ubuntu-latest' && matrix.go-version == '1.14.x' + if: matrix.platform == 'ubuntu-latest' && matrix.go-version == '1.15.x' run: | export PATH=$PATH:$(go env GOPATH)/bin # temporary fix. See https://github.com/actions/setup-go/issues/14 go get -u golang.org/x/lint/golint diff --git a/README.md b/README.md index c1f6e01..4ecca5c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ package pure ============ -![Project status](https://img.shields.io/badge/version-5.1.0-green.svg) +![Project status](https://img.shields.io/badge/version-5.2.0-green.svg) [![Build Status](https://travis-ci.org/go-playground/pure.svg?branch=master)](https://travis-ci.org/go-playground/pure) [![Coverage Status](https://coveralls.io/repos/github/go-playground/pure/badge.svg?branch=master)](https://coveralls.io/github/go-playground/pure?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/pure)](https://goreportcard.com/report/github.com/go-playground/pure) diff --git a/go.mod b/go.mod index a4d5a9e..fa8668b 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,7 @@ module github.com/go-playground/pure/v5 require ( github.com/go-playground/assert/v2 v2.0.1 - github.com/go-playground/pkg/v5 v5.1.0 + github.com/go-playground/pkg/v5 v5.2.0 ) -go 1.14 +go 1.15 diff --git a/go.sum b/go.sum index bfb2f56..6df4965 100644 --- a/go.sum +++ b/go.sum @@ -12,3 +12,5 @@ github.com/go-playground/pkg/v4 v4.0.0 h1:0lTr9H8RyCwom4TcfNhMCNvlTlFuin7uUlXcZQ github.com/go-playground/pkg/v4 v4.0.0/go.mod h1:TLowM3d3a/m04JlHK/zM6Ia8zf8+0C/9pTwhDEUqKO0= github.com/go-playground/pkg/v5 v5.1.0 h1:odCGeJgAQFjoU8eMfT0jjqeMoa4KspymUIq+vi5NpEk= github.com/go-playground/pkg/v5 v5.1.0/go.mod h1:0380E0lsFB1POWFypOLL8tX2f0O1OBCBpSdVmEy4mg0= +github.com/go-playground/pkg/v5 v5.2.0 h1:rjauo+ugKwbpX/wxGdhLQHMba2PMwtOXhB5HdV9l3Ow= +github.com/go-playground/pkg/v5 v5.2.0/go.mod h1:4JbhbKhH362Z8RQ7XBVlvysNbGUNgiMVo2Iuyy36qhc= diff --git a/helpers.go b/helpers.go index 814fd2b..8a381f9 100644 --- a/helpers.go +++ b/helpers.go @@ -45,6 +45,15 @@ func ClientIP(r *http.Request) (clientIP string) { return httpext.ClientIP(r) } +// +// JSONStream uses json.Encoder to stream the JSON reponse body. +// +// This differs from the JSON helper which unmarshalls into memory first allowing the capture of JSON encoding errors. +// +func JSONStream(w http.ResponseWriter, status int, i interface{}) error { + return httpext.JSONStream(w, status, i) +} + // JSON marshals provided interface + returns JSON + status code func JSON(w http.ResponseWriter, status int, i interface{}) error { return httpext.JSON(w, status, i) diff --git a/helpers_test.go b/helpers_test.go index e43e9ce..54d9158 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -506,6 +506,11 @@ func TestJSON(t *testing.T) { p := New() p.Use(Gzip2) + p.Get("/jsonstream", func(w http.ResponseWriter, r *http.Request) { + if err := JSONStream(w, http.StatusOK, zombie{1, "Patient Zero"}); err != nil { + panic(err) + } + }) p.Get("/json", func(w http.ResponseWriter, r *http.Request) { if err := JSON(w, http.StatusOK, zombie{1, "Patient Zero"}); err != nil { panic(err) @@ -535,10 +540,18 @@ func TestJSON(t *testing.T) { hf := p.Serve() - r, _ := http.NewRequest(http.MethodGet, "/json", nil) + r, _ := http.NewRequest(http.MethodGet, "/jsonstream", nil) w := httptest.NewRecorder() hf.ServeHTTP(w, r) + Equal(t, w.Code, http.StatusOK) + Equal(t, w.Header().Get(httpext.ContentType), httpext.ApplicationJSON) + Equal(t, w.Body.String(), jsonData+"\n") + + r, _ = http.NewRequest(http.MethodGet, "/json", nil) + w = httptest.NewRecorder() + hf.ServeHTTP(w, r) + Equal(t, w.Code, http.StatusOK) Equal(t, w.Header().Get(httpext.ContentType), httpext.ApplicationJSON) Equal(t, w.Body.String(), jsonData)