-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode_document.go
47 lines (38 loc) · 999 Bytes
/
encode_document.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
package jsonapi
import (
"encoding/json"
"reflect"
)
type EncodeDocument struct {
Data json.Marshaler `json:"data,omitempty"`
Errors Errors `json:"errors,omitempty"`
}
func NewSingularEncodeDocument(m interface{}) (EncodeDocument, error) {
encodable, err := ToEncodable(m)
if err != nil {
return EncodeDocument{}, err
}
return EncodeDocument{
Data: NewEncodeResourceObject(encodable),
}, nil
}
func NewMultipleEncodeDocument(m interface{}) (EncodeDocument, error) {
var resourceObjects EncodeResourceObjects
value := reflect.ValueOf(m)
for i := 0; i < value.Len(); i++ {
elem := value.Index(i)
encodable, err := ToEncodable(elem.Interface())
if err != nil {
return EncodeDocument{}, err
}
resourceObjects = append(resourceObjects, NewEncodeResourceObject(encodable))
}
return EncodeDocument{
Data: resourceObjects,
}, nil
}
func NewErrorsEncodeDocument(errors Errors) (EncodeDocument, error) {
return EncodeDocument{
Errors: errors,
}, nil
}