@@ -36,44 +36,78 @@ type Control struct {
3636 // Code of HTTP status
3737 code int
3838
39- // CompactJSON propery defines JSON output format (default is not compact)
39+ // compactJSON propery defines JSON output format (default is not compact)
4040 compactJSON bool
4141
42- // Params is set of parameters
43- Params []Param
42+ // if used, json header shows meta data
43+ useMetaData bool
44+
45+ // header with metadata
46+ header Header
47+
48+ // errors
49+ errorHeader ErrorHeader
50+
51+ // params is set of key/value parameters
52+ params []Param
4453
4554 // timer used to calculate a elapsed time for handler and writing it in a response
4655 timer time.Time
4756}
4857
4958// Param is a URL parameter which represents as key and value.
5059type Param struct {
51- Key string
52- Value string
60+ Key string `json:"key,omitempty"`
61+ Value string `json:"value,omitempty"`
5362}
5463
55- // Header is used to prepare a JSON header with duration triggered by UserTimer() method
64+ // Header is used to prepare a JSON header with meta data
5665type Header struct {
57- Duration time.Duration `json:"duration"`
58- Took string `json:"took"`
59- Data interface {} `json:"data"`
66+ Duration time.Duration `json:"duration,omitempty"`
67+ Took string `json:"took,omitempty"`
68+ APIVersion string `json:"apiVersion,omitempty"`
69+ Context string `json:"context,omitempty"`
70+ ID string `json:"id,omitempty"`
71+ Method string `json:"method,omitempty"`
72+ Params interface {} `json:"params,omitempty"`
73+ Data interface {} `json:"data,omitempty"`
74+ Error interface {} `json:"error,omitempty"`
75+ }
76+
77+ // ErrorHeader contains error code, message and array of specified error reports
78+ type ErrorHeader struct {
79+ Code uint16 `json:"code,omitempty"`
80+ Message string `json:"message,omitempty"`
81+ Errors []Error `json:"errors,omitempty"`
82+ }
83+
84+ // Error report format
85+ type Error struct {
86+ Domain string `json:"domain,omitempty"`
87+ Reason string `json:"reason,omitempty"`
88+ Message string `json:"message,omitempty"`
89+ Location string `json:"location,omitempty"`
90+ LocationType string `json:"locationType,omitempty"`
91+ ExtendedHelp string `json:"extendedHelp,omitempty"`
92+ SendReport string `json:"sendReport,omitempty"`
6093}
6194
6295// Get returns the first value associated with the given name.
6396// If there are no values associated with the key, an empty string is returned.
6497func (c * Control ) Get (name string ) string {
65- for idx := range c .Params {
66- if c .Params [idx ].Key == name {
67- return c .Params [idx ].Value
98+ for idx := range c .params {
99+ if c .params [idx ].Key == name {
100+ return c .params [idx ].Value
68101 }
69102 }
70103
71104 return c .Request .URL .Query ().Get (name )
72105}
73106
74107// Set adds new parameters which represents as set of key/value.
75- func (c * Control ) Set (params []Param ) {
76- c .Params = append (c .Params , params ... )
108+ func (c * Control ) Set (params ... Param ) * Control {
109+ c .params = append (c .params , params ... )
110+ return c
77111}
78112
79113// Code assigns http status code, which returns on http request
@@ -85,12 +119,68 @@ func (c *Control) Code(code int) *Control {
85119}
86120
87121// CompactJSON change JSON output format (default mode is false)
88- func (c * Control ) CompactJSON (mode bool ) {
122+ func (c * Control ) CompactJSON (mode bool ) * Control {
89123 c .compactJSON = mode
124+ return c
125+ }
126+
127+ // UseMetaData shows meta data in JSON Header
128+ func (c * Control ) UseMetaData () * Control {
129+ c .useMetaData = true
130+ return c
131+ }
132+
133+ // APIVersion add API version meta data
134+ func (c * Control ) APIVersion (version string ) * Control {
135+ c .useMetaData = true
136+ c .header .APIVersion = version
137+ return c
138+ }
139+
140+ // Context add context meta data
141+ func (c * Control ) Context (context string ) * Control {
142+ c .useMetaData = true
143+ c .header .Context = context
144+ return c
145+ }
146+
147+ // ID add id meta data
148+ func (c * Control ) ID (id string ) * Control {
149+ c .useMetaData = true
150+ c .header .ID = id
151+ return c
152+ }
153+
154+ // Method add method meta data
155+ func (c * Control ) Method (method string ) * Control {
156+ c .useMetaData = true
157+ c .header .Method = method
158+ return c
159+ }
160+
161+ // SetParams add params meta data in alternative format
162+ func (c * Control ) SetParams (params interface {}) * Control {
163+ c .useMetaData = true
164+ c .header .Params = params
165+ return c
166+ }
167+
168+ func (c * Control ) SetError (code uint16 , message string ) * Control {
169+ c .useMetaData = true
170+ c .errorHeader .Code = code
171+ c .errorHeader .Message = message
172+ return c
173+ }
174+
175+ func (c * Control ) AddError (errors ... Error ) * Control {
176+ c .useMetaData = true
177+ c .errorHeader .Errors = append (c .errorHeader .Errors , errors ... )
178+ return c
90179}
91180
92181// UseTimer allow caalculate elapsed time of request handling
93182func (c * Control ) UseTimer () {
183+ c .useMetaData = true
94184 c .timer = time .Now ()
95185}
96186
@@ -106,9 +196,20 @@ func (c *Control) Body(data interface{}) {
106196 c .Writer .Header ().Add ("Content-type" , MIMETEXT )
107197 }
108198 } else {
109- if ! c .timer .IsZero () {
110- took := time .Now ()
111- data = & Header {Duration : took .Sub (c .timer ), Took : took .Sub (c .timer ).String (), Data : data }
199+ if c .useMetaData {
200+ c .header .Data = data
201+ if ! c .timer .IsZero () {
202+ took := time .Now ()
203+ c .header .Duration = took .Sub (c .timer )
204+ c .header .Took = took .Sub (c .timer ).String ()
205+ }
206+ if c .header .Params == nil && len (c .params ) > 0 {
207+ c .header .Params = c .params
208+ }
209+ if c .errorHeader .Code != 0 || c .errorHeader .Message != "" || len (c .errorHeader .Errors ) > 0 {
210+ c .header .Error = c .errorHeader
211+ }
212+ data = c .header
112213 }
113214 var err error
114215 if c .compactJSON {
0 commit comments