rabbitmq-server/deps/rabbitmq_cli/lib/rabbitmq/cli/formatters/inspect.ex

41 lines
1.1 KiB
Elixir
Raw Normal View History

## This Source Code Form is subject to the terms of the Mozilla Public
## License, v. 2.0. If a copy of the MPL was not distributed with this
## file, You can obtain one at https://mozilla.org/MPL/2.0/.
##
2021-02-14 05:54:01 +08:00
## Copyright (c) 2007-2021 VMware, Inc. or its affiliates. All rights reserved.
2018-07-23 13:06:18 +08:00
alias RabbitMQ.CLI.Formatters.FormatterHelpers
2016-08-22 19:58:37 +08:00
defmodule RabbitMQ.CLI.Formatters.Inspect do
2016-11-15 01:52:08 +08:00
@behaviour RabbitMQ.CLI.FormatterBehaviour
2016-08-22 19:58:37 +08:00
def format_output(output, _) do
2016-12-07 20:47:28 +08:00
case is_binary(output) do
2019-01-31 03:20:29 +08:00
true -> output
2016-08-22 19:58:37 +08:00
false -> inspect(output)
end
end
def format_stream(stream, options) do
2019-01-31 03:20:29 +08:00
elements =
Stream.scan(
stream,
:empty,
FormatterHelpers.without_errors_2(fn element, previous ->
separator =
case previous do
:empty -> ""
_ -> ","
end
format_element(element, separator, options)
end)
)
Stream.concat([["["], elements, ["]"]])
end
2016-08-22 21:13:38 +08:00
def format_element(val, separator, options) do
separator <> format_output(val, options)
end
2016-12-07 20:47:28 +08:00
end