-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsync_pg_utils.go
266 lines (249 loc) · 7.82 KB
/
sync_pg_utils.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package gontentful
import (
"encoding/json"
"fmt"
"log"
"strings"
"github.com/lib/pq"
"github.com/mitchellh/mapstructure"
)
type rowField struct {
fieldName string
fieldValue interface{}
}
type columnData struct {
fieldColumns []string
columnReferences map[string]string
localizedColumns map[string]bool
}
func appendTables(schema *PGSyncSchema, item *Entry, tableName string, fieldColumns []string, refColumns map[string]string, localizedColumns map[string]bool, templateFormat bool) {
fieldsByLocale := make(map[string][]*rowField, 0)
defaultLocale := schema.DefaultLocale
fbLocales := make(map[string]*Locale)
for _, loc := range schema.Locales {
fbLocales[loc.Code] = loc
}
// iterate over fields
for fieldName, f := range item.Fields {
locFields, ok := f.(map[string]interface{})
if !ok {
continue // no locale, continue
}
// snace_case column name
columnName := toSnakeCase(fieldName)
// iterate over locale fields
for _, loc := range schema.Locales {
// create table
tbl := schema.Tables[tableName]
if tbl == nil {
tbl = newPGSyncTable(tableName, fieldColumns)
schema.Tables[tableName] = tbl
}
locale := strings.ToLower(loc.Code)
locCode := loc.Code
if !localizedColumns[columnName] {
locCode = defaultLocale
}
fieldValue := locFields[locCode]
fallbackLoc := loc
for {
if sv, ok := fieldValue.(string); fieldValue == nil || (ok && sv == "") {
if fallbackLoc.FallbackCode != "" {
fieldValue = locFields[fallbackLoc.FallbackCode]
fallbackLoc = fbLocales[fallbackLoc.FallbackCode]
continue
} else {
fieldValue = locFields[defaultLocale]
}
}
break
}
// if sv, ok := fieldValue.(string); fieldValue == nil || (ok && sv == "") {
// continue
// }
// collect row fields by locale
fieldsByLocale[locale] = append(fieldsByLocale[locale], &rowField{columnName, fieldValue})
}
}
// append rows with fields to tables
for locale, rowFields := range fieldsByLocale {
// table
tbl := schema.Tables[tableName]
if tbl != nil {
appendRowsToTable(item, tbl, rowFields, fieldColumns, templateFormat, schema.ConTables, schema.DeletedConTables, refColumns, tableName, locale)
}
}
}
func appendRowsToTable(item *Entry, tbl *PGSyncTable, rowFields []*rowField, fieldColumns []string, templateFormat bool, conTables map[string]*PGSyncConTable, deletedConTables map[string]*PGSyncConTable, refColumns map[string]string, tableName string, locale string) {
fieldValues := make(map[string]interface{})
id := fmtSysID(item.Sys.ID, templateFormat, locale)
fieldValues["_id"] = id
for _, rowField := range rowFields {
fieldValues[rowField.fieldName] = convertFieldValue(rowField.fieldValue, templateFormat, locale)
// append con tables with Array Links
if _, ok := refColumns[rowField.fieldName]; ok {
if links, ok := rowField.fieldValue.([]interface{}); ok {
addedRefs := make(map[string]bool)
conTableName := getConTableName(tableName, rowField.fieldName)
if conTables[conTableName] == nil {
conTables[conTableName] = &PGSyncConTable{
TableName: conTableName,
Columns: []string{tableName, fmt.Sprintf("%s_sys_id", tableName), refColumns[rowField.fieldName], fmt.Sprintf("%s_sys_id", refColumns[rowField.fieldName]), "_locale"},
Rows: make([][]interface{}, 0),
}
}
for _, e := range links {
f, ok := e.(map[string]interface{})
if ok {
sysConID := convertSysID(f, templateFormat)
conID, ok := convertSys(f, templateFormat, locale).(string)
if ok && id != "" && conID != "" && !addedRefs[conID] {
var conRow []interface{}
if templateFormat {
conRow = []interface{}{id, fmt.Sprintf("'%s'", item.Sys.ID), conID, sysConID, fmt.Sprintf("'%s'", locale)}
} else {
conRow = []interface{}{id, item.Sys.ID, conID, sysConID, locale}
}
conTables[conTableName].Rows = append(conTables[conTableName].Rows, conRow)
addedRefs[conID] = true
} else {
fmt.Println(tbl.TableName, id, rowField.fieldName, conID)
}
}
}
} else {
conTableName := getConTableName(tableName, rowField.fieldName)
if deletedConTables[conTableName] == nil {
deletedConTables[conTableName] = &PGSyncConTable{
TableName: conTableName,
Columns: []string{tableName},
Rows: make([][]interface{}, 0),
}
}
if id != "" {
conRow := []interface{}{id}
deletedConTables[conTableName].Rows = append(deletedConTables[conTableName].Rows, conRow)
}
}
}
if assetFile, ok := fieldValues[rowField.fieldName].(*AssetFile); ok {
url := assetFile.URL
fileName := assetFile.FileName
contentType := assetFile.ContentType
if templateFormat {
url = fmt.Sprintf("'%s'", url)
fileName = fmt.Sprintf("'%s'", fileName)
contentType = fmt.Sprintf("'%s'", contentType)
}
fieldValues["url"] = url
fieldValues["file_name"] = fileName
fieldValues["content_type"] = contentType
}
}
row := newPGSyncRow(item, fieldColumns, fieldValues, locale)
tbl.Rows = append(tbl.Rows, row)
}
func convertFieldValue(v interface{}, t bool, locale string) interface{} {
switch f := v.(type) {
case map[string]interface{}:
if f["sys"] != nil {
return convertSys(f, t, locale)
} else if f["fileName"] != nil {
var v *AssetFile
mapstructure.Decode(f, &v)
return v
} else {
data, err := json.Marshal(f)
if err != nil {
log.Fatal("failed to marshal content field")
}
if t {
return fmt.Sprintf("'%s'", string(data))
}
return string(data)
}
case []interface{}:
arr := make([]string, 0)
for i := 0; i < len(f); i++ {
fs := convertFieldValue(f[i], t, locale)
arr = append(arr, fmt.Sprintf("%v", fs))
}
if t {
return fmt.Sprintf("'{%s}'", strings.ReplaceAll(strings.Join(arr, ","), "'", "\""))
}
return pq.Array(arr)
case []string:
arr := make([]string, 0)
for i := 0; i < len(f); i++ {
fs := convertFieldValue(f[i], t, locale)
arr = append(arr, fmt.Sprintf("%v", fs))
}
if t {
return fmt.Sprintf("'{%s}'", strings.ReplaceAll(strings.Join(arr, ","), "'", "\""))
}
return pq.Array(arr)
case string:
if t {
return fmt.Sprintf("'%s'", strings.ReplaceAll(v.(string), "'", "''"))
}
}
return v
}
func convertSys(f map[string]interface{}, t bool, locale string) interface{} {
if s, ok := f["sys"].(map[string]interface{}); ok {
if s["type"] == LINK && s["id"] != nil {
return fmtSysID(s["id"], t, locale)
}
}
return nil
}
func fmtSysID(id interface{}, t bool, l string) string {
if t {
return fmt.Sprintf("'%v_%s'", id, l)
}
return fmt.Sprintf("%v_%s", id, l)
}
func convertSysID(f map[string]interface{}, t bool) string {
s, ok := f["sys"].(map[string]interface{})
if ok {
if s["type"] == LINK {
if t {
return fmt.Sprintf("'%v'", s["id"])
} else {
return fmt.Sprintf("%v", s["id"])
}
}
}
return ""
}
func getColumnsByContentType(types []*ContentType) map[string]*columnData {
typeColumns := make(map[string]*columnData)
for _, t := range types {
if typeColumns[t.Sys.ID] == nil {
fieldColumns, refColumns, locColumns := getContentTypeColumns(t)
typeColumns[t.Sys.ID] = &columnData{fieldColumns, refColumns, locColumns}
}
}
return typeColumns
}
func getContentTypeColumns(t *ContentType) ([]string, map[string]string, map[string]bool) {
fieldColumns := make([]string, 0)
refColumns := make(map[string]string)
localizedColumns := make(map[string]bool)
for _, f := range t.Fields {
if !f.Omitted {
colName := toSnakeCase(f.ID)
fieldColumns = append(fieldColumns, colName)
if f.Items != nil {
linkType := getFieldLinkType(f.Items.LinkType, f.Items.Validations)
if linkType != "" {
refColumns[colName] = linkType
}
}
if f.Localized && strings.ToLower(f.ID) != "slug" {
localizedColumns[colName] = true
}
}
}
return fieldColumns, refColumns, localizedColumns
}