Skip to content

Commit

Permalink
Added support for experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
vivekmittal committed Aug 13, 2021
1 parent 7a9f95f commit b2bc197
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
16 changes: 13 additions & 3 deletions jsend.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,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 Down Expand Up @@ -134,6 +135,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 @@ -190,6 +196,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)
r.Header().Set("Content-Length", strconv.Itoa(len(j)))

Expand Down

0 comments on commit b2bc197

Please sign in to comment.