-
Notifications
You must be signed in to change notification settings - Fork 0
/
res.go
58 lines (49 loc) · 1.49 KB
/
res.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package res
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"go.bobheadxi.dev/res/base"
"github.com/go-chi/render"
)
// M is an alias for a map
type M map[string]interface{}
// KV is used for defining specific values to be unmarshalled from BaseResponse
// data
type KV struct {
Key string
Value interface{}
}
// Unmarshal reads the response and unmarshalls the BaseResponse as well any
// requested key-value pairs.
// For example:
//
// var prop = map[string]string
// api.Unmarshal(resp.Body, api.KV{Key: "prop", Value: &prop})
//
// Values provided in KV.Value MUST be explicit pointers, even if the value is
// a pointer type, ie maps and slices.
func Unmarshal(r io.Reader, kvs ...KV) (*base.Response, error) {
bytes, err := ioutil.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("could not read bytes from reader: %s", err.Error())
}
// Unmarshal data into a BaseResponse, replacing BaseResponse.Data with a
// map to preserve raw JSON data in the keys
var (
data = make(map[string]json.RawMessage)
resp = base.Response{Data: &data}
)
if err := json.Unmarshal(bytes, &resp); err != nil {
return nil, fmt.Errorf("could not unmarshal data from reader: %s", err.Error())
}
// Unmarshal all requested kv-pairs, silently ignoring errors
for _, kv := range kvs {
json.Unmarshal(data[kv.Key], kv.Value)
}
return &resp, nil
}
// R is an alias for go-chi/render.Render
func R(w http.ResponseWriter, r *http.Request, v render.Renderer) { render.Render(w, r, v) }