Current section

Files

Jump to
forge_sdk lib forge_sdk protocol display google_protos.ex
Raw

lib/forge_sdk/protocol/display/google_protos.ex

defimpl ForgeSdk.Display, for: Google.Protobuf.Any do
@moduledoc """
Implementation of `Display` protocol for `Timestamp`
"""
alias ForgeSdk.Display
@spec display(nil | Google.Protobuf.Any.t(), any()) :: {any(), any()} | %{type: atom()}
def display(any, expand? \\ false) do
case ForgeAbi.decode_any(any) do
{:error, _} ->
case String.valid?(any.value) do
true -> Map.from_struct(any)
false -> Map.from_struct(%{any | value: Base.url_encode64(any.value, padding: false)})
end
{:ok, data} ->
type_url = any.type_url
case is_map(data) do
true ->
data
|> Display.display(expand?)
|> Map.put(:_type, get_type(type_url))
|> Map.put(:type_url, type_url)
|> Map.put(:encoded_value, Base.url_encode64(any.value, padding: false))
_ ->
%{
_type: get_type(type_url),
type_url: type_url,
data: Display.display(data, expand?),
value: get_value(any.value)
}
end
end
end
# TODO(tchen): we shall unify the name of tx (covert things like :confirm to :confirm_tx)
defp get_type(<<_::size(16)>> <> ":t:" <> type), do: "#{type}_tx"
defp get_type(type_url), do: type_url
defp get_value(value) do
case String.printable?(value) do
true -> value
false -> Base.url_encode64(value, padding: false)
end
end
end