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

iolist_to_binary is usualy not the best choice for list values #8

Open
seriyps opened this issue Feb 3, 2022 · 0 comments
Open

iolist_to_binary is usualy not the best choice for list values #8

seriyps opened this issue Feb 3, 2022 · 0 comments
Labels
discussion ⏰ Discussion feature 🚀 Feature Request improvement 💡 Improvement

Comments

@seriyps
Copy link

seriyps commented Feb 3, 2022

As discussed in the forum https://erlangforums.com/t/logstasher-is-erlang-logger-formatter-for-logstash/1026/2

Just applying term_to_binary to any list that happens to be a valid iolist is not always intuitive

The one trying to log smth like that:

Tags = ["foo", "bar", "baz"],
logger:info(#{label => something_happened, tags => Tags})

would rater expect it to be logged as

{"label": "something_happened", "tags": ["foo", "bar", "baz"]}

than

{"label": "something_happened", "tags": "foobarbaz"}

In our project we ended up with smth like

term_to_json([C | _] = List) when is_integer(C) ->
    case io_lib:char_list(S) of
      true ->
        unicode:characters_to_binary(List, utf8);  %% string() or specific form of unicode:chardata()
      false ->
        lists:map(fun term_to_json/1, List)  % JS array
    end;

Another issue is that calling iolist_to_binary is not guaranteed to succeed if it's guarded by io_lib:char_list:

> S = "привет", io_lib:char_list(S).       
true
> iolist_to_binary(S).
** exception error: bad argument
     in function  iolist_to_binary/1
        called as iolist_to_binary([1087,1088,1080,1074,1077,1090])
        *** argument 1: not an iodata term

unicode:characters_to_binary(S, utf8) might be a better choice.

@vkatsuba vkatsuba added discussion ⏰ Discussion feature 🚀 Feature Request improvement 💡 Improvement labels Feb 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion ⏰ Discussion feature 🚀 Feature Request improvement 💡 Improvement
Projects
None yet
Development

No branches or pull requests

2 participants