forked from janivihervas/contentful-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflatten.go
246 lines (219 loc) · 5.59 KB
/
flatten.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package contentful
import (
"encoding/json"
"fmt"
"time"
)
const (
linkTypeAsset = "Asset"
linkTypeEntry = "Entry"
linkType = "Link"
)
type sys struct {
Type string `json:"type"`
LinkType string `json:"linkType"`
ID string `json:"id"`
}
type itemInfo struct {
Type string `json:"type"`
ID string `json:"id"`
ContentType struct {
Sys sys `json:"sys"`
} `json:"contentType"`
Revision int `json:"revision"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
Locale string `json:"locale"`
}
type item struct {
Sys itemInfo `json:"sys"`
Fields map[string]interface{} `json:"fields"`
}
type includes struct {
Entry []item `json:"entry"`
Asset []item `json:"asset"`
}
type searchResults struct {
Total int `json:"total"`
Skip int `json:"skip"`
Limit int `json:"limit"`
Items []item `json:"items"`
Includes includes `json:"includes"`
}
func flattenItems(includes includes, items []item) ([]map[string]interface{}, error) {
flattenedItems := make([]map[string]interface{}, len(items))
for i, item := range items {
flattenedItem, err := flattenItem(includes, item)
if err != nil {
return flattenedItems, err
}
flattenedItems[i] = flattenedItem
}
return flattenedItems, nil
}
func flattenItem(includes includes, item item) (map[string]interface{}, error) {
flattenedFields := make(map[string]interface{}, len(item.Fields))
for key, field := range item.Fields {
flattenedField, err := flattenField(includes, field)
if err != nil {
return flattenedFields, err
}
flattenedFields[key] = flattenedField
}
if item.Sys.ID != "" {
flattenedFields["contentfulId"] = item.Sys.ID
flattenedFields["contentfulContentType"] = item.Sys.ContentType.Sys.ID
flattenedFields["contentfulRevision"] = item.Sys.Revision
flattenedFields["contentfulCreatedAt"] = item.Sys.CreatedAt
flattenedFields["contentfulUpdatedAt"] = item.Sys.UpdatedAt
flattenedFields["contentfulLocale"] = item.Sys.Locale
}
return flattenedFields, nil
}
// flattenField injects the references from "includes" object to the field therefore flattening the json.
// Example:
// response in json:
// {
// "items": [
// {
// "fields": {
// "reference": {
// "sys": {
// "type": "Link",
// "linkType": "Entry",
// "id": "entryID"
// }
// }
// }
// }
// ],
// "includes": {
// "Entry": [
// {
// "sys": {
// "type": "Entry",
// "id": "entryID"
// },
// "fields": {
// "key": "value"
// }
// },
// ],
// "Asset": [...]
// }
// }
//
// where includes object is
// "includes": {
// "Entry": [
// {
// "sys": {
// "type": "Entry",
// "id": "entryID"
// },
// "fields": {
// "key": "value"
// }
// },
// ],
// "Asset": [...]
// }
//
// and where field is
// "reference": {
// "sys": {
// "type": "Link",
// "linkType": "Entry",
// "id": "entryID"
// }
// }
//
// Return value will then be
// "reference": {
// "key": "value"
// }
func flattenField(includes includes, field interface{}) (interface{}, error) {
switch t := field.(type) {
// Either multiple references or values, flatten each individually
case []interface{}:
flattenedFields := make([]interface{}, len(t))
for i, v := range t {
flattenedField, err := flattenField(includes, v)
if err != nil {
return field, err
}
flattenedFields[i] = flattenedField
}
return flattenedFields, nil
// Reference or an object
case map[string]interface{}:
// Reference
if sys, ok := parseToSys(t["sys"]); ok {
return fetchReference(includes, sys)
}
// Field is not a reference but an object. Flatten like as if it were an item in search result.
flattenedItem, err := flattenItem(includes, item{Fields: t})
if err != nil {
return field, err
}
return flattenedItem, nil
// Plain value
default:
return field, nil
}
}
func parseToSys(field interface{}) (sys, bool) {
mapSys, ok := field.(map[string]interface{})
if !ok {
return sys{}, false
}
id, ok := mapSys["id"].(string)
if !ok {
return sys{}, false
}
linkTypeValue, ok := mapSys["linkType"].(string)
if !ok {
return sys{}, false
}
typeValue, ok := mapSys["type"].(string)
if !ok {
return sys{}, false
}
sys := sys{
ID: id,
LinkType: linkTypeValue,
Type: typeValue,
}
return sys, sys.ID != "" && (sys.LinkType == linkTypeAsset || sys.LinkType == linkTypeEntry) && sys.Type == linkType
}
func fetchReference(includes includes, sys sys) (interface{}, error) {
var (
references []item
found bool
item item
)
if sys.LinkType == linkTypeEntry {
references = includes.Entry
} else if sys.LinkType == linkTypeAsset {
references = includes.Asset
} else {
return struct{}{}, fmt.Errorf("link type is not %s or %s, but instead %s", linkTypeEntry, linkTypeAsset, sys.LinkType)
}
for _, ref := range references {
if ref.Sys.ID == sys.ID && ref.Sys.Type == sys.LinkType {
found = true
item = ref
break
}
}
if !found {
// TODO: try to fetch data separately
refString := "Could not convert to string"
bytes, err := json.MarshalIndent(references, "", " ")
if err == nil {
refString = string(bytes)
}
return struct{}{}, fmt.Errorf("could not find a reference with type %s and with id %s.\nReferences:\n%s", sys.LinkType, sys.ID, refString)
}
return flattenItem(includes, item)
}