-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocument.go
More file actions
121 lines (97 loc) · 2.92 KB
/
Copy pathdocument.go
File metadata and controls
121 lines (97 loc) · 2.92 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package jsonapi
import (
"bytes"
"encoding/json"
"io"
"net/http"
)
// NullLink can be used as a link to encode it as a null value.
const NullLink = "NULL"
var nullBytes = []byte("null")
// Link is document link that also can be null by setting NullLink.
type Link string
// UnmarshalJSON implements the json.Unmarshaler interface.
func (l *Link) UnmarshalJSON(data []byte) error {
// handle null
if bytes.Compare(data, nullBytes) == 0 {
*l = NullLink
return nil
}
// handle string
var str string
err := json.Unmarshal(data, &str)
*l = Link(str)
return err
}
// MarshalJSON implements the json.Marshaler interface.
func (l Link) MarshalJSON() ([]byte, error) {
// handle null
if l == NullLink {
return nullBytes, nil
}
return json.Marshal(string(l))
}
// DocumentLinks are a set of links related to a documents primary data.
//
// See: http://jsonapi.org/format/#document-links.
type DocumentLinks struct {
Self Link `json:"self,omitempty"`
Related Link `json:"related,omitempty"`
First Link `json:"first,omitempty"`
Previous Link `json:"prev,omitempty"`
Next Link `json:"next,omitempty"`
Last Link `json:"last,omitempty"`
}
// A Document is the root structure of every JSON API response. It is also used
// to include relationships.
//
// See: http://jsonapi.org/format/#document-top-level.
type Document struct {
// The documents primary data in the form of a single resource or a list
// of resources.
Data *HybridResource `json:"data,omitempty"`
// A list of resources that are related to the primary data and/or other
// included resources.
Included []*Resource `json:"included,omitempty"`
// A set of links related to the primary data.
Links *DocumentLinks `json:"links,omitempty"`
// A list of errors that occurred during the request.
Errors []*Error `json:"errors,omitempty"`
// Non-standard meta-information about the document.
//
// Note: Numbers are left as strings to avoid issues with mismatching types
// when they are later assigned to a struct.
Meta Map `json:"meta,omitempty"`
}
// ParseDocument will decode a JSON API document from the passed reader.
//
// Note: If the read document contains errors the first Error will be returned
// as an error.
func ParseDocument(r io.Reader) (*Document, error) {
// TODO: Check document validity more in depth?
// prepare document
var doc Document
// prepare decoder
dec := json.NewDecoder(r)
dec.UseNumber()
// decode body
err := dec.Decode(&doc)
if err != nil {
return nil, BadRequest(err.Error())
}
// check for errors
if len(doc.Errors) > 0 {
return nil, doc.Errors[0]
}
return &doc, nil
}
// WriteResponse will write the status and supplied document to the passed
// response writer.
func WriteResponse(w http.ResponseWriter, status int, doc *Document) error {
// set content type
w.Header().Set("Content-Type", MediaType)
// write status
w.WriteHeader(status)
// write document
return json.NewEncoder(w).Encode(doc)
}