Skip to content

Commit abb99a7

Browse files
Merge pull request #7969 from rabbitmq/mergify/bp/v3.12.x/pr-7963
CLI: make input ordering preditable when formatting a table (backport #7963)
2 parents b0804ae + bc3113a commit abb99a7

File tree

3 files changed

+17
-26
lines changed

3 files changed

+17
-26
lines changed

deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/formatter_helpers.ex

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@ defmodule RabbitMQ.CLI.Formatters.FormatterHelpers do
4848
def format_info_item(item, escaped \\ true)
4949

5050
def format_info_item(map, escaped) when is_map(map) do
51+
kv = to_predictably_ordered_keyword_list(map)
52+
5153
[
5254
"\#\{",
5355
Enum.map(
54-
map,
56+
kv,
5557
fn {k, v} ->
5658
["#{escape(k, escaped)} => ", format_info_item(v, escaped)]
5759
end
@@ -125,6 +127,14 @@ defmodule RabbitMQ.CLI.Formatters.FormatterHelpers do
125127
:io_lib.format("~1000000000000tp", [value])
126128
end
127129

130+
@spec to_predictably_ordered_keyword_list(Enumerable.t()) :: Keyword.t()
131+
def to_predictably_ordered_keyword_list(input0) do
132+
case input0 do
133+
m when is_map(m) -> Enum.sort(Keyword.new(m))
134+
other -> other
135+
end
136+
end
137+
128138
defp prettify_amqp_table(table, escaped) do
129139
for {k, t, v} <- table do
130140
{escape(k, escaped), prettify_typed_amqp_value(t, v, escaped)}

deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/table.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ defmodule RabbitMQ.CLI.Formatters.Table do
1818
fn
1919
[first | _] = element ->
2020
case FormatterHelpers.proplist?(first) or is_map(first) do
21-
true -> element
21+
true -> FormatterHelpers.to_predictably_ordered_keyword_list(element)
2222
false -> [element]
2323
end
2424

@@ -38,7 +38,10 @@ defmodule RabbitMQ.CLI.Formatters.Table do
3838
)
3939
end
4040

41-
def format_output(output, options) do
41+
def format_output(output0, options) do
42+
# on Erlang 26, map entry ordering has changed, this avoids
43+
# implicitly depending on map key order
44+
output = FormatterHelpers.to_predictably_ordered_keyword_list(output0)
4245
maybe_header(output, options)
4346
end
4447

@@ -61,6 +64,7 @@ defmodule RabbitMQ.CLI.Formatters.Table do
6164
defp format_output_1(output, options) when is_map(output) do
6265
escaped = escaped?(options)
6366
pad_to_header = pad_to_header?(options)
67+
6468
format_line(output, escaped, pad_to_header)
6569
end
6670

deps/rabbitmq_cli/test/core/table_formatter_test.exs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,6 @@ defmodule TableFormatterTest do
99

1010
@formatter RabbitMQ.CLI.Formatters.Table
1111

12-
test "format_output tab-separates map values" do
13-
assert @formatter.format_output(%{a: :apple, b: :beer}, %{}) == ["a\tb", "apple\tbeer"]
14-
15-
assert @formatter.format_output(%{a: :apple, b: :beer, c: 1}, %{}) == [
16-
"a\tb\tc",
17-
"apple\tbeer\t1"
18-
]
19-
20-
assert @formatter.format_output(%{a: "apple", b: 'beer', c: 1}, %{}) == [
21-
"a\tb\tc",
22-
"apple\t\"beer\"\t1"
23-
]
24-
end
25-
2612
test "format_output tab-separates keyword values" do
2713
assert @formatter.format_output([a: :apple, b: :beer], %{}) == ["a\tb", "apple\tbeer"]
2814

@@ -37,15 +23,6 @@ defmodule TableFormatterTest do
3723
]
3824
end
3925

40-
test "format_stream tab-separates map values" do
41-
assert @formatter.format_stream(
42-
[%{a: :apple, b: :beer, c: 1}, %{a: "aadvark", b: 'bee', c: 2}],
43-
%{}
44-
)
45-
|> Enum.to_list() ==
46-
["a\tb\tc", "apple\tbeer\t1", "aadvark\t\"bee\"\t2"]
47-
end
48-
4926
test "format_stream tab-separates keyword values" do
5027
assert @formatter.format_stream(
5128
[[a: :apple, b: :beer, c: 1], [a: "aadvark", b: 'bee', c: 2]],

0 commit comments

Comments
 (0)