Skip to content

Commit 7da1495

Browse files
committed
rename method + simplify unmarshal json implementation
Signed-off-by: Augustin Husson <husson.augustin@gmail.com>
1 parent 9f98a15 commit 7da1495

1 file changed

Lines changed: 12 additions & 14 deletions

File tree

rest/api.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ import (
2727
"github.com/prometheus/common/route"
2828
)
2929

30-
func returnJSON(w http.ResponseWriter, content interface{}) {
30+
func respondJSON(w http.ResponseWriter, content interface{}) {
3131
encoder := json.NewEncoder(w)
3232

3333
err := encoder.Encode(content)
3434
if err != nil {
35-
http.Error(w, errors.Wrapf(err, "failed to write response").Error(), 500)
35+
http.Error(w, errors.Wrapf(err, "failed to write response").Error(), http.StatusInternalServerError)
3636
}
3737
}
3838

@@ -48,19 +48,17 @@ type lspData struct {
4848
}
4949

5050
func (d *lspData) UnmarshalJSON(data []byte) error {
51-
var tmp lspData
5251
type plain lspData
53-
if err := json.Unmarshal(data, (*plain)(&tmp)); err != nil {
52+
if err := json.Unmarshal(data, (*plain)(d)); err != nil {
5453
return err
5554
}
56-
if len(tmp.Expr) == 0 {
55+
if len(d.Expr) == 0 {
5756
return fmt.Errorf("PromQL expression is not specified")
5857
}
59-
*d = tmp
6058
return nil
6159
}
6260

63-
func (d *lspData) returnPosition() (protocol.Position, error) {
61+
func (d *lspData) position() (protocol.Position, error) {
6462
if d.PositionLine == nil {
6563
return protocol.Position{}, errors.New("positionLine is not specified")
6664
}
@@ -148,7 +146,7 @@ func (a *API) diagnostics(w http.ResponseWriter, r *http.Request) {
148146
items = items[:*limit]
149147
}
150148

151-
returnJSON(w, items)
149+
respondJSON(w, items)
152150
}
153151

154152
func (a *API) hover(w http.ResponseWriter, r *http.Request) {
@@ -158,7 +156,7 @@ func (a *API) hover(w http.ResponseWriter, r *http.Request) {
158156
http.Error(w, err.Error(), http.StatusInternalServerError)
159157
return
160158
}
161-
position, err := requestData.returnPosition()
159+
position, err := requestData.position()
162160
if err != nil {
163161
http.Error(w, err.Error(), http.StatusBadRequest)
164162
return
@@ -177,7 +175,7 @@ func (a *API) hover(w http.ResponseWriter, r *http.Request) {
177175
return
178176
}
179177

180-
returnJSON(w, hover)
178+
respondJSON(w, hover)
181179
}
182180

183181
func (a *API) completion(w http.ResponseWriter, r *http.Request) {
@@ -187,7 +185,7 @@ func (a *API) completion(w http.ResponseWriter, r *http.Request) {
187185
http.Error(w, err.Error(), http.StatusInternalServerError)
188186
return
189187
}
190-
position, err := requestData.returnPosition()
188+
position, err := requestData.position()
191189
if err != nil {
192190
http.Error(w, err.Error(), http.StatusBadRequest)
193191
return
@@ -212,7 +210,7 @@ func (a *API) completion(w http.ResponseWriter, r *http.Request) {
212210
items = items[:*limit]
213211
}
214212

215-
returnJSON(w, items)
213+
respondJSON(w, items)
216214
}
217215

218216
func (a *API) signature(w http.ResponseWriter, r *http.Request) {
@@ -222,7 +220,7 @@ func (a *API) signature(w http.ResponseWriter, r *http.Request) {
222220
http.Error(w, err.Error(), http.StatusInternalServerError)
223221
return
224222
}
225-
position, err := requestData.returnPosition()
223+
position, err := requestData.position()
226224
if err != nil {
227225
http.Error(w, err.Error(), http.StatusBadRequest)
228226
return
@@ -241,5 +239,5 @@ func (a *API) signature(w http.ResponseWriter, r *http.Request) {
241239
return
242240
}
243241

244-
returnJSON(w, signature)
242+
respondJSON(w, signature)
245243
}

0 commit comments

Comments
 (0)