Follow-up to 14101
Improvement in the code that @the-mikedavis noticed just before #14118 was merged.
This commit is contained in:
parent
b75fc23770
commit
00528cb1e8
|
@ -92,8 +92,7 @@ defmodule RabbitMQ.CLI.Formatters.Json do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp convert_erlang_strings(data) when is_list(data) do
|
defp convert_erlang_strings(data) when is_list(data) do
|
||||||
# Only attempt Unicode conversion on proper lists of integers
|
try do
|
||||||
if is_proper_list_of_integers?(data) do
|
|
||||||
case :unicode.characters_to_binary(data, :utf8) do
|
case :unicode.characters_to_binary(data, :utf8) do
|
||||||
binary when is_binary(binary) ->
|
binary when is_binary(binary) ->
|
||||||
# Successfully converted - it was a valid Unicode string
|
# Successfully converted - it was a valid Unicode string
|
||||||
|
@ -102,8 +101,9 @@ defmodule RabbitMQ.CLI.Formatters.Json do
|
||||||
# Conversion failed - not a Unicode string, process as regular list
|
# Conversion failed - not a Unicode string, process as regular list
|
||||||
Enum.map(data, &convert_erlang_strings/1)
|
Enum.map(data, &convert_erlang_strings/1)
|
||||||
end
|
end
|
||||||
else
|
rescue
|
||||||
# Not a proper list of integers, process as regular list
|
ArgumentError ->
|
||||||
|
# badarg exception - not valid character data, process as regular list
|
||||||
Enum.map(data, &convert_erlang_strings/1)
|
Enum.map(data, &convert_erlang_strings/1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -122,15 +122,4 @@ defmodule RabbitMQ.CLI.Formatters.Json do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp convert_erlang_strings(data), do: data
|
defp convert_erlang_strings(data), do: data
|
||||||
|
|
||||||
# Check if data is a proper list containing only integers
|
|
||||||
defp is_proper_list_of_integers?([]), do: false # Empty lists are not strings
|
|
||||||
defp is_proper_list_of_integers?(data) when is_list(data) do
|
|
||||||
try do
|
|
||||||
Enum.all?(data, &is_integer/1)
|
|
||||||
rescue
|
|
||||||
_ -> false # Not a proper list or contains non-integers
|
|
||||||
end
|
|
||||||
end
|
|
||||||
defp is_proper_list_of_integers?(_), do: false
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue