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

Encoding map objects to string during escaping process #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions lib/clickhousex/codec/values.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ defmodule Clickhousex.Codec.Values do
["'", escape(param), "'"]
end

defp escape(s) when is_map(s) do
m = Jason.encode!(s)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can it be Jason.encode_to_iodata!/1?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! Thank you for the suggestion!

Copy link

@ruslandoga ruslandoga Nov 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't JSON iodata still need to be escaped?

With the current implementation I'm getting

Code: 62. DB::Exception: Cannot parse expression of type Object('json') here: '{\"should\":{\"'\":\"%\",\"\\\\\":\"\",\"_\":\"?\",\"it be\":\"escaped?\"}}'

for a map like

%{"should" => %{"it be" => "escaped?", "_" => "?", "'" => "%", "\\" => ""}}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the confusion. I think the previous approach with Jason.encode! is the way to go since escaping iodata seems like too much work.

to_iodata(m, 0, m, [])
end

defp escape(s) do
to_iodata(s, 0, s, [])
end
Expand Down