Skip to content

Commit a2806a7

Browse files
committed
rework to_string / serialization
1 parent aacccd4 commit a2806a7

2 files changed

Lines changed: 34 additions & 27 deletions

File tree

lua/CopilotChat/config/providers.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,12 @@ M.copilot = {
242242

243243
if response.body and response.body.endpoints and response.body.endpoints.api then
244244
log.info('get_headers ok, authenticated. Use api endpoint: ' .. response.body.endpoints.api)
245+
log.info(utils.to_string(response)) --TODO: remove me, testing only
245246
M.endpoints_api = response.body.endpoints.api
246247
else
247248
log.error(
248249
'get_headers authenticated, but missing key "endpoints.api" in server response. response: '
249-
.. utils.serializeTable(response)
250+
.. utils.to_string(response)
250251
)
251252
error(
252253
'get_headers authenticated, but missing key "endpoints.api" in server response. Check log for details: '

lua/CopilotChat/utils.lua

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -450,38 +450,44 @@ M.curl_post = async.wrap(function(url, opts, callback)
450450
curl.post(url, args)
451451
end, 3)
452452

453-
function M.serializeTable(val, name, skipnewlines, depth)
454-
skipnewlines = skipnewlines or false
455-
depth = depth or 0
456-
457-
local tmp = string.rep(' ', depth)
458-
459-
if name then
460-
tmp = tmp .. name .. ' = '
461-
end
462-
463-
if type(val) == 'table' then
464-
tmp = tmp .. '{' .. (not skipnewlines and '\n' or '')
465-
466-
for k, v in pairs(val) do
467-
tmp = tmp .. M.serializeTable(v, k, skipnewlines, depth + 1) .. ',' .. (not skipnewlines and '\n' or '')
453+
function M.to_string(tbl)
454+
-- credit: http://lua-users.org/wiki/TableSerialization (universal tostring)
455+
local function table_print(tt, indent, done)
456+
done = done or {}
457+
indent = indent or 0
458+
if type(tt) == 'table' then
459+
local sb = {}
460+
for key, value in pairs(tt) do
461+
table.insert(sb, string.rep(' ', indent)) -- indent it
462+
if type(value) == 'table' and not done[value] then
463+
done[value] = true
464+
table.insert(sb, key .. ' = {\n')
465+
table.insert(sb, table_print(value, indent + 2, done))
466+
table.insert(sb, string.rep(' ', indent)) -- indent it
467+
table.insert(sb, '}\n')
468+
elseif 'number' == type(key) then
469+
table.insert(sb, string.format('"%s"\n', tostring(value)))
470+
else
471+
table.insert(sb, string.format('%s = "%s"\n', tostring(key), tostring(value)))
472+
end
473+
end
474+
return table.concat(sb)
475+
else
476+
return tt .. '\n'
468477
end
478+
end
469479

470-
tmp = tmp .. string.rep(' ', depth) .. '}'
471-
elseif type(val) == 'number' then
472-
tmp = tmp .. tostring(val)
473-
elseif type(val) == 'string' then
474-
tmp = tmp .. string.format('%q', val)
475-
elseif type(val) == 'boolean' then
476-
tmp = tmp .. (val and 'true' or 'false')
480+
if 'nil' == type(tbl) then
481+
return tostring(nil)
482+
elseif 'table' == type(tbl) then
483+
return table_print(tbl)
484+
elseif 'string' == type(tbl) then
485+
return tbl
477486
else
478-
tmp = tmp .. '"[inserializeable datatype:' .. type(val) .. ']"'
487+
return tostring(tbl)
479488
end
480-
481-
return tmp
482489
end
483490

484-
485491
local function filter_files(files, max_count)
486492
local filetype = require('plenary.filetype')
487493

0 commit comments

Comments
 (0)