Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(parser): Unable to parse json in one line #43

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lua/curl/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ local format_command_for_curl = function(lines)
if found_first_json_char(json_nesting_stack, cleaned_line) then
opening_json_char = cleaned_line:sub(1, 1)
closing_json_char = opening_json_char == "[" and "]" or "}"
is_line_json_close(json_nesting_stack, opening_json_char, closing_json_char, cleaned_line)

cleaned_line = "'" .. cleaned_line

if is_line_json_close(json_nesting_stack, opening_json_char, closing_json_char, cleaned_line) then
cleaned_line = cleaned_line .. "'"
end
elseif #json_nesting_stack > 0 then
local found_json_end =
is_line_json_close(json_nesting_stack, opening_json_char, closing_json_char, cleaned_line)
Expand Down
17 changes: 17 additions & 0 deletions tests/parser/parser_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,21 @@ describe("Has feature", function()
test_util.assert_commands(expected_command, parsed_command)
end
end)

it("json content in one line", function()
local input_buffer = {
"curl -X POST https://jsonplaceholder.typicode.com/posts",
"-H 'Content-Type: application/json'",
"-d",
'{ "title": "now try this" }',
}

local expected_command =
"curl -X POST https://jsonplaceholder.typicode.com/posts -H 'Content-Type: application/json' -d '{ \"title\": \"now try this\" }'"

for index = 1, #input_buffer do
local parsed_command = parser.parse_curl_command(index, input_buffer)
test_util.assert_commands(expected_command, parsed_command)
end
end)
end)
Loading