Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add content length header in jsend #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
18 changes: 15 additions & 3 deletions jsend.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ const (
)

const (
fieldMsg = "message"
fieldData = "data"
fieldStatus = "status"
fieldMsg = "message"
fieldData = "data"
fieldStatus = "status"
fieldExperiments = "experiments"
)

// Wrap wraps given http.ResponseWriter and returns a response object which
Expand All @@ -101,6 +102,8 @@ type JResponseWriter interface {

Data(interface{}) JResponseWriter

Experiments(interface{}) JResponseWriter

Message(string) JResponseWriter

Status(int) JResponseWriter
Expand Down Expand Up @@ -133,6 +136,11 @@ func (r *Response) Data(data interface{}) JResponseWriter {
return r.Field(fieldData, data)
}

// Data sets response's "data" field with given value.
func (r *Response) Experiments(experiments interface{}) JResponseWriter {
return r.Field(fieldExperiments, experiments)
}

// Message sets response's "message" field with given value.
func (r *Response) Message(msg string) JResponseWriter {
return r.Field(fieldMsg, msg)
Expand Down Expand Up @@ -189,6 +197,10 @@ func (r *Response) Send() (int, error) {
r.Data([]byte(nil))
}

if _, hasExperiments := r.fields[fieldExperiments]; !hasExperiments {
r.Experiments([]byte(nil))
}

j, err := json.Marshal(r.fields)

if err != nil {
Expand Down