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 f47e169 commit bb88670
Show file tree
Hide file tree
Showing 2 changed files with 16 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/
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

0 comments on commit bb88670

Please sign in to comment.