@@ -36,44 +36,78 @@ type Control struct {
36
36
// Code of HTTP status
37
37
code int
38
38
39
- // CompactJSON propery defines JSON output format (default is not compact)
39
+ // compactJSON propery defines JSON output format (default is not compact)
40
40
compactJSON bool
41
41
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
44
53
45
54
// timer used to calculate a elapsed time for handler and writing it in a response
46
55
timer time.Time
47
56
}
48
57
49
58
// Param is a URL parameter which represents as key and value.
50
59
type Param struct {
51
- Key string
52
- Value string
60
+ Key string `json:"key,omitempty"`
61
+ Value string `json:"value,omitempty"`
53
62
}
54
63
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
56
65
type 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"`
60
93
}
61
94
62
95
// Get returns the first value associated with the given name.
63
96
// If there are no values associated with the key, an empty string is returned.
64
97
func (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
68
101
}
69
102
}
70
103
71
104
return c .Request .URL .Query ().Get (name )
72
105
}
73
106
74
107
// 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
77
111
}
78
112
79
113
// Code assigns http status code, which returns on http request
@@ -85,12 +119,68 @@ func (c *Control) Code(code int) *Control {
85
119
}
86
120
87
121
// CompactJSON change JSON output format (default mode is false)
88
- func (c * Control ) CompactJSON (mode bool ) {
122
+ func (c * Control ) CompactJSON (mode bool ) * Control {
89
123
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
90
179
}
91
180
92
181
// UseTimer allow caalculate elapsed time of request handling
93
182
func (c * Control ) UseTimer () {
183
+ c .useMetaData = true
94
184
c .timer = time .Now ()
95
185
}
96
186
@@ -106,9 +196,20 @@ func (c *Control) Body(data interface{}) {
106
196
c .Writer .Header ().Add ("Content-type" , MIMETEXT )
107
197
}
108
198
} 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
112
213
}
113
214
var err error
114
215
if c .compactJSON {
0 commit comments