Skip to content

Commit 933f152

Browse files
author
Dean Karn
authored
Add json stream helper (#12)
1 parent 20e7136 commit 933f152

File tree

6 files changed

+30
-6
lines changed

6 files changed

+30
-6
lines changed

.github/workflows/go.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ jobs:
44
test:
55
strategy:
66
matrix:
7-
go-version: [1.14.x,1.13.x]
7+
go-version: [1.15.x,1.14.x,1.13.x]
88
platform: [ubuntu-latest, macos-latest, windows-latest]
99
runs-on: ${{ matrix.platform }}
1010
steps:
@@ -25,7 +25,7 @@ jobs:
2525
uses: actions/checkout@v2
2626

2727
- name: Lint
28-
if: matrix.platform == 'ubuntu-latest' && matrix.go-version == '1.14.x'
28+
if: matrix.platform == 'ubuntu-latest' && matrix.go-version == '1.15.x'
2929
run: |
3030
export PATH=$PATH:$(go env GOPATH)/bin # temporary fix. See https://github.com/actions/setup-go/issues/14
3131
go get -u golang.org/x/lint/golint

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package pure
22
============
3-
<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)
3+
<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)
44
[![Build Status](https://travis-ci.org/go-playground/pure.svg?branch=master)](https://travis-ci.org/go-playground/pure)
55
[![Coverage Status](https://coveralls.io/repos/github/go-playground/pure/badge.svg?branch=master)](https://coveralls.io/github/go-playground/pure?branch=master)
66
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/pure)](https://goreportcard.com/report/github.com/go-playground/pure)

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module github.com/go-playground/pure/v5
22

33
require (
44
github.com/go-playground/assert/v2 v2.0.1
5-
github.com/go-playground/pkg/v5 v5.1.0
5+
github.com/go-playground/pkg/v5 v5.2.0
66
)
77

8-
go 1.14
8+
go 1.15

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ github.com/go-playground/pkg/v4 v4.0.0 h1:0lTr9H8RyCwom4TcfNhMCNvlTlFuin7uUlXcZQ
1212
github.com/go-playground/pkg/v4 v4.0.0/go.mod h1:TLowM3d3a/m04JlHK/zM6Ia8zf8+0C/9pTwhDEUqKO0=
1313
github.com/go-playground/pkg/v5 v5.1.0 h1:odCGeJgAQFjoU8eMfT0jjqeMoa4KspymUIq+vi5NpEk=
1414
github.com/go-playground/pkg/v5 v5.1.0/go.mod h1:0380E0lsFB1POWFypOLL8tX2f0O1OBCBpSdVmEy4mg0=
15+
github.com/go-playground/pkg/v5 v5.2.0 h1:rjauo+ugKwbpX/wxGdhLQHMba2PMwtOXhB5HdV9l3Ow=
16+
github.com/go-playground/pkg/v5 v5.2.0/go.mod h1:4JbhbKhH362Z8RQ7XBVlvysNbGUNgiMVo2Iuyy36qhc=

helpers.go

+9
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ func ClientIP(r *http.Request) (clientIP string) {
4545
return httpext.ClientIP(r)
4646
}
4747

48+
//
49+
// JSONStream uses json.Encoder to stream the JSON reponse body.
50+
//
51+
// This differs from the JSON helper which unmarshalls into memory first allowing the capture of JSON encoding errors.
52+
//
53+
func JSONStream(w http.ResponseWriter, status int, i interface{}) error {
54+
return httpext.JSONStream(w, status, i)
55+
}
56+
4857
// JSON marshals provided interface + returns JSON + status code
4958
func JSON(w http.ResponseWriter, status int, i interface{}) error {
5059
return httpext.JSON(w, status, i)

helpers_test.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,11 @@ func TestJSON(t *testing.T) {
506506

507507
p := New()
508508
p.Use(Gzip2)
509+
p.Get("/jsonstream", func(w http.ResponseWriter, r *http.Request) {
510+
if err := JSONStream(w, http.StatusOK, zombie{1, "Patient Zero"}); err != nil {
511+
panic(err)
512+
}
513+
})
509514
p.Get("/json", func(w http.ResponseWriter, r *http.Request) {
510515
if err := JSON(w, http.StatusOK, zombie{1, "Patient Zero"}); err != nil {
511516
panic(err)
@@ -535,10 +540,18 @@ func TestJSON(t *testing.T) {
535540

536541
hf := p.Serve()
537542

538-
r, _ := http.NewRequest(http.MethodGet, "/json", nil)
543+
r, _ := http.NewRequest(http.MethodGet, "/jsonstream", nil)
539544
w := httptest.NewRecorder()
540545
hf.ServeHTTP(w, r)
541546

547+
Equal(t, w.Code, http.StatusOK)
548+
Equal(t, w.Header().Get(httpext.ContentType), httpext.ApplicationJSON)
549+
Equal(t, w.Body.String(), jsonData+"\n")
550+
551+
r, _ = http.NewRequest(http.MethodGet, "/json", nil)
552+
w = httptest.NewRecorder()
553+
hf.ServeHTTP(w, r)
554+
542555
Equal(t, w.Code, http.StatusOK)
543556
Equal(t, w.Header().Get(httpext.ContentType), httpext.ApplicationJSON)
544557
Equal(t, w.Body.String(), jsonData)

0 commit comments

Comments
 (0)