|
| 1 | +defmodule Google.Protobuf do |
| 2 | + @moduledoc """ |
| 3 | + Utility functions for working with Google Protobuf structs. |
| 4 | + """ |
| 5 | + |
| 6 | + @doc """ |
| 7 | + Converts a `Google.Protobuf.Struct` struct to a `t:map()` recursively |
| 8 | + converting values to their Elixir equivalents. |
| 9 | +
|
| 10 | + ## Examples |
| 11 | +
|
| 12 | + iex> to_map(%Google.Protobuf.Struct{}) |
| 13 | + %{} |
| 14 | +
|
| 15 | + iex> to_map(%Google.Protobuf.Struct{ |
| 16 | + fields: %{ |
| 17 | + "key_one" => %Google.Protobuf.Value{ |
| 18 | + kind: {:string_value, "value_one"}, |
| 19 | + }, |
| 20 | + "key_two" => %Google.Protobuf.Value{ |
| 21 | + kind: {:number_value, 1234.0}, |
| 22 | + } |
| 23 | + }, |
| 24 | + }) |
| 25 | + %{"key_one" => "value_one", "key_two" => 1234.0} |
| 26 | +
|
| 27 | + """ |
| 28 | + @spec to_map(Google.Protobuf.Struct.t()) :: map() |
| 29 | + def to_map(struct) do |
| 30 | + Map.new(struct.fields, fn {k, v} -> |
| 31 | + {k, to_map_value(v)} |
| 32 | + end) |
| 33 | + end |
| 34 | + |
| 35 | + defp to_map_value(%{kind: {:null_value, :NULL_VALUE}}), do: nil |
| 36 | + defp to_map_value(%{kind: {:number_value, value}}), do: value |
| 37 | + defp to_map_value(%{kind: {:string_value, value}}), do: value |
| 38 | + defp to_map_value(%{kind: {:bool_value, value}}), do: value |
| 39 | + |
| 40 | + defp to_map_value(%{kind: {:struct_value, struct}}), |
| 41 | + do: to_map(struct) |
| 42 | + |
| 43 | + defp to_map_value(%{kind: {:list_value, %{values: values}}}), |
| 44 | + do: Enum.map(values, &to_map_value/1) |
| 45 | + |
| 46 | + @doc """ |
| 47 | + Converts a `t:map()` to a `Google.Protobuf.Struct` struct recursively |
| 48 | + wrapping values in their `Google.Protobuf.Value` equivalents. |
| 49 | +
|
| 50 | + ## Examples |
| 51 | +
|
| 52 | + iex> from_map(%{}) |
| 53 | + %Google.Protobuf.Struct{} |
| 54 | +
|
| 55 | + """ |
| 56 | + @spec from_map(map()) :: Google.Protobuf.Struct.t() |
| 57 | + def from_map(map) do |
| 58 | + struct(Google.Protobuf.Struct, %{ |
| 59 | + fields: |
| 60 | + Map.new(map, fn {k, v} -> |
| 61 | + {to_string(k), from_map_value(v)} |
| 62 | + end) |
| 63 | + }) |
| 64 | + end |
| 65 | + |
| 66 | + defp from_map_value(nil) do |
| 67 | + struct(Google.Protobuf.Value, %{kind: {:null_value, :NULL_VALUE}}) |
| 68 | + end |
| 69 | + |
| 70 | + defp from_map_value(value) when is_number(value) do |
| 71 | + struct(Google.Protobuf.Value, %{kind: {:number_value, value}}) |
| 72 | + end |
| 73 | + |
| 74 | + defp from_map_value(value) when is_binary(value) do |
| 75 | + struct(Google.Protobuf.Value, %{kind: {:string_value, value}}) |
| 76 | + end |
| 77 | + |
| 78 | + defp from_map_value(value) when is_boolean(value) do |
| 79 | + struct(Google.Protobuf.Value, %{kind: {:bool_value, value}}) |
| 80 | + end |
| 81 | + |
| 82 | + defp from_map_value(value) when is_map(value) do |
| 83 | + struct(Google.Protobuf.Value, %{kind: {:struct_value, from_map(value)}}) |
| 84 | + end |
| 85 | + |
| 86 | + defp from_map_value(value) when is_list(value) do |
| 87 | + struct(Google.Protobuf.Value, %{ |
| 88 | + kind: |
| 89 | + {:list_value, |
| 90 | + struct(Google.Protobuf.ListValue, %{ |
| 91 | + values: Enum.map(value, &from_map_value/1) |
| 92 | + })} |
| 93 | + }) |
| 94 | + end |
| 95 | + |
| 96 | + @doc """ |
| 97 | + Converts a `DateTime` struct to a `Google.Protobuf.Timestamp` struct. |
| 98 | +
|
| 99 | + Note: Elixir `DateTime.from_unix!/2` will convert units to |
| 100 | + microseconds internally. Nanosecond precision is not guaranteed. |
| 101 | + See examples for details. |
| 102 | +
|
| 103 | + ## Examples |
| 104 | +
|
| 105 | + iex> to_datetime(%Google.Protobuf.Timestamp{seconds: 5, nanos: 0}) |
| 106 | + ~U[1970-01-01 00:00:05.000000Z] |
| 107 | +
|
| 108 | + iex> one = to_datetime(%Google.Protobuf.Timestamp{seconds: 10, nanos: 100}) |
| 109 | + ...> two = to_datetime(%Google.Protobuf.Timestamp{seconds: 10, nanos: 105}) |
| 110 | + ...> DateTime.diff(one, two, :nanosecond) |
| 111 | + 0 |
| 112 | +
|
| 113 | + """ |
| 114 | + @spec to_datetime(Google.Protobuf.Timestamp.t()) :: DateTime.t() |
| 115 | + def to_datetime(%{seconds: seconds, nanos: nanos}) do |
| 116 | + DateTime.from_unix!(seconds * 1_000_000_000 + nanos, :nanosecond) |
| 117 | + end |
| 118 | + |
| 119 | + @doc """ |
| 120 | + Converts a `Google.Protobuf.Timestamp` struct to a `DateTime` struct. |
| 121 | +
|
| 122 | + ## Examples |
| 123 | +
|
| 124 | + iex> from_datetime(~U[1970-01-01 00:00:05.000000Z]) |
| 125 | + %Google.Protobuf.Timestamp{seconds: 5, nanos: 0} |
| 126 | +
|
| 127 | + """ |
| 128 | + @spec from_datetime(DateTime.t()) :: Google.Protobuf.Timestamp.t() |
| 129 | + def from_datetime(%DateTime{} = datetime) do |
| 130 | + nanoseconds = DateTime.to_unix(datetime, :nanosecond) |
| 131 | + |
| 132 | + struct(Google.Protobuf.Timestamp, %{ |
| 133 | + seconds: div(nanoseconds, 1_000_000_000), |
| 134 | + nanos: rem(nanoseconds, 1_000_000_000) |
| 135 | + }) |
| 136 | + end |
| 137 | +end |
0 commit comments