-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_structs.go
170 lines (141 loc) · 4.65 KB
/
data_structs.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package jsonapi
import (
"bytes"
"encoding/json"
"errors"
"net/url"
"regexp"
"strings"
)
var objectSuffix = []byte("{")
var arraySuffix = []byte("[")
// A Document represents a JSON API document as specified here: http://jsonapi.org.
type Document struct {
Links *Links `json:"links,omitempty"`
Data *DataContainer `json:"data"`
Included []Data `json:"included,omitempty"`
Meta map[string]interface{} `json:"meta,omitempty"`
}
// A DataContainer is used to marshal and unmarshal single objects and arrays
// of objects.
type DataContainer struct {
DataObject *Data
DataArray []Data
}
// UnmarshalJSON unmarshals the JSON-encoded data to the DataObject field if the
// root element is an object or to the DataArray field for arrays.
func (c *DataContainer) UnmarshalJSON(payload []byte) error {
if bytes.HasPrefix(payload, objectSuffix) {
return json.Unmarshal(payload, &c.DataObject)
}
if bytes.HasPrefix(payload, arraySuffix) {
return json.Unmarshal(payload, &c.DataArray)
}
return errors.New("expected a JSON encoded object or array")
}
// MarshalJSON returns the JSON encoding of the DataArray field or the DataObject
// field. It will return "null" if neither of them is set.
func (c *DataContainer) MarshalJSON() ([]byte, error) {
if c.DataArray != nil {
return json.Marshal(c.DataArray)
}
return json.Marshal(c.DataObject)
}
// Links is a general struct for document links and relationship links.
type Links struct {
Self string `json:"self,omitempty"`
Related string `json:"related,omitempty"`
First string `json:"first,omitempty"`
Previous string `json:"prev,omitempty"`
Next string `json:"next,omitempty"`
Last string `json:"last,omitempty"`
}
// Data is a general struct for document data and included data.
type Data struct {
Type string `json:"type"`
ID string `json:"id"`
Attributes json.RawMessage `json:"attributes"`
Relationships map[string]Relationship `json:"relationships,omitempty"`
Links *Links `json:"links,omitempty"`
}
// Relationship contains reference IDs to the related structs
type Relationship struct {
Links *Links `json:"links,omitempty"`
Data *RelationshipDataContainer `json:"data,omitempty"`
Meta map[string]interface{} `json:"meta,omitempty"`
}
// A RelationshipDataContainer is used to marshal and unmarshal single relationship
// objects and arrays of relationship objects.
type RelationshipDataContainer struct {
DataObject *RelationshipData
DataArray []RelationshipData
}
// UnmarshalJSON unmarshals the JSON-encoded data to the DataObject field if the
// root element is an object or to the DataArray field for arrays.
func (c *RelationshipDataContainer) UnmarshalJSON(payload []byte) error {
if bytes.HasPrefix(payload, objectSuffix) {
// payload is an object
return json.Unmarshal(payload, &c.DataObject)
}
if bytes.HasPrefix(payload, arraySuffix) {
// payload is an array
return json.Unmarshal(payload, &c.DataArray)
}
return errors.New("Invalid json for relationship data array/object")
}
// MarshalJSON returns the JSON encoding of the DataArray field or the DataObject
// field. It will return "null" if neither of them is set.
func (c *RelationshipDataContainer) MarshalJSON() ([]byte, error) {
if c.DataArray != nil {
return json.Marshal(c.DataArray)
}
return json.Marshal(c.DataObject)
}
// RelationshipData represents one specific reference ID.
type RelationshipData struct {
Type string `json:"type"`
ID string `json:"id"`
}
type CustomObject struct {
Fields []string
Object interface{}
}
type FilterFields map[string][]string
func (f FilterFields) ParseQuery(q url.Values) {
rpm := regexp.MustCompile(`(?i)^fields\[([^\]]+)]$`)
for k, v := range q {
matches := rpm.FindStringSubmatch(k)
if len(matches) > 0 {
f[matches[1]] = strings.Split(strings.Join(v, ","), ",")
}
}
}
type ObjectAttributes map[string]interface{}
func (co CustomObject) JSONToStruct() map[string]string {
rpm := regexp.MustCompile(`(?i)^([^,]+)(,|$)`)
res := map[string]string{}
ref := getType(co.Object)
for i := 0; i < ref.NumField(); i++ {
f := ref.Field(i)
tag, ok := f.Tag.Lookup("json")
if ok {
matches := rpm.FindStringSubmatch(tag)
if len(matches) > 0 && matches[1] != "-" {
res[matches[1]] = f.Name
}
}
}
return res
}
func (co CustomObject) MarshalJSON() ([]byte, error) {
obj := ObjectAttributes{}
dict := co.JSONToStruct()
ref := getValue(co.Object)
for _, f := range co.Fields {
if dict[f] != "" {
obj[f] = ref.FieldByName(dict[f]).Interface()
}
}
b, err := json.Marshal(&obj)
return b, err
}