From bb886705baa1c8456bf74c77fca7cd8e53ffae79 Mon Sep 17 00:00:00 2001 From: Vivek Mittal Date: Fri, 13 Aug 2021 19:09:17 +0530 Subject: [PATCH] Added support for experiments --- .gitignore | 1 + jsend.go | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..62c8935 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ \ No newline at end of file diff --git a/jsend.go b/jsend.go index 07d64cb..abe4bfd 100644 --- a/jsend.go +++ b/jsend.go @@ -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 @@ -101,6 +102,8 @@ type JResponseWriter interface { Data(interface{}) JResponseWriter + Experiments(interface{}) JResponseWriter + Message(string) JResponseWriter Status(int) JResponseWriter @@ -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) @@ -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 {