This repository was archived by the owner on May 27, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathstruct.pb.ex
More file actions
128 lines (95 loc) · 3.36 KB
/
Copy pathstruct.pb.ex
File metadata and controls
128 lines (95 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
defmodule Google.Protobuf.NullValue do
@moduledoc false
use Protobuf, enum: true, syntax: :proto3, protoc_gen_elixir_version: "0.12.0"
field :NULL_VALUE, 0
end
defmodule Google.Protobuf.Struct.FieldsEntry do
@moduledoc false
use Protobuf, map: true, syntax: :proto3, protoc_gen_elixir_version: "0.12.0"
field :key, 1, type: :string
field :value, 2, type: Google.Protobuf.Value
end
defmodule Google.Protobuf.Struct do
@moduledoc false
use Protobuf, syntax: :proto3, protoc_gen_elixir_version: "0.12.0"
field :fields, 1, repeated: true, type: Google.Protobuf.Struct.FieldsEntry, map: true
@doc """
Converts a `Google.Protobuf.Struct` to a `map()`.
## Examples
iex> Struct.to_map(%Struct{})
%{}
"""
@spec to_map(__MODULE__.t()) :: map()
def to_map(struct) do
Map.new(struct.fields, fn {k, v} ->
{k, to_map_value(v)}
end)
end
defp to_map_value(%{kind: {:null_value, :NULL_VALUE}}), do: nil
defp to_map_value(%{kind: {:number_value, value}}), do: value
defp to_map_value(%{kind: {:string_value, value}}), do: value
defp to_map_value(%{kind: {:bool_value, value}}), do: value
defp to_map_value(%{kind: {:struct_value, struct}}),
do: to_map(struct)
defp to_map_value(%{kind: {:list_value, %{values: values}}}),
do: Enum.map(values, &to_map_value/1)
@doc """
Converts a `map()` to a `Google.Protobuf.Struct`.
## Examples
iex> Struct.from_map(%{key: "value"})
%Struct{}
"""
@spec from_map(map()) :: __MODULE__.t()
def from_map(map) do
struct(__MODULE__, %{
fields:
Map.new(map, fn {k, v} ->
{to_string(k), from_map_value(v)}
end)
})
end
defp from_map_value(nil) do
struct(Google.Protobuf.Value, %{kind: {:null_value, :NULL_VALUE}})
end
defp from_map_value(value) when is_number(value) do
struct(Google.Protobuf.Value, %{kind: {:number_value, value}})
end
defp from_map_value(value) when is_binary(value) do
struct(Google.Protobuf.Value, %{kind: {:string_value, value}})
end
defp from_map_value(value) when is_boolean(value) do
struct(Google.Protobuf.Value, %{kind: {:bool_value, value}})
end
defp from_map_value(value) when is_map(value) do
struct(Google.Protobuf.Value, %{kind: {:struct_value, from_map(value)}})
end
defp from_map_value(value) when is_list(value) do
struct(Google.Protobuf.Value, %{
kind:
{:list_value,
struct(Google.Protobuf.ListValue, %{
values: Enum.map(value, &from_map_value/1)
})}
})
end
end
defmodule Google.Protobuf.Value do
@moduledoc false
use Protobuf, syntax: :proto3, protoc_gen_elixir_version: "0.12.0"
oneof :kind, 0
field :null_value, 1,
type: Google.Protobuf.NullValue,
json_name: "nullValue",
enum: true,
oneof: 0
field :number_value, 2, type: :double, json_name: "numberValue", oneof: 0
field :string_value, 3, type: :string, json_name: "stringValue", oneof: 0
field :bool_value, 4, type: :bool, json_name: "boolValue", oneof: 0
field :struct_value, 5, type: Google.Protobuf.Struct, json_name: "structValue", oneof: 0
field :list_value, 6, type: Google.Protobuf.ListValue, json_name: "listValue", oneof: 0
end
defmodule Google.Protobuf.ListValue do
@moduledoc false
use Protobuf, syntax: :proto3, protoc_gen_elixir_version: "0.12.0"
field :values, 1, repeated: true, type: Google.Protobuf.Value
end