Skip to content

Commit 867d84d

Browse files
committed
dose not need workaround
1 parent 6f3c3d8 commit 867d84d

File tree

2 files changed

+66
-45
lines changed

2 files changed

+66
-45
lines changed

script/client.lua

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -225,21 +225,19 @@ end
225225
---@class json.patch
226226
---@field op 'add' | 'remove' | 'replace' | 'move' | 'copy' | 'test'
227227
---@field path string
228-
---@field data any
228+
---@field value any
229229

230230
---@param cfg table
231231
---@param change config.change
232232
---@return json.patch?
233233
local function makeConfigPatch(cfg, change)
234-
local value = cfg[change.key]
235-
local parent = cfg
236-
local parentKey = ''
234+
local value = cfg[change.key]
237235
if change.action == 'add' then
238236
if type(value) == 'table' and #cfg[change.key] > 0 then
239237
return {
240-
op = 'add',
241-
path = '/' .. change.key .. '/-',
242-
data = change.value,
238+
op = 'add',
239+
path = '/' .. change.key .. '/-',
240+
value = change.value,
243241
}
244242
else
245243
return makeConfigPatch(cfg, {
@@ -249,35 +247,25 @@ local function makeConfigPatch(cfg, change)
249247
})
250248
end
251249
elseif change.action == 'set' then
252-
if next(parent) then
253-
if value ~= nil then
254-
return {
255-
op = 'replace',
256-
path = '/' .. change.key,
257-
data = change.value,
258-
}
259-
else
260-
return {
261-
op = 'add',
262-
path = '/' .. change.key,
263-
data = change.value,
264-
}
265-
end
250+
if value ~= nil then
251+
return {
252+
op = 'replace',
253+
path = '/' .. change.key,
254+
value = change.value,
255+
}
266256
else
267-
-- TODO: workaround, json-edit cannot edit an empty object
268257
return {
269-
op = 'add',
270-
path = parentKey,
271-
data = { [change.key] = change.value },
258+
op = 'add',
259+
path = '/' .. change.key,
260+
value = change.value,
272261
}
273262
end
274263
elseif change.action == 'prop' then
275-
if type(value) == 'table' and #value == 0 and next(value) then
276-
-- TODO: workaround, json-edit cannot edit an empty object
264+
if type(value) == 'table' and #value == 0 then
277265
return {
278-
op = 'add',
279-
path = '/' .. change.key .. '/' .. change.prop,
280-
data = change.value,
266+
op = 'add',
267+
path = '/' .. change.key .. '/' .. change.prop,
268+
value = change.value,
281269
}
282270
else
283271
return makeConfigPatch(cfg, {

script/json-edit.lua

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ local function query_(ast, pathlst, n)
425425
end
426426
end
427427
if n == #pathlst then
428-
return data, k, isarray
428+
return ast, k, isarray
429429
end
430430
return query_(data[k], pathlst, n + 1)
431431
end
@@ -499,6 +499,19 @@ local function apply_array_insert_after(str, option, value, node)
499499
.. finish_text
500500
end
501501

502+
local function apply_array_insert_empty(str, option, value, node)
503+
local start_text = str:sub(1, node.s)
504+
local finish_text = str:sub(node.f-1)
505+
option.depth = option.depth + node.d + 1
506+
return start_text
507+
.. option.newline
508+
.. string_rep(option.indent, option.depth)
509+
.. json.beautify(value, option)
510+
.. option.newline
511+
.. string_rep(option.indent, option.depth-1)
512+
.. finish_text
513+
end
514+
502515
local function apply_replace(str, option, value, node)
503516
local start_text = str:sub(1, node.s-1)
504517
local finish_text = str:sub(node.f)
@@ -509,7 +522,7 @@ local function apply_replace(str, option, value, node)
509522
end
510523

511524
local function apply_object_insert(str, option, value, t, k)
512-
local node = find_max_node(t)
525+
local node = find_max_node(t.v)
513526
if node then
514527
local start_text = str:sub(1, node.f-1)
515528
local finish_text = str:sub(node.f)
@@ -523,6 +536,20 @@ local function apply_object_insert(str, option, value, t, k)
523536
.. '": '
524537
.. json.beautify(value, option)
525538
.. finish_text
539+
else
540+
local start_text = str:sub(1, t.s)
541+
local finish_text = str:sub(t.f-1)
542+
option.depth = option.depth + t.d + 1
543+
return start_text
544+
.. option.newline
545+
.. string_rep(option.indent, option.depth)
546+
.. '"'
547+
.. json._encode_string(k)
548+
.. '": '
549+
.. json.beautify(value, option)
550+
.. option.newline
551+
.. string_rep(option.indent, option.depth-1)
552+
.. finish_text
526553
end
527554
end
528555

@@ -553,14 +580,16 @@ function OP.add(str, option, path, value)
553580
return
554581
end
555582
if isarray then
556-
if t[k] then
557-
return apply_array_insert_before(str, option, value, t[k])
583+
if t.v[k] then
584+
return apply_array_insert_before(str, option, value, t.v[k])
585+
elseif k == 1 then
586+
return apply_array_insert_empty(str, option, value, t)
558587
else
559-
return apply_array_insert_after(str, option, value, t[k-1])
588+
return apply_array_insert_after(str, option, value, t.v[k-1])
560589
end
561590
else
562-
if t[k] then
563-
return apply_replace(str, option, value, t[k])
591+
if t.v[k] then
592+
return apply_replace(str, option, value, t.v[k])
564593
else
565594
return apply_object_insert(str, option, value, t, k)
566595
end
@@ -577,15 +606,15 @@ function OP.remove(str, _, path)
577606
return
578607
end
579608
if isarray then
580-
if k > #t then
609+
if k > #t.v then
581610
return
582611
end
583-
return apply_remove(str, t[k].s, t[k].f)
612+
return apply_remove(str, t.v[k].s, t.v[k].f)
584613
else
585-
if t[k] == nil then
614+
if t.v[k] == nil then
586615
return
587616
end
588-
return apply_remove(str, t[k].key_s, t[k].f)
617+
return apply_remove(str, t.v[k].key_s, t.v[k].f)
589618
end
590619
end
591620

@@ -601,11 +630,15 @@ function OP.replace(str, option, path, value)
601630
if not t then
602631
return
603632
end
604-
if t[k] then
605-
return apply_replace(str, option, value, t[k])
633+
if t.v[k] then
634+
return apply_replace(str, option, value, t.v[k])
606635
else
607636
if isarray then
608-
return apply_array_insert_after(str, option, value, t[k-1])
637+
if k == 1 then
638+
return apply_array_insert_empty(str, option, value, t)
639+
else
640+
return apply_array_insert_after(str, option, value, t.v[k-1])
641+
end
609642
else
610643
return apply_object_insert(str, option, value, t, k)
611644
end
@@ -618,7 +651,7 @@ local function edit(str, patch, option)
618651
return
619652
end
620653
option = json.beautify_option(option)
621-
return f(str, option, patch.path, patch.data)
654+
return f(str, option, patch.path, patch.value)
622655
end
623656

624657
json.edit = edit

0 commit comments

Comments
 (0)