Skip to content

Commit

Permalink
add mapping/0 to Enum message
Browse files Browse the repository at this point in the history
  • Loading branch information
tony612 committed Apr 26, 2019
1 parent fd42cfd commit 8c86518
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/protobuf/dsl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ defmodule Protobuf.DSL do
end)
if !found, do: raise "The first enum value must be zero in proto3"
end
mapping = Enum.reduce(props, %{}, fn {_, %{fnum: fnum, name_atom: name_atom}}, acc ->
Map.put(acc, name_atom, fnum)
end)
Enum.map(props, fn {_, %{fnum: fnum, name_atom: name_atom}} ->
quote do
def value(unquote(name_atom)), do: unquote(fnum)
Expand All @@ -70,7 +73,11 @@ defmodule Protobuf.DSL do
quote do
def key(unquote(fnum)), do: unquote(name_atom)
end
end)
end) ++ [
quote do
def mapping(), do: unquote(Macro.escape(mapping))
end
]
end

defp def_enum_functions(_), do: nil
Expand Down
1 change: 1 addition & 0 deletions test/protobuf/dsl_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ defmodule Protobuf.DSLTest do
assert TestMsg.EnumFoo.key(1) == :A
assert TestMsg.EnumFoo.key(2) == :B
assert TestMsg.EnumFoo.key(4) == :C
assert TestMsg.EnumFoo.mapping() == %{UNKNOWN: 0, A: 1, B: 2, C: 4}

assert %FieldProps{fnum: 11, type: {:enum, TestMsg.EnumFoo}, wire_type: 0} =
Foo.__message_props__().field_props[11]
Expand Down

0 comments on commit 8c86518

Please sign in to comment.