Skip to content

Commit

Permalink
Add json stream helper (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
deankarn committed Nov 19, 2020
1 parent 20e7136 commit 933f152
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package pure
============
<img align="right" src="https://raw.githubusercontent.com/go-playground/pure/master/logo.png">![Project status](https://img.shields.io/badge/version-5.1.0-green.svg)
<img align="right" src="https://raw.githubusercontent.com/go-playground/pure/master/logo.png">![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)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
9 changes: 9 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 14 additions & 1 deletion helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 933f152

Please sign in to comment.