Repository navigation
Expand file tree
/
Copy pathproduct_merge.lua
More file actions
244 lines (213 loc) · 6.46 KB
/
Copy pathproduct_merge.lua
File metadata and controls
244 lines (213 loc) · 6.46 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
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
local function append_all(target, source)
if source == nil then
return target
end
for i = 1, #source do
target[#target + 1] = source[i]
end
return target
end
local function canonical(value)
local value_type = type(value)
if value_type == "nil" then
return "null"
end
if value_type == "boolean" or value_type == "number" then
return tostring(value)
end
if value_type == "string" then
return string.format("%q", value)
end
if value_type == "userdata" then
return tostring(value)
end
if value_type ~= "table" then
error("unsupported value type: " .. value_type)
end
local keys = {}
for key in pairs(value) do
keys[#keys + 1] = key
end
table.sort(keys, function(left, right)
return tostring(left) < tostring(right)
end)
local parts = {"{"}
for i = 1, #keys do
local key = keys[i]
parts[#parts + 1] = canonical(key)
parts[#parts + 1] = ":"
parts[#parts + 1] = canonical(value[key])
parts[#parts + 1] = ","
end
parts[#parts + 1] = "}"
return table.concat(parts)
end
local function deduplicate(items, key_function)
local result = weir.array()
local seen = {}
for i = 1, #items do
local item = items[i]
local key = key_function(item)
if not seen[key] then
seen[key] = true
result[#result + 1] = item
end
end
return result
end
local function keep_tail(items, limit)
if #items <= limit then
return items
end
local result = weir.array()
local first = #items - limit + 1
for i = first, #items do
result[#result + 1] = items[i]
end
return result
end
local function union_strings(current, incoming)
local combined = weir.array()
append_all(combined, current)
append_all(combined, incoming)
return deduplicate(combined, function(item)
return item
end)
end
local function merge_string_set(current, incoming, field)
local values = incoming[field]
if values == nil or #values == 0 then
return
end
current[field] = union_strings(current[field], values)
end
local function history(opts)
local current, incoming, field = opts.current, opts.incoming, opts.field
local key_function, limit = opts.key_function, opts.limit
local items = current[field]
local additions = incoming[field]
if additions ~= nil and #additions > 0 then
items = items or weir.array()
append_all(items, additions)
items = deduplicate(items, key_function)
current[field] = items
end
if items ~= nil and #items > limit then
current[field] = keep_tail(items, limit)
end
end
local function replace_nonempty(current, incoming, field)
local value = incoming[field]
if value ~= nil and value ~= "" then
current[field] = value
end
end
local function replace_nonempty_array(current, incoming, field)
local value = incoming[field]
if value ~= nil and #value > 0 then
current[field] = value
end
end
local scalar_fields = {
"platform",
"country",
"id",
"url",
"title",
"description",
"condition",
"ecommerce_class",
"translated_text",
"serial_number",
}
local array_replace_fields = {
"category",
"gallery",
"prices",
"hostnames",
"pto_class",
"brands",
}
return function(current, incoming)
if current == nil then
current = {}
end
local uids = current.uids or weir.array()
if current.uid ~= nil and current.uid ~= "" then
uids[#uids + 1] = current.uid
end
if incoming.uid ~= nil and incoming.uid ~= "" then
uids[#uids + 1] = incoming.uid
current.uid = incoming.uid
end
append_all(uids, incoming.uids)
uids = deduplicate(uids, function(item)
return item
end)
local nonempty_uids = weir.array()
for i = 1, #uids do
if uids[i] ~= "" then
nonempty_uids[#nonempty_uids + 1] = uids[i]
end
end
current.uids = nonempty_uids
for i = 1, #scalar_fields do
replace_nonempty(current, incoming, scalar_fields[i])
end
for i = 1, #array_replace_fields do
replace_nonempty_array(current, incoming, array_replace_fields[i])
end
if incoming.brand ~= nil and incoming.brand ~= "" then
current.brand = string.upper(incoming.brand)
end
local history_options = {current = current, incoming = incoming, field = "solds", limit = 20}
history_options.key_function = function(item)
local record_at = item.record_at or ""
if #record_at > 10 then
record_at = string.sub(record_at, 1, 10)
end
return tostring(item.sold or 0) .. "-" .. tostring(item.period_hours or 0) .. "-" .. record_at
end
history(history_options)
history_options.field = "stocks"
history_options.key_function = function(item)
return tostring(item.stock or 0) .. canonical(item.variables or {})
end
history(history_options)
if incoming.comment_count ~= nil and incoming.comment_count ~= 0 then
current.comment_count = incoming.comment_count
end
history_options.field = "comments"
history_options.key_function = canonical
history(history_options)
if incoming.rating ~= nil then
current.rating = incoming.rating
end
if incoming.offers ~= nil and #incoming.offers > 0 then
local offers = weir.array()
append_all(offers, incoming.offers)
append_all(offers, current.offers)
current.offers = deduplicate(offers, function(item)
return item.uid
end)
end
if current.offers ~= nil and #current.offers > 50 then
current.offers = keep_tail(current.offers, 50)
end
merge_string_set(current, incoming, "allowed_countries")
merge_string_set(current, incoming, "restricted_countries")
merge_string_set(current, incoming, "languages")
merge_string_set(current, incoming, "countries_from_ip")
if incoming.last_found_at ~= nil then
current.last_found_at = incoming.last_found_at
end
if current.first_found_at == nil and incoming.first_found_at ~= nil then
current.first_found_at = incoming.first_found_at
end
if incoming.evicted_at ~= nil then
current.evicted_at = incoming.evicted_at
end
current.sold_by_platform = incoming.sold_by_platform == true
current.available = incoming.available == true
return current
end